Mongodb Full Text Search

MongoDB Full Text Search

MondodDB full text search is used to search text content. Text Search uses Text Index and $text operator to perform its operation.

Why we need Full Text Search

First, we need to know why and when we need Full Text Search options.

For example- when we try to search something on google, then we enter keywords/ phrases. Google returns relevant results sorted by their score/ rank. Another example- when we enter a keywords/phrases on wiki, then wiki search engine shows the related article.

So, when we will plan to develop an app and we want to integrate this type of searching functionality, then we need MongoDB Text Search.

How Full Text Search works

MongoDB uses stemming techniques to search for specified words in the string content. So, how stemming technique works? There are two main concept to understand stemming technique in mongoDB full text search. These are Stop word and Stemming.

Stop word- stop words are used to filter words that are irrelevant for searching.

Stemming- stemming is the process to reduce inflectional forms to common base form.

For example to understand the whole things –

Text –          “the boy’s cars are different colors”

Screen Shot 2017-10-06 at 3.17.04 PM

In summary, the main goal of this technique is to reduce inflectional forms and sometimes derivationally related forms of a word to a common base form.

like as bellow:

am, are, is => be

car, cars, car’s, cars => car

So, the result of this mapping of our example will be something like :

“the boy car be differ color”

Note: this above process is language dependent. About supported languages read here.

How Implement it

First, we will create a collection stores with our example document.

db.stores.insert(
  [
    { _id: 1, description: "the boy's cars are different colors" }
  ]
)

Here we will see, how to implement it and to understand the basic concept of Text Searching technique.

Then we create a Text index to run the following command

db.stores.createIndex( { description: "text" } )

To, perform a search query, here we should use $text operator. To verify our understanding , we will search a word “differ” to our stores collection and see what we will get-

db.stores.find( { $text: { $search: "differ" } } )

Output:

[{
  "_id" : 1,
  "description" : "the boy's cars are different colors"
}]

Yah, here we get our expected output.

if we search “the boy car be”

db.stores.find( { $text: { $search: "the boy car be" } } )

here we will get the same output.

[{
  "_id" : 1,
  "description" : "the boy's cars are different colors"
}]

 

More detail documentation with example we will get from here

You May Also Like

About the Author: Md. Delwar Hossain

He has 11 years of experience in developing standalone software and web applications for multiple database platforms. He has been passionate about new tools and technologies. He is positive and trustworthy. He is capable to learn and adapt quickly to different situations. He is a great team player and enjoys leading and mentoring. He is specialized in architecting and building complex web and mobile application. He has strong skills to automate POS, inventory, supply chain, trading export/ import, human resource management, manufacturing and production, distribution management system and hospital management system.

Leave a Reply

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