For enquiries call:

Phone

+1-469-442-0620

HomeBlogWeb DevelopmentHow To Use Mongodb Text Search?

How To Use Mongodb Text Search?

Published
29th Sep, 2023
Views
view count loader
Read it in
8 Mins
In this article
    How To Use Mongodb Text Search?

    MongoDB, one of the top NoSQL databases, is well renowned for its rapid speed, versatile structure, scalability, and excellent indexing capabilities. Before we get into the minutiae, let's look at the bigger picture. When it comes to locating content on the internet, full-text search is a must-have function. When we see the material utilizing the phrases or keywords, the best illustration is a Google search. In this post, we will learn about MongoDB's full-text search capabilities based on text indexes.

    MongoDB debuted with an experimental feature allowing Full-Text Search via Text Indexes in version 2.4. This feature is now an essential element of the product (and is no longer an experimental feature). In this post, we'll go through the fundamentals of MongoDB's full-text search capabilities.

    Searching Text - An Essential Feature

    Consider a normal Google search to better understand the concepts of full-text search. When we use Google to find content, we enter a string of text, strings, phrases, or keywords, and a number of results are returned. Full-text search in MongoDB enables you to run complicated queries comparable to those you'd run using a search engine. You can look for phrases and stemmed variations on a word, and you can also remove specific "negated" terms from your search results.

    You can create a text index on any field in the document whose value is a string or an array of strings using MongoDB full-text search. When we construct a text index on a field, MongoDB tokenizes and stems the text content of the indexed field and creates the indexes accordingly.

    Here are some other circumstances in which we might see a full-text search:

    • Consider searching Wiki for your favorite topic. When you input a search term on Wiki, the search engine returns results for all articles that include the keywords/phrases you entered (even if those keywords were used deep inside the article). The relevancy of these search results is determined by their matched score. 
    • Consider a social networking site where a user may conduct a search to find all the posts that contain the term cats in them; or, to be more specific, all the posts that have comments that contain the word cats.

    Let us now look at some practical examples to help us understand things better. I'd like you to follow along with me by running the examples in mongo shell. We'll start by creating some example data that we'll use throughout the tutorial, and then we'll go through key ideas.

    To begin, connect to your MongoDB server: 

    Once you have MongoDB installed on your system, you can use mongo shell to connect with a MongoDB server.

    Run the mongo command from your command prompt to launch the MongoDB shell. The mongo command, by default, launches a shell linked to a locally installed MongoDB instance running on port 27017.

    Run the mongo command without any further parameters:

    >mongo

    This will produce a welcome message with information about the server to which the shell is connected, as well as the version of MongoDB that is installed.

    How To Use Mongodb Text Search?
    Congrats, you are in mongo shell.

    Now run the following commands:

    >use message

    Let's use the following statement to insert some documents.

    db.message.insert({"subject":"Ishan is having a dog", "content":"Dogs are most loyal pet", "likes": 60, "year":2015, "language":"english"})
    
    db.message.insert({"subject":"Dogs eat cats", "content":"Cats are not evil", "likes": 30, "year":2015, "language":"english"})
    
    db.message.insert({"subject":"Cats eat rats", "content":"Rats like cheese", "likes": 55, "year":2014, "language":"english"})

    Creating an Index

    To execute a text search, we must first establish a text index on the fields. This can be done on a single or numerous fields. The statement below will generate a text index on a single field.

    >db.message.createIndex({"subject":"text"})

    Let's use the following statement to insert some documents.

    db.message.insert({"subject":"Ishan is having a dog", "content":"Dogs are most loyal pet", "likes": 60, "year":2015, "language":"english"})
    
    db.message.insert({"subject":"Dogs eat cats", "content":"Cats are not evil", "likes": 30, "year":2015, "language":"english"})
    
    db.message.insert({"subject":"Cats eat rats", "content":"Rats like cheese", "likes": 55, "year":2014, "language":"english"})

    Creating an Index

    To execute a text search, we must first establish a text index on the fields. This can be done on a single or numerous fields. The statement below will generate a text index on a single field.

    >db.message.createIndex({"subject":"text"})

    How To Use Mongodb Text Search?
    We will generate a text index based on the description and subtitle attributes. In MongoDB, we can only construct one text index per collection. So, using the following line, we will establish a compound text index.

    db.messages.createIndex({"subject":"text","content":"text"})

    Using the $text Operator

    The $text operator can also be used to search a text index. This operator is used to perform text search operations on a text-indexed collection. This operator tokenizes each search string with whitespace and treats most punctuation as delimiters with the exception of – and \." It performs a logical OR operation on the tokens after tokenizing the search phrase. Use the $meta query operator to sort the generated documents.

    Syntax: 
    $text: 
    { 
         $search: <string>, 
         $language: <string>, 
         $caseSensitive: <boolean>, 
         $diacriticSensitive: <boolean> 
    }

    $search Field

    We'll now try to find documents with the keywords 'dog' in the topic and content boxes. We can use the following sentence to accomplish this.

    > db.message.find({$text: {$search: "dog"}})

    Example:

    > db.message.find({$text: {$search: "dog"}},{ subject: 1, content:1}) 
    This will give the output as:   
    { "_id" : ObjectId("6176ce6de02fd70a168ad9c6"), "subject" : "Ishan is having a dog", "content" : "Dogs are most loyal pet" } 
    { "_id" : ObjectId("6176ce77e02fd70a168ad9c7"), "subject" : "Dogs eat cats", "content" : "Cats are not evil" }

    TextScore

    Each page that has the search phrase in the indexed fields receives a score from the $text operator. The score shows a document's relevancy to a specific text search query. The score can be specified as part of the sort() method definition as well as the projection expression. The $meta: "textScore" expression offers information about the $text operation's processing. For more information on retrieving the score for projection or sort, see the $meta MongoDB projection operator.

    We're doing a text search, thus we'd like to receive some statistics on how relevant the resulting documents are. To do this, we will use the $meta: "textScore" expression, which offers information about the $text operator's processing. Using the sort command, we will also sort the documents by textScore. A greater textScore indicates a better match.

    db.messages.find({$text: {$search: "dogs"}}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})

    This query returns the following documents:

    { "_id" : ObjectId("6176b68b750fd1447889f942"), "subject" : "Joe owns a dog", "content" : "Dogs are man's best friend", "likes" : 60, "year" : 2015, "language" : "english", "score" : 1.2916666666666665 } 
    
    { "_id" : ObjectId("6176b69f750fd1447889f943"), "subject" : "Dogs eat cats and dog eats pigeons too", "content" : "Cats are not evil", "likes" : 30, "year" : 2015, "language" : "english", "score" : 1 }

    How To Use Mongodb Text Search?
    As you can see, the first document gets a score of 1.2916666666666665 (since the keyword dog appears twice in its subject), whereas the second has a score of 1. The query also ordered the returned documents by their score in descending order.

    Compound Indexing:

    We will allow compound text indexing on the subject and content fields in our example. Proceed to run the following command in mongo shell:

    db.messages.createIndex({"subject":"text","content":"text"})

    How To Use Mongodb Text Search?
    This command will not work. Attempting to create a second text index will result in an error message stating that a full-text search index already exists. Why is this the case? The explanation is that text indexes are limited to one text index per collection. As a result, if you want to build another text index, you must delete the old one and establish a new one.

    db.messages.dropIndex("subject_text")  
    db.messages.createIndex({"subject":"text","content":"text"})

    After running the index creation queries listed above, try searching for all pages with the keyword cat.

    db.messages.find({$text: {$search: "cat"}}, {score: {$meta: "textScore"}}).sort({score:{$meta:"textScore"}})

    The above query will give the following output:

    { "_id" : ObjectId("6176b69f750fd1447889f943"), "subject" : "Dogs eat cats and dog eats pigeons too", "content" : "Cats are not evil", "likes" : 30, "year" : 2015, "language" : "english", "score" : 1.3333333333333335 }
    
    { "_id" : ObjectId("6176b6cb750fd1447889f944"), "subject" : "Cats eat rats", "content" : "Rats do not cook food", "likes" : 55, "year" : 2014, "language" : "english", "score" : 0.6666666666666666 }

    How To Use Mongodb Text Search?
    Indexing the Entire Document

    In the last example, we created a composite index on the subject and content fields. However, there may be times when you want any text content in your papers to be searchable.

    Consider storing emails in MongoDB documents, for example. In the case of emails, all fields must be searchable, including Sender, Recipient, Subject, and Body. In such cases, you can use the $** wildcard specifier to index all of your document's string fields.

    The query would be as follows (make sure you delete the existing index before establishing a new one):

    db.messages.createIndex({"$**":"text"})

    This query would create text indexes on any string fields in our documents.

    Implementing Text Search in an Aggregation Pipeline:

    Text search is supported in the aggregate pipeline via the $text query operator in the $match stage. 

    But the following regulations apply to text search in the aggregation pipeline:

    • The pipeline's initial stage must be the $match stage with a $text. 
    • In the stage, a $text operator can only appear once. 
    • The $text operator expression is not permitted in $or or $not expressions. 
    • By default, the text search does not return matching documents in the order of matching scores. Use the $meta aggregation expression in the $sort stage to sort by descending score.

    The $text operator assigns a text score to each document that contains the search word in the index field. The score shows the importance of a document in relation to a given text search query.

    Examples: 

    The following examples are based on a message collection with a text index on the field subject:

    >use people 
    > db.people.insert({"name":"Ishan","pet":"dog"}) 
    > db.people.insert({"name":"Abhresh","pet":"cat"}) 
    > db.people.insert({"name":"Madan","pet":"cat"}) 
    > db.people.insert({"name":"Sneha","pet":"dog"})
    
    >db.people.find().pretty()

    How To Use Mongodb Text Search?
    Count the number of the document in which the pet value is dog:

    db.people.aggregate([{$match:{$text:{$search:"dog"}}},{$group:{_id:null,total:{$sum:1}}}])

    How To Use Mongodb Text Search?
    Count the number of the document in which the pet value is Cat:

    db.people.aggregate([{$match:{$text:{$search:"dog"}}},{$group:{_id:null,total:{$sum:1}}}])

    How To Use Mongodb Text Search?

    Looking to enhance your coding skills? Join our Python Certificate Course Online and unlock endless possibilities in the world of programming. Start your journey today!

    Summary

    If you handle string content in MongoDB, you should use full-text search to make your searches more effective and accurate. In this article, we demonstrated how to conduct a basic full-text search on a sample dataset. 

    Full-text search has always been one of MongoDB's most requested capabilities. This article began with an introduction to full-text search before moving on to the fundamentals of generating text indexes. 

    Following that, we looked into compound indexing, wildcard indexing.  We also looked at some key concepts including analyzing text indexes, and text search in the aggregation pipeline. In the forthcoming MongoDB versions, we can expect some significant enhancements to this capability.

    Profile

    Abhresh Sugandhi

    Author

    Abhresh is specialized as a corporate trainer, He has a decade of experience in technical training blended with virtual webinars and instructor-led session created courses, tutorials, and articles for organizations. He is also the founder of Nikasio.com, which offers multiple services in technical training, project consulting, content development, etc.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon