Open In App

Creating a Search Index with Static Field Mapping in MongoDB

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 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:

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?

Why Use Static Field Mapping for Search Indexes?

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

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

Disadvantages of Using Static Mapping

Best Practices for Using Static Field Mapping

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.


Article Tags :