Easy Tutorial
❮ Mongodb Create Collection Mongodb Objectid ❯

MongoDB Full-Text Search

Full-text search involves creating an index for each word, indicating the frequency and position of its occurrence in the article. When a user queries, the search program uses the pre-built index to find the results and returns them to the user.

This process is similar to looking up a word in a dictionary using the index.

MongoDB started supporting full-text search from version 2.4, and currently supports full-text indexing for 15 languages:


Enabling Full-Text Search

MongoDB enables full-text search by default from version 2.6 onwards. If you are using an earlier version, you need to enable it using the following code:

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

Or use the command:

mongod --setParameter textSearchEnabled=true

Creating a Full-Text Index

Consider the following document data in the posts collection, which includes article content (post_text) and tags:

{
   "post_text": "enjoy the mongodb articles on tutorialpro",
   "tags": [
      "mongodb",
      "tutorialpro"
   ]
}

We can create a full-text index on the post_text field to search within the article content:

>db.posts.ensureIndex({post_text:"text"})

Using the Full-Text Index

Now that we have a full-text index on post_text, we can search for the keyword "tutorialpro":

>db.posts.find({$text:{$search:"tutorialpro"}})

The following command returns the document containing the keyword "tutorialpro":

{ 
   "_id" : ObjectId("53493d14d852429c10000002"), 
   "post_text" : "enjoy the mongodb articles on tutorialpro", 
   "tags" : [ "mongodb", "tutorialpro" ]
}

If you are using an older version of MongoDB, you can use the following command:

>db.posts.runCommand("text",{search:"tutorialpro"})

Using a full-text index can improve search efficiency.


Deleting a Full-Text Index

To delete an existing full-text index, you can use the find command to find the index name:

>db.posts.getIndexes()

With the index name obtained from the above command, in this case, "post_text_text", execute the following command to delete the index:

>db.posts.dropIndex("post_text_text")
❮ Mongodb Create Collection Mongodb Objectid ❯