MongoDB Atlas text index with autocomplete failing to match

5.2k views Asked by At

I created a text index on one of my database collections which has autocomplete set for each. The collection fields which I have indexed are firstname, surname and email. The following is what I used to create my text index. It succeeds:

 {
 "mappings": {
"dynamic": false,
"fields": {
  "email": [
    {
      "foldDiacritics": false,
      "maxGrams": 7,
      "minGrams": 3,
      "tokenization": "edgeGram",
      "type": "autocomplete"
    }
  ],
  "firstname": [
    {
      "foldDiacritics": false,
      "maxGrams": 7,
      "minGrams": 3,
      "tokenization": "edgeGram",
      "type": "autocomplete"
    }
  ],
  "surname": [
    {
      "foldDiacritics": false,
      "maxGrams": 7,
      "minGrams": 3,
      "tokenization": "edgeGram",
      "type": "autocomplete"
    }
  ]
}
}
}

When I run the following query in the mongoDB shell I receive no error but also no results when the surname field has an exact match to the query:

db.users.aggregate([ { "$search": { "autocomplete": 
{ "query": "janson","path": "surname", "fuzzy": 
{ "maxEdits": 2, "prefixLength": 3 } } } }])

I have also tried it with all three collection fields in the index:

db.users.aggregate([ { "$search": { "autocomplete": 
{ "query": "janson","path": ["firstname","surname", "email"], "fuzzy": 
{ "maxEdits": 2, "prefixLength": 3 } } } }])

This gives me the following error:

"Remote error from mongot :: caused by :: "path" must be a string (from "autocomplete")",

I don't see how the fault could be with my index definition because it is validated by atlas and exists on the server. What could be the problem?

Thanks

EDIT

a simplified query:

db.groups.aggregate([ { "$search": { "autocomplete": { "query": "test", "path": "title" } } }])

The Atlas index specification:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "title": [
        {
          "foldDiacritics": false,
          "maxGrams": 7,
          "minGrams": 3,
          "tokenization": "edgeGram",
          "type": "autocomplete"
        }
      ]
    }
  }
}

A simple document which is purported by Atlas to be 100% indexed:

{
    "_id": {
        "$oid": "5f773d551dd78dfa97219ae2"
    },
    "title": "test"
}
1

There are 1 answers

6
Alex Blex On BEST ANSWER

You need to specify index name if you are using non-default index:

db.users.aggregate([ { 
    "$search": {
        "index": "<REPLACE_WITH_INDEX_NAME>", 
        "autocomplete": { 
            "query": "janson",
            "path": "surname", 
            "fuzzy": { 
                "maxEdits": 2, 
                "prefixLength": 3 
            } 
         } 
    } 
}])

the <REPLACE_WITH_INDEX_NAME> is SearchIndex1 on the image below:

index name