MongoDB: Getting Started with MongoDB Atlas Search

25 / Sep / 2024 by Ankit . 0 comments

Introduction

In this blog, we’ll discuss about the MongoDB Atlas Search and how we can query the database using Atlas Search.

What is Atlas Search?

Atlas Search is an embedded full-text search in MongoDB Atlas that gives you a seamless, scalable experience for building relevance-based app features. As we already know, Atlas Search is built on top of Apache Lucene, the world’s leading text search technology. It eliminates the need for a separate search engine like Elastic or Solr by embedding search capabilities into the MongoDB database.

In that sense, it simplifies the stack, merging database and search operations into one layer. MongoDB’s Atlas Search allows fine-grained text indexing and querying of data on your Atlas cluster.

Here are some of it’s top features:

  • Full-Text Search
  • Rich Query Syntax
  • Scalability
  • Faceting and Aggregations
  • Synonyms and Text Analysis
  • Relevance Scoring
  • Highlighting
  • Autocomplete
  • Integration with MongoDB
  • Security and Access Control

Prerequisites

To use MongoDB Atlas search, you’ll need:

  • Atlas Access: Permission to create and manage search indexes in the MongoDB Atlas dashboard.
  • MongoDB Atlas Cluster: An active cluster in MongoDB Atlas.
  • Collection: A collection in which you want to create a search index.

Read More: Efficient Data Migration from MongoDB to S3 using PySpark

Create a Search Index in MongoDB Atlas

  1. Log in to your MongoDB Atlas account and select the project and cluster.
  2. Create a New Search Index
    • Click on the “Create Search Index” button in the Atlas Search tab.
    • Configure the index name and the collection on which you want to create the search index.
    • Define the fields which you want to include and provide the field types as well (eg. string, number, etc.)
    • You can also configure the custom analyzers to fine-tune text analysis and search relevance. (Optional)
    • Define synonyms to improve search accuracy by mapping different terms to equivalent meanings. (Optional)
    • Review and Create the index.
  3. You can track the progress of the index creation in the MongoDB Atlas dashboard.

Full-Text Searching

In the MongoDB Atlas search, there are a few ways in which we can perform the text searching, These are the following scenarios that may need in searching in any applications:

  • Full-Text Search
  • Partial Search
  • Fuzzy Search

1. Full Text Search

We use the “string” as the field type for the full text searching on any field in a MongoDB collection. Here is an example of the mapping that we will need to define while creating the search index:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "title": {
        "type": "string"
      },
      "content": {
        "type": "string"
      }
    }
  }
}

So, in this mapping we are defining that the search index will have the two fields “title” and “context” and we will need only these two fields in the index that’s why we have the dynamic property value as “false”.

Dynamic property is set to “true” if we do not want to specify the limited fields in the index. It means that every field in the collections will be indexed in this case. This property is set to “true” if the schema changes regularly or is unknown. We use the “text” operator in the atlas search query for full-text searching. Here is an example of the search query on the collection and we will need to specify the “collectionName”, “indexName”, “searchTerm”, and fieldName.

/*This is the sample format of the atlas search query for full text search*/
db.collectionName.aggregate([
  {
    $search: {
      index: "indexName",
      text: {
        query: "searchTerm",
        path: "fieldName"
      }
    }
  }
])
/*Atlas search query with index "content_search_index", search term as "Best Book" on the "content" field.*/
db.testCollection.aggregate([
  {
    $search: {
      index: "content_search_index",
      text: {
        query: "Best Book",
        path: "content"
      }
    }
  }
])

This query will return the documents which have the exact term as “Best Book” in the “content” field. These are the some sample results of this query:

Output:

"This is the Best Book in the library"
"The best book in the series has the most unexpected plot twists."
"If you’re looking for inspiration, this is the best book to read."

 

2. Partial Search

We use “autocomplete” as the field type for the partial text searching on any field in a MongoDB collection. Here is the example of the mapping which we will need to define while creating the search index for the partial search:

{ 
  "mappings": { 
    "dynamic": false, 
    "fields": { 
      "title": { 
        "type": "autocomplete" 
      }, 
      "content": { 
        "type": "autocomplete" 
      }
    } 
  } 
}

So, in this mapping, we are defining that the search index will have the two fields “title” and “context” on which we need to perform the partial search. We use the “autocomplete” operator in the atlas search query for full-text searching.

Here is an example of the search query on the collection and we will need to specify the “collectionName”, “indexName”, “searchTerm”, and fieldName.

/*This is the sample format of the atlas search query for partial search.*/
db.collectionName.aggregate([
  {
    $search: {
      index: "indexName",
      autocomplete: {
        query: "searchTerm",
        path: "fieldName"
      }
    }
  }
])
/*Atlas search query with index "content_search_index", search term as "Best Book" on the "content" field.*/
db.testCollection.aggregate([
  {
    $search: {
      index: "content_search_index",
      autocomplete: {
        query: "Best Book",
        path: "content"
      }
    }
  }
])

This query will return the documents that have the term “Best Book” in the “content” field. These are some sample results of this query:

Output:

"This is the Best Book in the library"
"This is one of the best bakery in the town."
"I have received the best birthday gift ever."
"The best book in the series has the most unexpected plot twists."
"If you’re looking for inspiration, this is the best book to read.

Note: As we can see in the above output the best-matched results are not always on the top in the filtered list (results 4 and 5). This is a limitation of the “autocomplete” field type in the Atlas Search. To fix this we can a new approach as follows:

  • Improvement in the “autocomplete” field type (Best matched results at the top):

If we want to get the best matched results at the top followed by the partial matched results. Then we need to use both field types “string” and “autocomplete” for a single field. For example, if we want to get the best results at the top for the “content” field, here is the required index mapping for this scenario:

{ 
  "mappings": { 
    "dynamic": false, 
    "fields": { 
      "title": { 
        "type": "string" 
      }, 
      "content": [
        {
          "type": "string"
        },
        {
          "type": "autocomplete"
        }
      ]
    } 
  } 
}

So, in this mapping, we are defining the multiple field types (string and autocomplete) for the “content” field. This is to ensure that we get the best matched results at the top followed by the partial matched results. Now, if we use the same atlas search query with the “autocomplete” field type, then the results would be different:

/*Atlas search query with index "content_search_index", search term as "Best Book" on the "content" field.*/

db.testCollection.aggregate([ 
  { 
    $search: { 
      index: "content_search_index", 
      autocomplete: { 
        query: "Best Book", 
        path: "content" 
      }
    } 
  } 
])

This query will return the documents which have the exact term as “Best Book” in the “content” field at the top followed by the partial matching results. These are the sample results of this query:

Output:

"This is the Best Book in the library"
"The best book in the series has the most unexpected plot twists." 
"If you’re looking for inspiration, this is the best book to read.
"This is one of the best bakery in the town."
"I have received the best birthday gift ever."

 

3. Fuzzy Search:

We use the “fuzzy” operator in the atlas search queries for the fuzzy searching on any field in a MongoDB collection. We can use the same mapping for the fuzzy search as we used for the full-text searching:

{ 
  "mappings": {
    "dynamic": false, 
    "fields": { 
      "title": { 
        "type": "string" 
      }, 
      "content": { 
        "type": "string" 
      }
    } 
  } 
}

Here is the sample query for the fuzzy search on a particular field:

db.testCollection.aggregate([
  {
    $search: {
      index: "content_search_index",
      text: {
        query: "Bast Bokk", // Intentionally misspelled term
        path: "content",
        fuzzy: {
          maxEdits: 2 // Allows up to 2 single-character
        }
      }
    }
  }
])

In the above query, we have provided the “Bast Bokk” as the search term intentionally. This will return the results if the search term which is misspelled up to 2 characters. Here are the results returned by this query:

Output:

"This is the Best Book in the library" 
"The best book in the series has the most unexpected plot twists." 
"If you’re looking for inspiration, this is the best book to read.

 

Conclusion

This blog is best for those who are just curious to learn new technologies or just want to implement the Atlas Search in their projects. For more information, you can also refer to the official MongoDB documentation on Atlas Search.

 

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *