MongoDB

MongoDB Full Text Search Tutorial

Hello, in this tutorial we are going to explore the full-text search functionalities of MongoDB right from fundamentals. MongoDB uses text indexes to perform the different full-text search operations and is case sensitive.

1. Introduction

MongoDB, one of the leading NoSQL databases, is well known for its fast performance, flexible schema, scalability and great indexing capabilities. At the core of this fast performance lie the MongoDB indexes, which support efficient execution of queries by avoiding full-collection scans and hence limiting the number of documents that MongoDB searches.

Starting from version 2.4, MongoDB began with an experimental feature supporting Full-Text Search using Text Indexes. But now, this feature has become an integral part of the product.

1.1 The Basics

Full-text search refers to the technique of searching a full-text database against the search criteria specified by the user. It is something similar to how we search any content in any search application i.e. by entering certain string keywords/phrases and getting back the relevant results sorted by their ranking.

The Text Search uses stemming techniques to look for specified words in the string fields by dropping stop words like a, an, the, etc.

It has features such as:

  • Full-text search as an index type when creating new indexes, just like any other
  • Support for advanced queries, similar to the Google search syntax e.g. negation and phrase matching
  • Indexing of multiple fields, with weighting to give different fields higher priority
  • Supports 15 different languages

1.2 MongoDB Search Syntax

In MongoDB, $text performs a search on the content of the indexed field with a text index. In below example, we will create an index on the subject field of the document. A $text expression has the following syntax:

Basic Syntax

{
  $text:
    {
      $search: ,
      $language: ,
      $caseSensitive: ,
      $diacriticSensitive: 
    }
}

But before we move on, let’s take a look at the search fields that are supported in MongoDB.

1.3 MongoDB Search Fields

MethodDescription
$searchMandatoryA string of search terms which MongoDB parses and uses to perform query the text index. MongoDB performs a logical OR search of the terms unless specified as a phrase.
$languageOptionalThe language that determines the list of stop words for the search and the rules for the stemmer and tokenizer. $text operator supports multiple languages, few of them are Dutch, French, German, Spanish, Arabic etc.
$caseSensitiveOptionalA boolean flag to enable or disable case sensitive search. Defaults to false.
$diacriticSensitiveOptionalA boolean flag to enable or disable diacritic sensitive search against version 3 text indexes.

2. MongoDB Full-Text Search Tutorial

To demonstrate this example, I have inserted few records into articles collection, which will help you to understand the different full-text search scenarios.

2.1 Enabling Text Search

Initially, Text Search was an experimental feature but starting from version 2.4, this configuration is enabled by default. But if you are using the previous version of MongoDB, the following code shows how this can be done:

Enable Text Search Command

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

2.2 Creating Text Index

Consider the following document under the articles collection containing the id and subject fields,

Fig. 1 MongoDB Collection
Fig. 1 MongoDB Collection

We will create a text index on subject field so that we can we can perform a search operation.

Creating Text Index Command

> db.articles.ensureIndex({subject:"text"});

If the command is executed successfully, the following output will be shown:

Fig. 2: Creating Text Index Command Output
Fig. 2: Creating Text Index Command Output

2.3 Using Text Index

Now that we have created a text index on subject field, we will do the searching operation based on the different search criteria’s.

2.3.1 $search: “coffee”

This query returns the documents that contain the term coffee in the indexed subject field.

Query 1

> db.articles.find({$text: {$search: "coffee" }});

If the command is executed successfully, the following output will be shown:

Fig. 3: Query 1 Output
Fig. 3: Query 1 Output

2.3.2 $search: “bake coffee cake”

This query returns documents that contain either bake or coffee or cake in the indexed subject field, or more precisely, the stemmed version of these words (e.g. bake, baking, baked).

Query 2

> db.articles.find({ $text: {$search: "bake coffee cake"}});

If the command is executed successfully, the following output will be shown:

Fig. 4: Query 2 Output
Fig. 4: Query 2 Output

2.3.3 $search: “\”coffee shop\””

This query returns documents that contain the phrase coffee shop.

Query 3

> db.articles.find({ $text: {$search: "\"coffee shop\"" }});

If the command is executed successfully, the following output will be shown:

Fig. 5: Query 3 Output
Fig. 5: Query 3 Output

2.3.4 $search: “coffee -shop”

This query returns documents that contain the phrase coffee -shop.

Query 4

> db.articles.find({ $text: {$search: "coffee -shop"}});

If the command is executed successfully, the following output will be shown:

Fig. 6: Query 4 Output
Fig. 6: Query 4 Output

2.3.5 Case Sensitive Search – $search: “Coffee”

This query performs a case sensitive search for the term Coffee. It may impact your search performance if $caseSensitive : true.

Query 5

> db.articles.find({ $text: {$search: "Coffee", $caseSensitive: true}});

If the command is executed successfully, the following output will be shown:

Fig. 7: Query 5 Output
Fig. 7: Query 5 Output

2.3.6 Diacritic Sensitive Search – $search: “CAFÉ”

This query performs a diacritic sensitive text search on the term CAFÉ.

Query 6

> db.articles.find({$text:{ $search: "CAFÉ", $diacriticSensitive: true }});

If the command is executed successfully, the following output will be shown:

Fig. 8: Query 6 Output
Fig. 8: Query 6 Output

2.4 Deleting Text Index

To delete an existing text index, first find the name of the index using the below query.

Finding Index Name

> db.articles.getIndexes();

If the command is executed successfully, the following output will be shown:

Fig. 9: Index Name
Fig. 9: Index Name

After getting the name of the index from the above query, run the below command. Here, subject_text is the name of the index.

Deleting Index

> db.articles.dropIndex("subject_text");

If the command is executed successfully, the following output will be shown:

Fig. 10: Deleting Index
Fig. 10: Deleting Index

3. Conclusion

Simple, right? The new text index features provide a simple, fully consistent way to do a basic search without deploying any extra services and it highly improves the search efficiency as compared to normal search.

4. Download the MongoDB Files

This was an example of MongoDB Full-Text Search. Run the script and the result will be printed in the console window.

Download
You can download the full source code of this example here: MongoDB Shell – Executed Commands

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button