How to set up atlas search index for field inside an array?

Hi Experts,

I have document with below structure:

{
header :  "898288283",
items: [
              { itemNo: "1", name: "dpcxed"},
              { itemNo: "2", name: "edfic"},
          ]
}

I need set up autocomplete index for the items.name fields. May u know what is the properly index definition for it?

thanks in advance.

Hey Joshua,

This exact ask is a feature we are working on currently (updates to come soon!), but one option is you could use Embedded Documents (query, index definition) with type as “autocomplete”. I believe items is embedded and under it items.name is type:autocomplete

1 Like

Hi ,
i created a new search index with below json config.

{
  "mappings": {
    "fields": {
      "items": {
        "dynamic": false,
        "fields": {
          "name": {
            "type": "autocomplete"
          }
        },
        "type": "embeddedDocuments"
      }
    }
  }
}

But when i try to execute .

{
  index: 'embedded',
  autocomplete: {
    query: 'dp',
    path: 'name',
    tokenOrder: 'sequential',
    fuzzy: {
      // maxEdits: 1,
      // "maxExpansions": 100,
    }
  }
}

I got below error
PlanExecutor error during aggregation :: caused by :: Remote error from mongot :: caused by :: autocomplete index field definition not present at path materialNo

Hey Josh, you have to also run the embeddeddocument query. I tested it with this and it worked for me:

{
  embeddedDocument: {
    path: "items",
    operator: {
      autocomplete: {
          query: 'dp',
          path: 'items.name',
          tokenOrder: 'sequential',
          fuzzy: {}
    }}
}}
1 Like

Hi Elie,
In compass’s aggregrations tab, I do not have the option to choose an operator with “embeddedDocument”.
There is $search $searchMeta…
I am using mongo db 5.0. Or you are using different version?

This is the full aggregation, you would still use $search…

[{$search: {
  embeddedDocument: {
    path: "items",
    operator: {
      autocomplete: {
          query: 'dp',
          path: 'items.name',
          tokenOrder: 'sequential',
          fuzzy: {}
    }}
}}}]

I think you should be able to paste that into Compass. I’m on 5.0.9. I don’t believe it is tied to a version.

2 Likes

Hi Elle,
I tried to reindex then it start works. Many thanks.