MongoDB Text Search – Indexing Single & Multiple Fields

Job-ready Online Courses: Knowledge Awaits – Click to Access!

In our last tutorial, we had learned about Executing MongoDB in Java Program. Now, we will be looking at MongoDB Text Search.
In this article, we will study how to index mongoDB text search partial, multiple fields, and entire document. Along with this, we will learn the different languages supported by the text search.

MongoDB Text Search

We can perform MongoDB text search of string content with the help of query operations. $text operator is used to perform this kind of text search in MongoDB.

To understand this topic in a better manner we will be using an example of a collection named as examples with the following structure.
{
"subject":"Joe", 
"content":"best friend", 
"likes": 60, 
"year":2015, 
"language":"english"
}

Let’s add some sample documents in the collection.

db.examples.insert({"subject":"Joe", "content":"best friend", "likes": 60, "year":2015, "language":"english"})

db.examples.insert({"subject":"Dogs", "content":"Cats", "likes": 30, "year":2015, "language":"english"})

db.examples.insert({"subject":"Cats", "content":"Rats", "likes": 55, "year":2014, "language":"english"})

db.examples.insert({"subject":"Rats", "content":"Joe", "likes": 75, "year":2014, "language":"english"})

i. Indexing a Single Field

Here, we will create a text index on the subject field of our document using the query given below:
db.examples.createIndex({"subject":"text"})

Now, if you want to test this newly created text index on the subject field, we will have to find documents using $text operator. We will look for all documents that have keyword dog in their subject field.

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

Here, we are using {$meta: “textScore” } to get some statistics about how relevant our resultant document is. After that, we will sort all documents by their textScore using sort command. Higher the textScore, more relevant the match is.

The output for the following code will be like this:
{ “_id” : ObjectId(“55f4a5d9b592880356441e94”), “subject” : “Dogs”, “content” : “Cats”, “likes” : 30, “year” : 2015, “language” : “english”, “score” : 0.66 }
Here the document is having a score as 0.66 as the word dog appeared only once.

ii. Indexing Multiple Fields

In our real-time scenarios, we will have to use MongoDB text search on multiple fields of a document. In our example to understand this, we will enable compound text indexing on the subject and content fields.
But before doing this we will have to drop the existing text index as it can take only one text index per collection.
db.examples.dropIndex("subject_text") 
db.examples.createIndex({"subject":"text","content":"text"})

After writing this query now write another query to find out all documents with keyword cat.

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

This query will give us the following output:

{ “_id” : ObjectId(“55f4af22b592880356441ea4”), “subject” : “Dogs”, “content” : “Cats”, “likes” : 30, “year” : 2015, “language” : “english”, “score” : 0.6666 }
{ “_id” : ObjectId(“55f4af22b592880356441ea5”), “subject” : “Cats”, “content” : “Rats”, “likes” : 55, “year” : 2014, “language” : “english”, “score” : 0.6666 }

iii. Indexing the Entire Document

Suppose, if we want to do indexing for all mails stored in MongoDB. For that, we will have to take into consideration sender, recipient, subject and body also using $** wildcard specifier.
Now we will look at the example to understand it better:
db.messages.createIndex({"$**":"text"})

This query will set up text indexes on any string fields in our documents. For this insert a new document with a new field location in it.

db.examples.insert({"subject":"Birds", "content":"Rats", "likes": 12, "year":2013, location: "Switzerland", "language":"english"})

Now if we try to search with Switzerland keyword then it will show up the current document we inserted.

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

iv. Languages Supported by Text Search by MongoDB

A new version is introduced by MongoDB, version 2 for the text search features. Version 1 only supports the long form of each language name. But in version, we can use two-letter language codes defined in ISO 639-1.

Language NameTwo-Letter Code
Danish           da
English           en
Dutch           nl
Finnish           fi
French           fr
German           de
Hungarian           hu
Italian           it
Norwegian           nb
Portuguese           pt
Romanian           ro
Russian           ru
Spanish           es
Swedish           sv
Turkish           tr

So, this was all about MongoDB Text Search Tutorial. Hope, you like our explanation.

Summary

Hence, we have studied about text search in MongoDB with different scenarios to be searched from an example.
In addition, we had discussed all the supported languages by MongoDB Text Search with ISO 639-1 code. Furthermore, if you have any query, feel free to share with us. Surely, we will get back to you.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses

DataFlair Team

The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.

1 Response

  1. Hein says:

    Typo toextScore

Leave a Reply

Your email address will not be published. Required fields are marked *