Open In App

Using Relevance-Based Search and Search Indexes in MongoDB

Last Updated : 09 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In MongoDB, mastering its relevancebased search capabilities can significantly enhance user experiences across diverse applications. MongoDB’s good in this area is present in its text indexes, which are good at quickly and accurately retrieving text data based on search queries.

In this article we will learn about their Prerequisites, Understanding the Relevance-Based Search with Text Indexes, and How to use Relevance-Based Search with the help of examples in detail.

Prerequisites

  • MongoDB: Before understanding MongoDB’s relevance-based search, It’s beneficial to have a solid understanding of MongoDB.

Understanding the Relevance-Based Search with Text Indexes

MongoDB’s relevance-based search functionality allows us to implement efficient text search features. This functionality revolves around text indexes, advanced data structures that enable the quick and accurate retrieval of text data based on search queries. Let’s understand in more brief manner.

1. Text Index Creation

  • Text index creation is the initial step to enable relevance-based search in MongoDB.
  • We can create text indices on fields containing text data using the $text operator.
  • Text indices break down the text data into individual words and their variations.
  • The process used to enhances the search ability by allowing MongoDB to efficiently match search queries with indexed text data.

Syntax (creating text index):

db.collectionName.createIndex({ fieldName: "text" });

2. Querying with Text Search

  • Once the text index created, we can perform text searches using the $text operator in conjunction with the $search parameter.
  • MongoDB utilizes these text indexes to quickly identify relevant documents based on the search query.

Syntax (Search Query):

db.collectionName.find({ $text: { $search: "text to search" } });

3. Relevance Score

  • An important aspect of MongoDB’s relevance-based search is its scoring mechanism.
  • MongoDB assigns a relevance score to each document based on factors such as keyword frequency, proximity and other relevancy parameters.
  • The scoring mechanism ensures that the most relevant results are provided, overall enhancing the search experience.
  • By using the below command, we can check the relevance score for any search query.

Syntax (Search Query with Score):

db.collectionName.find({ $text: { $search: "text to search" } }, { score: { $meta: "textScore" } });

How to use Relevance-Based Search?

Let’s setup our database to use the relevance based search feature in MongoDB.

Database Setup

Step 1: Insert some documents into our collection called articles.

db.articles.insertMany([
{ "_id": 1, "name": "Hritik", "content": "He writes about Web Tech" },
{ "_id": 3, "name": "Suman", "content": "He is a Data scientist" },
{ "_id": 8, "name": "Garry", "content": "She is a frontend engineer" },
{ "_id": 5, "name": "Ryan", "content": "He writes about Web Tech and AI" },
{ "_id": 7, "name": "Damon", "content": "He don't like to do anything" },
{ "_id": 2, "name": "Nik", "content": "He is a Software Engineer" },
{ "_id": 4, "name": "Sara", "content": "She write about UI/UX" }
]);

Output:

CollectionCreated

Collection Created

Step 2: Create a Text Index on the content field.

db.articles.createIndex({ content: "text" });

Output:

contextIndex

context_text index Created

Searching For Documents

Example 1: Let’s try to find the documents, which contain the “web” text. To solve this, we can use the following query

db.articles.find({$text: {$search: "web"}}, { score: { $meta: "textScore" } });

Output:

Screenshot-2024-04-08-at-33655-AM-min

Relevance-based search to search the”web” text

Example 2: Find the document that contain the word “write”. To accomplish this, we can use the following command

db.articles.find({$text: {$search: "write"}}, { score: { $meta: "textScore" } });

Output:

Screenshot-2024-04-08-at-34911-AM-min

Relevance-based search to search the “write” text

Explanation: Here, we can see that the results are not sorted by the relevance score. The reason behind this is when we executed the text search query using $text operator, MongoDB retrieves the documents that match the search criteria and then calculated the relevance scores for each matching document.

Example 3: Suppose we need to retrieve documents from the “articles” collection where the text index includes the word “he” in the “content” field.

db.articles.find({$text: {$search: "he"}}, { score: { $meta: "textScore" } })

Output:

Screenshot-2024-04-08-at-40224-AM

Finding Stop Words and got no results

Explanation: After executing the above query we did not get any results, the reason behind this is MongoDB’s text search excludes common stop words by default. Here”he” is considered as a stop word in our text index, that’s why it won’t be included in the search.

Conclusion

Overall, MongoDB’s relevance-based search and search indexes offers various possibilities for us, which help to boost search capabilities within our applications. Understanding how text indexing works, along with implement relevant search operators for querying and index settings to design search functionalities that effectively meet our application’s requirements.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads