MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

Getting started with MongoDB Atlas Search and Java

Aasawari Sahasrabuddhe7 min read • Published Jul 30, 2024 • Updated Jul 30, 2024
SpringAtlasJava
FULL APPLICATION
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
MongoDB’s Atlas Search is an embedded full-text search available in MongoDB Atlas, providing a seamlessly scalable experience for building relevance-based app features. Built on Apache Lucene, the Atlas Search feature supports a wide scope of search functionalities without the need for additional applications to handle searching capabilities.
In this tutorial series, we will delve into the concepts of Atlas Search in detail and build a sample application using Spring Boot. These articles will guide you through the process of creating search indexes and implementing search functionalities on documents using advanced features like fuzzy searches, autocomplete, and more.
In the first part of this series, we will cover how to create search indexes using various methods:
  1. Atlas UI: A user-friendly interface for managing search indexes
  2. Atlas CLI: Command line interface for more direct control
  3. Spring Boot application: Programmatically managing indexes within your application
By the end of this tutorial, you'll have a solid understanding of how to leverage MongoDB’s Atlas Search to enhance your application’s search capabilities.
Let's get started!

Prerequisites

Below are the prerequisites you need to get started.
  1. An M0 cluster — you can create one by registering for a free Atlas account
  2. Java version 17+
Note: The free tier or M0 allows only three Atlas search indexes to be created at once. To create more than three at a time, you must upgrade to M2/M5 or higher. Also, if you wish to create an index using CLI and drivers, make sure to upgrade the cluster to M10 or higher.

What are Atlas Search indexes?

Traditional database indexes, such as B-trees or hash indexes in MongoDB, improve query performance by allowing the database to locate data quickly without scanning every document. They are typically used for exact match queries, range queries, and sorting operations.
On the other hand, the Atlas Search indexes are designed for full-text search, supporting advanced text queries, relevancy scoring, and text analysis. It is a specialised data structure that organises data to make it easily searchable. It maps terms to the documents containing those terms, enabling quicker retrieval of documents based on specific identifiers. To query data in your Atlas cluster using Atlas Search, you need to configure an Atlas Search index.
The search indexes can be created on a single field or multiple document fields. The indexes on these fields could be created with different mechanisms which we will discuss.
Learn more about analysers, tokenisers, and field mappings.

Creating Atlas Search indexes

Case 1: Creating an index with dynamic mapping set to true

Using Atlas UI

Log in to your Atlas cluster and switch to the “Atlas Search” section on the screen.
Click on “Create Search Index.”
Screenshot representing the Atlas UI to create the Search indexes
Screenshot representing the Atlas UI to create the Search indexes
Click on “Next” and select the index name and collection on which you wish to create the search index.
The index build will start and after a few minutes, you will see the screen below:
The screenshot representing that the index has been created
The screenshot representing that the index has been created

Using Atlas CLI

To create and manage Atlas Search indexes using the Atlas CLI, your Atlas cluster must run MongoDB 6.0+ or 7.0+.
You can't use the mongosh command to create and manage Atlas Search indexes on M0, M2, or M5 Atlas clusters. You will have to upgrade to M10+ to use this method. The index will be created using the below command:
The index.json file should contain the following information:
It will give you the following output as:
Index testIndex01 created.

Using Spring Boot application

To create an index using Spring Boot application, you will have to use the code example below:

Case 2: Creating an index with field mapping

Using Atlas UI

As mentioned in the first step to create the index, select the Visual Editor and select the database and collection name as sample_mflix and movies respectively.
Once you select it click “Next” and then on the “Refine Your Index” tab. Click on “Add Field Mapping” and you will see the below screen pop up:
Screenshot representing the Atlas UI to add field mapping
Screenshot representing the Atlas UI to add field mapping
Select the field name and data type that you would like to recognise the field name as. For example, the below screenshot represents the field mapping added for the genres and year fields.
Screenshot representing the field mapping created
Screenshot representing the field mapping created
Click on “Save Changes” and the index creation will start.

Using Atlas CLI

Update the index.json file as:
Now to create the index, use the same command as mentioned in Case 1 above.

Using Spring Boot application

If you wish to create the above index using the Spring Boot application, you can try using the below code:

Case 3: Creating an index to test the autocomplete capability

The autocomplete feature of Atlas Search allows you to text with incomplete words and sentences. The fields that you intend to query with the autocomplete operator must be indexed with the autocomplete data type in the collection's index definition. To learn more about the feature, you can follow the documentation for autocomplete in Atlas Search.

Using Atlas UI

After following the same steps to create field mapping with data type as autocomplete for fullplot, specify the details as below and create the index.
Screenshot representing Atlas UI to create autocomplete field mapping and tokenisation
Screenshot representing Atlas UI to create autocomplete field mapping and tokenisation

Using Atlas CLI

To create the same index with Atlas CLI, update the index.json file as:
As mentioned in the above screenshot, to have autocomplete tokenisation set for Atlas Search, you will have to see certain parameters to create the tokens.
For example, in the above JSON, the foldDiacritics set to false returns only results that match the strings with or without diacritics in the query. For instance, a search for cafè returns results only with the characters in the word cafè.
The maxGrams and minGrams are attributes for nGram tokenisers which determine the number of characters to include in the longest and shortest tokens created respectively.
Use the same command as above to create the index.

Using Spring Boot application

Use the code snippet below to create the same index programmatically in Spring.

Case 4: Creating a search index to make use of analyzers

Atlas Search allows you to create analyzers to control how it turns a string field's contents into searchable terms.
The methods below will help you understand how to create indexes for using these analyzers.

Using Atlas UI

Follow the same steps that you have been following in the above cases. The below screenshot will help you understand how you can add analyzers using the UI.
Screenshot representing Atlas UI to create an analyzer
Screenshot representing Atlas UI to create an analyzer
Once done, create the index.

Using Atlas CLI

Update the index.json as below:
And later, use the same command to create the index.

Using Spring Boot application

To create the above index, use the below code in your Spring application.
The code for creating all the indexes programmatically is available at the GitHub repository.

Deleting the search indexes

After the indexes are created, and you wish to drop those indexes, make sure you drop the indexes using one of the following methods:
Using UI: Click on the index on the UI and select “Delete Index” from the list.
Using Atlas CLI: Follow the steps below to create all the indexes and index IDs and use the command to delete the index.
Using Spring Boot application: Use the below command to delete all the indexes created.

Conclusion

Creating Atlas Search indexes using the Atlas UI, CLI, and Spring Boot application provides a comprehensive understanding of how to leverage MongoDB's powerful search capabilities. The Atlas UI offers an intuitive and visual approach, perfect for those who prefer a graphical interface. The CLI gives more control and scripting capabilities, making it ideal for automation and integration into development workflows. Integrating search index creation in a Spring Boot application showcases the seamless integration of MongoDB Atlas with Java-based applications, enabling developers to build robust and scalable search functionalities.
By mastering these methods, you can enhance your applications with advanced search features, ensuring a better user experience and improved data retrieval performance. Whether you are a beginner or an experienced developer, combining UI, CLI, and programmatic approaches provides a flexible and comprehensive toolkit for managing search indexes in MongoDB Atlas.
If you have any questions or need further assistance, please reach out to the MongoDB Community Forums and refer to the official documentation and MongoDB University tutorials for more in-depth information and guidance.
Happy searching!
Top Comments in Forums
There are no comments on this article yet.
Start the Conversation

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Improve Your App's Search Results with Auto-Tuning


Aug 14, 2024 | 5 min read
Tutorial

Streamlining Log Management to Amazon S3 Using Atlas Push-based Log Exports With HashiCorp Terraform


Jul 08, 2024 | 6 min read
Tutorial

Analyzing Analyzers to Build the Right Search Index for Your App


Jan 23, 2023 | 8 min read
Tutorial

How to Deploy MongoDB Atlas with Terraform on AWS


Jan 23, 2024 | 12 min read
Table of Contents