1

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?

Doug
  • 14,387
  • 17
  • 74
  • 104
Shrey
  • 146
  • 2
  • 11
  • 2
    Does [this](https://stackoverflow.com/questions/44833817/mongodb-full-and-partial-text-search) answer your question? – Spack Jarrow Aug 22 '20 at 20:12

1 Answers1

3

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.

Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43
  • Where would the maxGrams and tokenization parameters come into play? – Shrey Aug 23 '20 at 21:05
  • When you define your mapping, you define those there to set the behaviour of the index. this can't be done dynamically as the tokens have to be created first. – Tom Slabbaert Aug 24 '20 at 06:07
  • Alright I have it setup so that it works with `alb`, `albu`, etc, but the `autocomplete` key only allows one field for the `path` key. Is there another way to handle this so that multiple fields can be queried? – Shrey Aug 24 '20 at 16:29