Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atlas search array of objects

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.

like image 321
Mika Avatar asked Oct 28 '25 08:10

Mika


1 Answers

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"
          }
        }
      }
    }
  }
}

like image 135
Chris Avatar answered Oct 29 '25 23:10

Chris