Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

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:
1atlas clusters search indexes create --clusterName <clusterName> --file index.json
The index.json file should contain the following information:
1{
2 "name": "testIndex01",
3 "database": "sample_mflix",
4 "collectionName": "movies",
5 "mappings": {
6 "dynamic": true
7 }
8}
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:
1try (MongoClient mongoClient = MongoClients.create(uri)) {
2MongoDatabase database = mongoClient.getDatabase("sample_mflix");
3MongoCollection<Document> collection = database.getCollection("movies");
4SearchIndexModel indexOne = new SearchIndexModel("indexOne",
5 new Document("mappings",
6 new Document("dynamic", true)));
7collection.createSearchIndexes(Collections.singletonList(indexOne));

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:
1{
2 "name": "testIndex02",
3 "database": "sample_mflix",
4 "collectionName": "movies",
5 "mappings": {
6 "dynamic": true,
7 "fields": {
8 "genre": [
9 {
10 "type": "string"
11 },
12 {
13 "type": "stringFacet"
14 }
15 ],
16 "year": [
17 {
18 "type": "numberFacet"
19 },
20 {
21 "type": "number"
22 }
23 ]
24 }
25 }
26}
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:
1try (MongoClient mongoClient = MongoClients.create(uri)) {
2MongoDatabase database = mongoClient.getDatabase("sample_mflix");
3MongoCollection<Document> collection = database.getCollection("movies");
4SearchIndexModel indexTwo = new SearchIndexModel("testIndex02",
5 new Document("mappings",
6 new Document("dynamic", true).append("fields", new Document().append("genres", Arrays.asList(
7 new Document().append("type", "stringFacet"),
8 new Document().append("type", "string")))
9.append("year",Arrays.asList(
10 new Document().append("type", "numberFacet"),
11 new Document().append("type", "number"))))));
12collection.createSearchIndexes(Collections.singletonList(indexTwo));
13}

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:
1{
2 "name": "testIndex03",
3 "database": "sample_mflix",
4 "collectionName": "movies",
5 "mappings": {
6 "dynamic": true,
7 "fields": {
8 "fullplot": {
9 "foldDiacritics": false,
10 "maxGrams": 7,
11 "minGrams": 3,
12 "tokenization": "nGram",
13 "type": "autocomplete"
14 }
15 }
16 }
17}
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.
1try (MongoClient mongoClient = MongoClients.create(uri)) {
2MongoDatabase database = mongoClient.getDatabase("sample_mflix");
3MongoCollection<Document> collection = database.getCollection("movies");
4SearchIndexModel indexThree = new SearchIndexModel("testIndex03",
5 new Document("mappings",
6 new Document("dynamic",false).append("fields",
7 new Document().append("fullplot",
8Arrays.asList(new Document().append("type", "stringFacet"),
9 new Document().append("type", "string"),
10 new Document().append("type", "autocomplete")
11 .append("tokenization", "nGram")
12 .append("minGrams", 3)
13 .append("maxGrams", 7)
14 .append("foldDiacritics", false)))
15 .append("title", new Document().append("type", "string")))));
16collection.createSearchIndexes(Collections.singletonList(indexThree));
17}

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:
1{
2 "name": "testIndex04",
3 "database": "sample_mflix",
4 "collectionName": "movies",
5 "analyzer": "lucene.english",
6 "searchAnalyzer": "lucene.english",
7 "mappings": {
8 "dynamic": true,
9 "fields": {
10 "fullplot": {
11 "type": "string"
12 }
13 }
14 },
15 "synonyms": [
16 {
17 "analyzer": "lucene.english",
18 "name": "synonymName",
19 "source": {
20 "collection": "test_synonyms"
21 }
22 }
23 ]
24}
And later, use the same command to create the index.
1atlas clusters search indexes create --clusterName <clusterName> --file index.json

Using Spring Boot application

To create the above index, use the below code in your Spring application.
1try (MongoClient mongoClient = MongoClients.create(uri)) {
2MongoDatabase database = mongoClient.getDatabase("sample_mflix");
3MongoCollection<Document> collection = database.getCollection("movies");
4SearchIndexModel indexFlour = new SearchIndexModel("testIndex04",
5 new Document("mappings", new Document()
6 .append("dynamic", true)
7 .append("fields", new Document()
8 .append("fullplot", new Document()
9 .append("analyzer", "lucene.english")
10 .append("type", "string"))))
11 .append("synonyms", List.of(new Document()
12 .append("analyzer", "lucene.english")
13 .append("name", "synonymName")
14 .append("source", new Document()
15 .append("collection", "test_synonyms")))));
16collection.createSearchIndexes(Collections.singletonList(indexThree));
17}
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.
1atlas clusters search indexes list --clusterName <clusterName> --db sample_mflix --collection movies
2atlas clusters search indexes delete <index ID> --clusterName <clusterName>
Using Spring Boot application: Use the below command to delete all the indexes created.
1collection.dropSearchIndex("<indexName>");

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
This is part of a series
Atlas Search with Spring Boot
Up Next
Continue

More in this series
Related
Tutorial

Build a JavaScript AI Agent With LangGraph.js and MongoDB


Sep 18, 2024 | 15 min read
Article

Using Atlas Search from Java


Jul 14, 2023 | 13 min read
Tutorial

CIDR Subnet Selection for MongoDB Atlas


Sep 23, 2022 | 2 min read
Tutorial

An Introduction to Indexes for MongoDB Atlas Search


Feb 03, 2023 | 7 min read
Table of Contents