Web Programming TutorialsLearn about the Concept of Text Search in MongoDB

Learn about the Concept of Text Search in MongoDB

Text Search

Inn this article, we are going to discuss the concept of text search in the MongoDB. The MongoDB 2.4+ version onward, it supports the text indexes which facilitates searching within the string content. MongoDB’s text search makes use of the stemming techniques in order to look for the definite words within the string fields by dropping stemming stop words such as a, an, the, etc. Currently, the MongoDB supports text search features for 15 languages.

How to Enable Text Search?
MongoDB’s Text Search was just an experiment in version 2.4 but from MongoDB version 2.6 onward the Text Search configuration is enabled by default. Therefore, if you are using MongoDB versions that are less than 2.6, the Text Search needs to be enabled before we can use this feature of the MongoDB. The following is the syntax to enable Text Search in MongoDB (if not enabled by default).

> db.adminCommand({setParameter:true,textSearchEnabled:true})

How to create the Text Index?
Consider the following document structure that stores book details and has tags. The document stores book title, author, publish year and status of the book along with associated tags.

> db.book.save({
...    "book_title": "Learn MongoDB in a Week",
...    "author" : "Aparajita Jain",
...    "status" : "active",
...    "publish_year": "2016",
...    "tags": [
...       "mongodb",
...       "eduonix",
...       "2016"
...    ]
... })
WriteResult({ "nInserted" : 1 })
> db.book.save({
...    "book_title": "Learn PHP7 in a Month",
...    "author" : "Aparajita Jain",
...    "status" : "active",
...    "publish_year": "2016",
...    "tags": [
...       "PHP7",
...       "eduonix",
...       "2016"
...    ]
... })
WriteResult({ "nInserted" : 1 })
> db.book.save({
...    "book_title": "Learn Node.js in Two Weeks",
...    "author" : "Aparajita Jain",
...    "status" : "active",
...    "publish_year": "2016",
...    "tags": [
...       "Node.js",
...       "eduonix",
...       "2016"
...    ]
... })
WriteResult({ "nInserted" : 1 })
> db.book.save({
...    "book_title": "Learn Software Testing in Two Months",
...    "author" : "Aparajita Jain",
...    "status" : "active",
...    "publish_year": "2016",
...    "tags": [
...       "Node.js",
...       "eduonix",
...       "2016"
...    ]
... })
WriteResult({ "nInserted" : 1 })
> db.book.find()
{ "_id" : ObjectId("59334255523476d644344dbd"), "book_title" : "Learn MongoDB in a Week", "author" : "Aparajita Jain", "status" : "active", "publish_year" : "2016", "tags" : [ "mongodb", "eduonix", "2016" ] }
{ "_id" : ObjectId("59334260523476d644344dbe"), "book_title" : "Learn PHP7 in a Month", "author" : "Aparajita Jain", "status" : "active", "publish_year" : "2016", "tags" : [ "PHP7", "eduonix", "2016" ] }
{ "_id" : ObjectId("5933426e523476d644344dbf"), "book_title" : "Learn Node.js in Two Weeks", "author" : "Aparajita Jain", "status" : "active", "publish_year" : "2016", "tags" : [ "Node.js", "eduonix", "2016" ] }
{ "_id" : ObjectId("593342d6523476d644344dc0"), "book_title" : "Learn Software Testing in Two Months", "author" : "Aparajita Jain", "status" : "active", "publish_year" : "2016", "tags" : [ "Node.js", "eduonix", "2016" ] }
>

Now, we are going to create a text index on book_title field so that we can search inside our book titles for a given document by using the following index.

> db.book.ensureIndex({book_title : "text"})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
>

How to use Text Index?
Now, we are going to search for all the books which have the word “Learn” in their text of book title by using the following query in the MongoDB.

> db.book.find({$text:{$search : "Learn"}})
{ "_id" : ObjectId("5933426e523476d644344dbf"), "book_title" : "Learn Node.js in Two Weeks", "author" : "Aparajita Jain", "status" : "active", "publish_year" : "2016", "tags" : [ "Node.js", "eduonix", "2016" ] }
{ "_id" : ObjectId("593342d6523476d644344dc0"), "book_title" : "Learn Software Testing in Two Months", "author" : "Aparajita Jain", "status" : "active", "publish_year" : "2016", "tags" : [ "Node.js", "eduonix", "2016" ] }
{ "_id" : ObjectId("59334255523476d644344dbd"), "book_title" : "Learn MongoDB in a Week", "author" : "Aparajita Jain", "status" : "active", "publish_year" : "2016", "tags" : [ "mongodb", "eduonix", "2016" ] }
{ "_id" : ObjectId("59334260523476d644344dbe"), "book_title" : "Learn PHP7 in a Month", "author" : "Aparajita Jain", "status" : "active", "publish_year" : "2016", "tags" : [ "PHP7", "eduonix", "2016" ] }
>

In the output of above MongoDB query, we can observe that all the 4 book titles which has the text as “Learn” have returned by the text search query.

How to delete Text Index?
We can delete an existing text index by searching the index name and using the “dropIndex” method as shown below.

> db.book.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.book"
        },
        {
                "v" : 2,
                "key" : {
                        "_fts" : "text",
                        "_ftsx" : 1
                },
                "name" : "book_title_text",
                "ns" : "test.book",
                "weights" : {
                        "book_title" : 1
                },
                "default_language" : "english",
                "language_override" : "language",
                "textIndexVersion" : 3
        }
]
>

By using the above query, we can observe that the index name is “book_title_text”. Now, we can use the “dropIndex” method in the following query to delete this index.

> db.book.dropIndex("book_title_text")
{ "nIndexesWas" : 2, "ok" : 1 }
>

Conclusion: –
In this article, we discussed about the concept of text search in the MongoDB along with the suitable examples.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -