Search code examples
mongodbmongodb-atlas-search

MongoDB aggregate only working with exact string


I am trying to implement a search feature to MongoDB and this is the aggregate pipeline I am using:

[
    {
        '$search': {
            'text': {
                'query': 'albus', 
                'path': [
                    'first_name', 'email', 'last_name'
                ]
            }
        }
    }, {
        '$project': {
            '_id': 1, 
            'first_name': 1, 
            'last_name': 1
        }
    }, {
        '$limit': 5
    }
]

The command returns documents that contain only exactly albus or Albus, but return nothing for queries like alb, albu, etc. In the demo video I watched here: https://www.youtube.com/watch?time_continue=8&v=kZ77X67GUfk, the instructor was able to search based on substring.

The search index I am currently using is the default dynamic one. How would I need to change my command?


Solution

  • You need to use the autocomplete feature, so your query will look like this:

    {
        $search: {
          "autocomplete": {
                'query': 'albus', 
                'path': [
                   'first_name', 'email', 'last_name'
                ]
          }
       }
    }
    

    Mind you both first_name, email and last_name need to be mapped as autocomplete type so a name like albus will be indexed as a, al, alb, albu, albus. Obviously this will vastly increase your index size.

    Another thing to consider is tweaking the maxGrams and tokenization parameters. this will allow very long names to still work as expected and if you want to allow substring match like lbu matching albus.