
Introduction
Mongo Atlas Search is an embedded full-text search in MongoDB Atlas that gives you a seamless, scalable experience for building relevance-based app features.
Mongo Atlas Search allows fine-grained text indexing and querying of data on your Atlas cluster. It enables advanced search functionality for your applications without any additional management or separate search system alongside your database. Atlas Search provides options for several kinds of text analysers, a rich query language that uses Atlas Search aggregation pipeline stages like $search in conjunction with other MongoDB aggregation pipeline stages, and score-based results ranking.
Atlas Search is a strong competitor against Elastic search because you don’t need to maintain two clusters now, one for storing the data and another one for serving the data for searching. With Atlas Search, you can have the NoSQL database (mongod) and the full-text search engine (mongot) running in the same cluster and thus save the cost.
Atlas search comes into play when in a situation where you have a narrow timeframe, and you have to implement all this perfectly, autocomplete in the search box, fuzzy search(A situation whereby you match wrong spellings, synonyms and more), a full-text search and not just a search where you match keywords, ID’s, or the whole string, also users are likely to make mistakes or use the wrong phrase in their search. So here comes the Atlas search.
Audience
All the users can consider atlas search as one of the choices because of multiple use cases. Also, Let’s say you’re about to build a project that has lots of stuff in it, for example, a blog, the collections in your database might be ‘users’ and ‘posts’, and that alone is enough for your database. Now, as your site grows, you might want to implement a function that fetches similar posts to what the user has just read based on keywords.
You would also love a situation where your users can easily search for a topic and get a result that is highly related to what they want.
You want an auto-complete as users type their search phrase. So all this feature comes handy with Atlas search.
Prerequisite
Knowledge of mongo aggregate queries are needed to fully grasp the use of mongoDB in the application.
Search using Atlas
Mongo Atlas supports multiple programming languages like java, python, ruby, etc. You can also use altas search features via CLI or the Atlas UI. For example, if you are building a java application like netflix and you want to search for a movie name, then you can connect your java application to the mongoDB and can perform the search with your application.
To enable search, you need to follow the following steps to create the search index inside mongoDB cluster.
Create Search Index
- To create a search index in mongo atlas search, you need to login to the mongo cluster and follow the following steps.
- After clicking create search Index, you need to choose the collection on which search index will be created. Also, static and dynamic field mapping is supported in the mongo atlas.
How To Use Search Index
You can use search index along with the usual aggregation pipelines using the keyword $search.
Here is the sample mongo compass aggregation pipeline example:
db.movies.aggregate([ { "$sea̧rch": { index: "default", "phrase": { "path": "plot", "query": "new purpose", }, }, { "$limit": 5 } } ])
KeyNote:
- The $search pipeline stage is used to do full-text searches against an Atlas Search Index.
- The index field is optional if the index name is “default”.
- The text operator is used to perform full-text searches using the analyzer specified in the index definitions.
- text.query specifies the query string for the search.
- text.path specifies the fields to search against.
Similar aggregation pipeline can be exported into a programming language using the atlas UI.
Full text search in Atlas
Full-text search refers to searching some text inside extensive text data and returning results that contain some or all of the words from the query. In contrast, traditional search would return exact matches.
Following is the query for full text search:
db[collection].aggregate( [ { "$search": { "text": { "query": term, "path": ["body_en", "chapter_en"], "fuzzy": {"maxEdits": 1, "maxExpansions": 10}, }, "highlight": {"path": ["body_en", "chapter_en"]}, } }, {"$limit": 50}, { "$project": { "_id": 0, "collection_id": 1, "collection": 1, "hadith_no": 1, "score": {"$meta": "searchScore"}, "highlights": {"$meta": "searchHighlights"}, } }, ] )
The first aggregation is the $search and it holds the text operator and highlight option. text is the full-text search in Atlas Search and it uses the index you have created earlier. The query holds the search term and if there are multiple terms it searches for each term and produces a matching score that we have used in the $project aggregation to sort them in order. The result is already sorted. The path holds on which fields the search is to be done.
And it also offers fuzzy searching, Just by adding fuzzy in the text. The maxEdits inside fuzzy is the number of allowable character to replace and maxExpansions is the number variation. In my fuzzy, for the word cot, it will look for cat, eat, can, fat, at, etc up to 10 variations and you can notice a single character replace.
Phrase search in Atlas
The phrase search performs search for documents containing an ordered sequence of terms. Like, if you have to search a phrase inside a sentence, in that case you can use Phrase text search.
dbcollection.aggregate( [ { "$search": { "phrase": { "query": term, "path": "body_en", "slop": 2, }, "highlight": {"path": "body_en"}, } }, {"$limit": 50}, { "$project": { "_id": 0, "collection_id": 1, "score": {"$meta": "searchScore"}, "highlights": {"$meta": "searchHighlights"}, } }, ] )
The implementation is again really simple. slop is the new field here, this is the allowable distance between words that are put in the query field. So here slop: 2 means there can be at most two words between the words we have put on the query. As always the exact matches score higher.
Compound search in Atlas
Until now, You explored full-text search and phrase search. But what if you need the combination of both. Atlas supports the compound operator which is really great. It helps to combine the operators and also score among them. You can combine multiple aggregation keyword and filter your search efficiently.
db[collection].aggregate( [ { "$search": { "compound": { "must": [ { "text": { "query": term, "path": ["body_en", "chapter_en"], "fuzzy": {"maxEdits": 1, "maxExpansions": 10}, } } ], "should": [ { "phrase": { "query": term, "path": "body_en", "slop": 2, } } ], }, "highlight": {"path": ["body_en", "chapter_en"]}, } }, {"$limit": 50}, { "$project": { "_id": 0, "collection_id": 1, "book_ref_no": 1, "hadith_grade": 1, "score": {"$meta": "searchScore"}, "highlights": {"$meta": "searchHighlights"}, } }, ] )
Inside the compound operator, we used must and should. must holds the clause that must match to search terms and should is like better to have, if the should clause match, the score will be higher. This makes complex queries simple.
We put the full-text search inside must, that means we want my search query to match the terms individually, there can be words in the search query that are not present in the whole DB, so we chose this to be the must. And my should clause holds the phrase search. It will be better if the search finds a phrase, the score will be higher for this case.
Conclusion
In this article, we’ve have briefly introduced what MongoDB Atlas Search is and what it can do. Simpl, Atlas Search is used for full-text searches and can be a strong competitor of Elasticsearch. Both Atlas Search and Elasticsearch use Apache Lucene under the hood and thus the syntax for them is pretty similar.
We have just scraped the surface of Atlas Search by creating a Search Index in Atlas UI and performing simple search queries in mongosh. Full-text search with Atlas Search is a very big topic and covers many sections including mappings, analyzers, compound queries, autocomplete, etc. Here are some links which help you to explore and search more.