Open In App

Creating a Search Index with Static Field Mapping in MongoDB

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Atlas Search Index in MongoDB Atlas is a critical feature for efficient search operations. It organizes data into a searchable format, enhancing search speed and document retrieval. MongoDB Atlas supports both dynamic and static mapping for search indexes, with static mapping offering granular control over indexed fields.

In this article, We will learn about Atlas Search Index, Static Mapping, the Need of Static Field Mapping for Search Indexes also How to create a Search Index with Static Mapping, and so on.

What is Atlas Search Index?

  • The Atlas Search Index in MongoDB is a data structure that categorizes data into an easily searchable format to minimize search time. It maps the terms and the document containing those terms. It enables faster retrieval of documents. Search Indexes are used to define how relevance-based search should be performed.
  • Atlas Search index can be created on a single field or multiple fields. It is recommended to index the fields that are used regularly to sort or filter our data to retrieve the documents containing the relevant data at query time quickly.
  • MongoDB Atlas supports static and dynamic mapping.

The default field mapping of Atlas Search Index is dynamic mapping and indexing all the fields in the collection. it is the fastest and easiest way to get search running, but it comes with some drawbacks:

  • MongoDB Atlas Search needs to determine the data type of each field in the collection during indexing and querying, which can add extra processing time.
  • Dynamic mapping indexes all fields by default, regardless of whether they’re relevant for searching, taking more disk space and might impact cluster performance.
  • Providing less control over data type validation

If we have data with a lot of fields, only some of which the end user of an application might care about, we can show the relevant results by static mapping certain fields.

What is Static Mapping?

  • Static Mapping in Atlas search allows us to explicitly define which fields to index, and also determine the type of analysis to be applied to each field and it optimizes the performance and provides advanced search requirements.
  • Static indexing on a specific field makes the search quicker and more efficient by minimizing the number of fields to be indexed.
  • Static mapping helps validate the document structure, improving data quality and consistency.
  • Static mapping involves explicitly defining which fields to index and how they should be analyzed for search operations.

Why Use Static Field Mapping for Search Indexes?

  • Improved Relevancy: By choosing which fields to index and how they are analyzed, we can do specific the search experience to our specific data and user needs. This ensures searches return the most relevant results, focusing on the information that truly matters.
  • Enhanced Performance: Static mapping allows us to optimize the size and structure of our search index. We can exclude unnecessary data, like large descriptions or repetitive fields. This leads to faster search times and a more efficient use of resources.
  • Granular Control: Static mapping empowers us to define exactly how each field is handled during searches. This includes things like stemming (reducing words to their root form) or synonym handling. This fine-grained control lets us to create a search experience that perfectly aligns with our data and avoids irrelevant matches.
  • Cost Optimization: In cloud-based environments like MongoDB Atlas, storage and processing power can impact costs. By keeping our search index focused on relevant data through static mapping, we can potentially reduce storage requirements and improve search efficiency, leading to cost savings.

Prerequisites

  1. You should know how to connect mongoDB using a connection String when working with MongoDB driver (MongoDB version 4.2 or higher).
  2. A Collection to create a Atlas Secarh Index.

Steps to Create a Search Index with Static Field Mapping

Creating a search Index with static field mapping with MongoDB Atlas UI are defined below:

  1. Connect to MongoDB By logging into our MongoDB Atlas Database.
  2. Select project your specific project and select the database you are working with and the collection. As per here, we are considering “GeeksForGeeks” database and “students” collection within it.
  3. Click on Search Indexes
  4. Click on create Search Index to create search index.
  5. Select an Atlas Search Configuration Method and click Next.
    • For a guided experience, select the Atlas Search Visual Editor.
    • To edit the raw index definition, select the Atlas Search JSON Editor.
  6. Enter the Index Name, and set the Database and Collection and click on Next.
  7. Click on Refine Your Index.
  8. Toggle Off dynamic mapping and only anticipate the search that the user might query. Let us consider that we are creating a student portal application. in this case the most important fields that the user might query are student name, course, graduation year, address.
  9. Add Field mapping and data type, the click on Add. For this particular example we have taken, we add a name, course and graduation year to field name and set datatype as string and date for graduation year.
  10. Click on Create Search Index.
  11. Close the “You’re All Set!” Modal Window.
  12. Check the status, the newly created search indexes appear on Atlas Search tab.

After creating search indexes for course, address, and graduation year in the similar way.

Search-Indexes-With-Static-Mapping

Search Indexes With Static Mapping

Creating a Search Index with Static Mapping Using MongoDB Driver

We are creating a search index for a cluster using the MongoDB Driver (Programmatically). Connect to MongoDB using connection string.

Create a sample application named create-index.js defines a search index with static mapping in our collection, and then runs the createSearchIndex command to create the index.

db.students.createSearchIndex(
{
"analyzer": "lucene.standard",
"searchAnalyzer": "lucene.standard",
"mappings": {
"dynamic": false,
"fields": {
"course": {
"type": "string",
"analyzer": "lucene.keyword" // Exact match for course names
},
"name": {
"type": "string",
"analyzer": "lucene.standard" // Standard analyzer for full-text search on names
},
"graduationYear": {
"type": "integer" // Numeric data type for graduation year
}
}
}
})

Advantages of Using Static Mapping

  • Granular Control: It provides precise control over index fields and analysis for optimized search.
  • Predictable Performance: Its consistency in indexing leads to predictable search performance.
  • Schema Validation: It consists of defined data types that helps validate document structure, improving data quality.
  • Flexibility: It provides custom analyzers for different fields enable tailored search experiences.

Disadvantages of Using Static Mapping

  • Limited Adaptability: Static mapping provides flexibility in defining how fields are searched. However, upfront planning for all relevant fields is crucial, as new search needs might require adjustments in the future.
  • Less Dynamic: It is less dynamic due to manual configuration being required for each indexed field, making it less adaptable to frequent schema changes.
  • Maintenance Overhead: It requires to update static mapping for evolving schemas adds maintenance complexity.
  • Potential for Errors: It requires careful planning and testing as incorrect data types or analyzers may lead to unexpected search outcomes.

Best Practices for Using Static Field Mapping

  • Define mappings in advance: Static mappings require you to define mappings in advance.
  • Consider search requirements: Before mapping index fields, assess search requirements and identify the data type of the order outline data.
  • Limit mapping fields: An unlimited number of mapping fields can lead to a mapping explosion, which can cause out of memory errors.
  • Ignore strings with too large a length: Ycan configure a field not to be indexed if the string length is too large or if the document contains wrong data.

Conclusion

Overall, the Atlas Search Index in MongoDB Atlas is a powerful tool for improving search capabilities within your database. By understanding and using static mapping, you can customize your search indexes to specific data and user needs, leading to improved relevancy, enhanced performance, and cost optimization.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads