Using Relevance-Based Search and Search Indexes in MongoDB
Last Updated :
09 Apr, 2024
In MongoDB, mastering its relevance-based 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:
Collection CreatedStep 2: Create a Text Index on the content field.
db.articles.createIndex({ content: "text" });
Output:
context_text index CreatedSearching 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" textExample 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" textExplanation: 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 resultsExplanation: 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.
Similar Reads
Grouping Search Results Using Facets in MongoDB
Faceted search in MongoDB organizes data based on multiple attributes, like categories or tags, using the aggregation framework. This technique enhances data navigation and analysis. To implement faceted search, access MongoDB Cloud, have basic MongoDB query knowledge, and use MongoDB Compass for vi
4 min read
Using $search and Compound Operators in MongoDB
In MongoDB, advanced querying techniques allow users to efficiently manage and retrieve data. The $search operator is a powerful tool for performing text searches within collections while compound operators like $and, $or, and $nor enable the creation of complex queries by combining multiple conditi
5 min read
MongoDB Schema Design Best Practices and Techniques
MongoDBâs flexible, document-based schema design provides significant advantages in managing complex, dynamic data models. Unlike traditional relational databases, MongoDB doesnât enforce rigid schemas, enabling seamless evolution of our data over time. In this article, we will explain MongoDB schem
6 min read
How to Create Indexes in MongoDB using Node.js?
MongoDB, a popular NoSQL database, provides powerful indexing capabilities to improve query performance. Indexes in MongoDB help in quickly locating documents and speeding up read operations. In this tutorial, we'll explore how to create indexes in MongoDB using Node.js. What is an Index in MongoDB?
3 min read
Relevance Scoring and Search Relevance in Elasticsearch
Elasticsearch is a powerful search engine that good at full-text search among other types of queries. One of its key features is the ability to rank search results based on relevance. Relevance scoring determines how well a document matches a given search query and ensures that the most relevant res
6 min read
How to do a Full-Text Search in MongoDB using Mongoose
In MongoDB, performing a full-text search allows us to query and retrieve documents based on textual content matching certain criteria. When using Mongoose, an ODM (Object Data Modeling) library for MongoDB in Node.js, conducting full-text search operations can be efficiently achieved using the Mong
5 min read
How to Perform Text Search in MongoDB using Node.js?
MongoDB is an open-source, cross-platform, No-SQL database that stores data in documents, which contain data in the form of key-value pairs. In this article, we will learn about how to perform text-based searches in MongoDB using node.js. Prerequisites Node.jsMongoDBMongoDB Atlas Connect with Applic
5 min read
Single Field Indexes In MongoDB
In MongoDB, indexes play a crucial role in improving query performance by efficiently locating and retrieving documents from collections. One type of index commonly used is the single-field index, which indexes a single field within a collection. In this article, we will learn about the concept of s
5 min read
Mongoose Schemas and Indexes
Mongoose is a powerful Object Data Modeling (ODM) library for MongoDB in a Node.js environment. It provides a straightforward way to interact with MongoDB, including features like schema definition, model creation, and database query handling. One key feature of Mongoose is its ability to create and
5 min read
How to Integrate MongoDB Atlas and Segment using MongoDB Stitch
Data integration has become a crucial component of modern business strategies, allowing companies to enhance the power of data for informed decision-making. According to a recent survey, 85% of businesses believe that integrating data across multiple platforms is important for their success. In this
5 min read