2 / 2
Jul 2024

Hi Team,

Aiming to achieve character by character and word by word (initial words with subsequent single word) autocomplete feature in mongo atlas. The autocomplete operator is not sufficient to achieve the same. Any help on this would be greatful.

Thanks,
Manikandan

Hello,
To achieve character-by-character and word-by-word autocomplete in MongoDB Atlas, you can use the autocomplete operator provided by Atlas Search. Here’s how it works:

Autocomplete Operator:
The autocomplete operator performs a search for a word or phrase containing a sequence of characters from an incomplete input string.
Fields you intend to query with this operator must be indexed with the autocomplete data type in the collection’s index definition1.
You can use it for search-as-you-type applications to predict words accurately as characters are entered in your search field.
It returns results based on the tokenization strategy specified in the index definition for autocompletion.
Note that Atlas Search might return inaccurate results for queries with more than three words in a single string.
If you want to build suggestions or dropdowns, consider querying a collection of suggested search terms or using past search terms to populate the dropdown.
You can also define synonym mappings in your Atlas Search index to search for exact or alternative words1.
Syntax:

{ "$search": { "index": "<index name>", // Optional, defaults to "default" "autocomplete": { "query": "<search-string>", "path": "<field-to-search>", "tokenOrder": "any|sequential", "fuzzy": <options>, "score": <options> } } }

Scoring Behavior:
Exact matches receive a lower score than non-exact matches.
To boost exact matches, index the field as both autocomplete and string types, and query using the compound operator1.
Remember to adjust the parameters according to your specific use case. I hope this helps you achieve the desired autocomplete functionality!

Best regards,
florence023