Implementing Autocomplete Feature in MongoDB with Atlas Search Index

Hello everyone,

I’m trying to implement an autocomplete feature. I have data with repeating names; for example, let’s consider name like “Australia”, “Harbour Bridge Australia”, and many other entries with Australia. Also, I have other entries like “Austin”, “Austria”, etc.

Now I want to extract relevant terms. Let’s say my input query is “Au”. The suggestion I am expecting is a unique combination of names like Australia, Austin and Austria.

I have set up a MongoDB Atlas Search Index for this feature. However, the result I am getting is all the suggestion that matches the “Au” keyword, which I think is expected as this is basically the title that I have in the database.

So is there a way of doing this and getting only the relevant search results and not suggesting “Harbour Bridge Australia” initially and only suggesting once it is the obvious query “Austr”

Any insights, or guidance on best practices for implementing this feature would be greatly appreciated.

Thank you in advance for your help!

Hi @Gribesh_Dhakal and welcome to MongoDB community forums!!

This seems to be a little confusing to me to understand and hence assist you further here.
As per my understanding, you are looking to:

  1. Search with "Au" keyword, to return only Australia, Austria and Austin.
  2. Search with "Austr" to return “Harbour Bridge Australia”

Could you confirm if my understanding is correct? You might want to have a look at how ngrams work in Atlas Search techniques. for further understanding.

Also, it would be beneficial for the community to help if you could share the following information:

  1. A few sample documents from the collection.
  2. The index definition that you are using.
  3. The desired output from the search operation.

Regards
Aasawari