2
votes

I have the following schema:

{
   name: String,
   phones: [
        {
            number: String,
            type: String
        }
   ]
}

How do I index phones.number so that I can write something like:

collection.aggregate([{
       "$search":{ 
            "compound":{
                  "should":[
                      {"autocomplete":{"query":"012345","path":"name"}},
                      {"autocomplete":{"query":"012345","path":"phones.number"}}
                  ]
             }
         }
}])

The docs here give an example for an array of strings but not an array of objects.

1
Autocomplete is not a valid index definition for arrays either, afaik, - Nice-Guy

1 Answers

3
votes

As per this answer, indexing by a property of a subdocument in an array is supported. Just create an index by phones.number.

See the documentation for more information.


EDIT

I was confusing standard indexing with indexing for Atlas Search. From the documentation, you should be able to index an array of documents this way:


{
  "analyzer":"lucene.standard",
  "searchAnalyzer":"lucene.standard",
  "mappings":{
    "dynamic":false,
    "fields":{
      "name":{
        "type":"string",
        "analyzer":"lucene.standard"
      },
      "phones":{
        "type":"document",
        "fields":{
          "number":{
            "type":"string",
            "analyzer":"lucene.standard"
          },
          "type":{
            "type":"string",
            "analyzer":"lucene.standard"
          }
        }
      }
    }
  }
}