Open In App

Using Relevance-Based Search and Search Indexes in MongoDB

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

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

Syntax (creating text index):



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

2. Querying with Text Search

Syntax (Search Query):

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

3. Relevance Score

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:

Collection Created

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

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

Output:

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:

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:

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:

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.


Article Tags :