Getting started with MongoDB Atlas Search and Java
Aasawari Sahasrabuddhe7 min read • Published Jul 30, 2024 • Updated Jul 30, 2024
FULL APPLICATION
Rate this tutorial
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:
- Atlas UI: A user-friendly interface for managing search indexes
- Atlas CLI: Command line interface for more direct control
- 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!
Below are the prerequisites you need to get started.
- 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.
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.
Log in to your Atlas cluster and switch to the “Atlas Search” section on the screen.
Click on “Create Search Index.”
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:
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:1 atlas 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.
To create an index using Spring Boot application, you will have to use the code example below:
1 try (MongoClient mongoClient = MongoClients.create(uri)) { 2 MongoDatabase database = mongoClient.getDatabase("sample_mflix"); 3 MongoCollection<Document> collection = database.getCollection("movies"); 4 SearchIndexModel indexOne = new SearchIndexModel("indexOne", 5 new Document("mappings", 6 new Document("dynamic", true))); 7 collection.createSearchIndexes(Collections.singletonList(indexOne));
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:
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.
Click on “Save Changes” and the index creation will start.
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.
If you wish to create the above index using the Spring Boot application, you can try using the below code:
1 try (MongoClient mongoClient = MongoClients.create(uri)) { 2 MongoDatabase database = mongoClient.getDatabase("sample_mflix"); 3 MongoCollection<Document> collection = database.getCollection("movies"); 4 SearchIndexModel 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")))))); 12 collection.createSearchIndexes(Collections.singletonList(indexTwo)); 13 }
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.
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.
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.
Use the code snippet below to create the same index programmatically in Spring.
1 try (MongoClient mongoClient = MongoClients.create(uri)) { 2 MongoDatabase database = mongoClient.getDatabase("sample_mflix"); 3 MongoCollection<Document> collection = database.getCollection("movies"); 4 SearchIndexModel indexThree = new SearchIndexModel("testIndex03", 5 new Document("mappings", 6 new Document("dynamic",false).append("fields", 7 new Document().append("fullplot", 8 Arrays.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"))))); 16 collection.createSearchIndexes(Collections.singletonList(indexThree)); 17 }
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.
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.
Once done, create the index.
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.
1 atlas clusters search indexes create --clusterName <clusterName> --file index.json
To create the above index, use the below code in your Spring application.
1 try (MongoClient mongoClient = MongoClients.create(uri)) { 2 MongoDatabase database = mongoClient.getDatabase("sample_mflix"); 3 MongoCollection<Document> collection = database.getCollection("movies"); 4 SearchIndexModel 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"))))); 16 collection.createSearchIndexes(Collections.singletonList(indexThree)); 17 }
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.
1 atlas clusters search indexes list --clusterName <clusterName> --db sample_mflix --collection movies 2 atlas clusters search indexes delete <index ID> --clusterName <clusterName>
Using Spring Boot application: Use the below command to delete all the indexes created.
1 collection.dropSearchIndex("<indexName>");
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.