Learn about the Concept of Text Search in MongoDB

0
2349
Text Search

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).

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.

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.

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.

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.

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.

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