EventGet 50% off your ticket to MongoDB.local London on October 2. Use code WEB50Learn more >>
MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

Exploring Search Capabilities With Atlas Search

Aasawari Sahasrabuddhe9 min read • Published Jul 30, 2024 • Updated Aug 20, 2024
SpringAtlasJava
FULL APPLICATION
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Welcome to the second part of our series on leveraging MongoDB Atlas Search with Spring Boot. In Part 1, we learned how to create Atlas Search indexes using various methods: the MongoDB CLI, the Atlas UI, and programmatically within a Spring Boot application. We explored the fundamental steps required to set up these indexes, providing a solid foundation for implementing powerful search capabilities.
In this part, we will build upon that foundation by diving deeper into the practical application of these indexes. This article will focus on crafting and executing search queries using Spring Boot, illustrating how to harness the full potential of Atlas Search in your applications.
We will also explain key concepts and terminologies associated with Atlas Search, ensuring you have a comprehensive understanding of how it operates.
Whether you are a seasoned developer or new to MongoDB and Spring Boot, this series aims to equip you with the knowledge and tools needed to implement efficient and effective search functionality in your applications.

Pre-requisites

  1. A dedicated Atlas cluster to create indexes programmatically
  2. Java Version 22
  3. Sample data loaded in your atlas cluster

Querying with Atlas Search indexes

Before we get into writing search queries using the indexes we've created, we highly recommend familiarizing yourself with the foundational concepts of MongoDB Atlas Search. A great resource for this is Part 1 of the series "Getting Started With MongoDB Atlas Search and Java.".
This tutorial will provide you with an understanding of the key principles behind Atlas Search and demonstrate how to create different types of indexes tailored to various use cases. By gaining this background knowledge, you'll be better equipped to effectively implement and leverage search functionalities in your Spring Boot applications.
In each of the sections below, we will cover the terminology and understand real-life examples where the indexes would be helpful.
The search queries mentioned will be used on the movies collection of the sample_mflix database

Case 1: Setting dynamic properties to true

If you have an application where the schema changes frequently, setting up dynamic property to true will automatically create the indexes on the supported data type fields. For example, the testIndex01 has the dynamic mapping to true, which indexes all fields with supported data types.
The below function makes use of testIndex01 to query all the string index fields.
Using the above function, you can find movies that mention the keyword mentioned in the _query _on the title, plot, and fullplot fields defined in the path. _For example…
…will give all movies with the keyword “cartoon” as shown below:

Case 2: Using analyzers in search indexes

In this case, we will be utilising the testIndex02 created with field mapping. Field mapping means static mapping has been used and not all fields are indexes. Only fields that are mentioned are indexed.
In our case, the field _genre _has been indexed and can be used with the below method:
For example, if you wish to list all the movies whose genre is action, you can make the REST call as:
This will give the output as:

Case 3: Using facets to categorise the data

From the above example, we will be using the facets used by the searchMoviesAndCategorise method to perform a search on genres and categorise the data based on genres. Later, the testIndex02 also has numberFacet created on the year field, which will bucket all the movies released every 10 years and return the count for all movies.
For example, when you use the below API call, it will categorise the data based on genres first and then categorise on the number of movies released every 10 years to date.
It will give the output as:

Case 4: Search text using incomplete spellings

The autocomplete feature helps you search using keywords that are spelt incompletely. This feature is helpful when a user wants to look for a book with an incomplete name on the e-commerce website. In this case, we will be making use of testIndex03 and outlook for movies where incomplete keywords for the fullplot fields have been mentioned.
The search function can be written as:
For example, if the fullplot field mentions Spa and Cow as incomplete keywords for Spain, Space, cowboys, etc. to test, you can search using the following API.
It will give a response as:

Case 5: Searching using incorrect spelling

Sometimes, we search for items/products on websites using the wrong spelling due to autocorrect or unawareness of the right spelling, but the application still gives us the right results. This is possible because of the fuzzy search feature.
MongoDB’s Atlas Search also allows you to search through the text with the incorrect spelling.
The testIndex03 created in the previous part of the series will be used here in the below function:
In this case, you can search for the movie with a misspelled title, and the right movie name will appear in the results.
For example, if you search for the movie with the below REST call…
…where the word Dinosour is wrongly spelt as Dinosor, it will give the results as:

Case 6: Search with synonym keywords

Similar to the case mentioned above, MongoDB Atlas Search also allows searching with synonym keywords — i.e., words that have the same or nearly the same meaning.
To explore this feature, we utilise the testIndex04 to write the search query.
When we create the search index, we specify the collection as Synonyms Source Collection, from where the synonyms will be mapped. The testIndex04 mentions the collection name as test_synonyms, which is the source collection.
The search query is written as:
The $meta used in the query with searchScore will determine how close the document is to the searched keyword. Before we do the rest call to test the query, we need to create the mapping for the synonyms in a different collection.
Insert the data below into the test_synonyms collection.
To test the above query, you can use the REST call as:
This will give results based on the mapping created, as shown below.
The complete code for the application using all the cases mentioned above is available in the GitHub repository.

Clean up the indexes

After you have utilised all the indexes created, you can use the below method to delete all the search indexes that have been created through the application.
And call the API with the below command

Conclusion

In this second part of our series on exploring MongoDB Atlas Search with Spring Boot, we have delved into the practical applications of the search indexes we created in Part 1. By demonstrating various search scenarios, we highlighted the versatility and power of Atlas Search in handling different types of queries and data structures.
In this part, we have covered some use cases like autocomplete, fuzzy, field mapping, etc. If you wish to learn about more cases, we also have other articles — MongoDB Atlas Search Using the Java Driver and Spring Data and How to Build a Search Service in Java — where you can explore more use cases.
If you have any questions or suggestions, please feel free to reach out on the MongoDB community forum and also explore more fun tutorials through our MongoDB Developer Center.
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

Building a Knowledge Base and Visualization Graphs for RAG With MongoDB


Sep 02, 2024 | 12 min read
Industry Event
locationBOSTON, MA, UNITED STATES | IN-PERSON

MongoDB Developer Day Boston


Sep 24, 2024 | 12:30 PM - 9:00 PM UTC
Article

Building Service-Based Atlas Cluster Management


Sep 23, 2022 | 4 min read
Tutorial

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


Jul 08, 2024 | 6 min read
Table of Contents
  • Pre-requisites