Full-Text Search in MongoDB

Foridul Islam
5 min readAug 14, 2022

--

A guide to performing a full-text search in MongoDB

MongoDB queries to filter data by searching for exact matches, using greater-than or less-than comparisons, or using regular expressions will work well enough in many situations.

However, Full-text search refers to searching some text inside extensive text data and returning results that contain some or all of the words from the query. In contrast, the traditional search would return exact matches.

Types of text search in MongoDB

There are basically 2 types of text search in MongoDB.

(i) String/Regex search

(ii) Full-text search

(i) String search

The string search is a way of searching text in MongoDB where it tries to find the consecutive characters of the text. In this way, it tries to match the character by character that’s why it’s performed relatively slower.

There is another way for text search is using regular expressions. The regular expressions represent a search pattern and try to match that pattern.

Example

Before executing the MongoDB query, we have to insert some data.

Consider the following data in a collection name Fruits.

db.Fruits.insert({ _id : "1", Name: "Apple", Detail: "An apple a day keeps a doctor away" });db.Fruits.insert({ _id : "2", Banana: "Apple", Detail: "A yellow fruit" });db.Fruits.insert({ _id : "3", Name: "Mango", Detail: "Mangoes are very sweet fruits" });db.Fruits.insert({ _id : "4", Name: "Cherry", Detail: "A cherry is the fruit of many plants of the genus Prunus" });db.Fruits.insert({ _id : "5", Name: "Strawberry", Detail: "The strawberry is a widely grown hybrid species of the genus Fragaria" });db.Fruits.insert({ _id : "6", Name: "Tomato", Detail: "A Green fruit" });

Now, if we want to find the fruits with the text fruit,

The query is similar to using Like in SQL query.

SELECT * FROM Fruit WHERE Detail LIKE "%fruit%";

In MongoDB, we can use the $regex operator to perform the text search.

The query is as follows,

db.Fruits.find({"Detail": {"$regex": /fruit/i }});

The results are as follows,

This type of search is easy to use and good for small data sets. When the dataset increase, this would be a problem for performance. To overcome the issues we can use a full-text search.

(ii) Full-text search

Full-text search is a process to search large amounts of text. In this process, the search engine will use a full-text search to look for keywords in all the data fields that it indexed. The main key to this technique is indexing.

Create an Index for full-text search

The key to an efficient full-text search is index creation. Essentially, the index creation process goes through each text field of a dataset.

For each word, it will start by removing any diacritics (marks placed above or below letters, such as é, à, and ç in French). Then, based on the used language, the algorithms will remove filler words and only keep the stem of the terms. This way, “to eat,” “eating,” and “ate” are all classified as the same “eat” keyword. It then changes the casing to use only either uppercase or lowercase. The exact indexing process is determined by the analyzer that is used.

To create an index in the collection Fruits for the fields of Name and Detail, run the following command,

db.Fruits.createIndex( { Name: "text", Detail: "text" } )

The output will be as followings,

Perform a full-text search in MongoDB

To find the fruits where Detail has the word “yellow fruit”, run the following command,

db.Fruits.find( { $text: { $search: “yellow fruit” } } )

The results will be as follows,

We noticed that, In the Detail field, there has one of the words “yellow” or “fruit” or both.

Sort the full-text search results

MongoDB will return its results in unsorted order by default. However, text search queries will compute a relevance score for each document that specifies how well a document matches the query.

To sort the results in order of relevance score, you must explicitly project the $meta textScore field and sort on it:

db.Fruits.find(
{ $text: { $search: "yellow fruit" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )

The results will be as follows,

{ “_id” : “2”, “Banana” : “Apple”, “Detail” : “A yellow fruit”, “score” : 1.5 }
{ “_id” : “6”, “Name” : “Tomato”, “Detail” : “A Green fruit”, “score” : 0.75 }
{ “_id” : “3”, “Name” : “Mango”, “Detail” : “Mangoes are very sweet fruits”, “score” : 0.6666666666666666 }
{ “_id” : “4”, “Name” : “Cherry”, “Detail” : “A cherry is the fruit of many plants of the genus Prunus”, “score” : 0.5833333333333334 }

We noticed that the results are now ordered according to the score. The text score signifies how well the document matched the search term or terms.

Summary

To perform a full-text search in MongoDB, you must have to add an index for the field you want to search text. Also, it will optimize query performance.

Happy searching!!

--

--

Foridul Islam
Foridul Islam

Written by Foridul Islam

Sr. Software Engineer at SELISE Digital Platforms

No responses yet