Index Search by subdocument with autocomplete

Im attempting to Search a collection by a populated subdocument, however Im unable to achieve this.

My model (purchases) is the following:

{
	amount:{ type:Number} ,
	_entityBuyer: {
		type: mongoose.Schema.Types.ObjectId,
		ref: 'Entity',
	},
}

Example of model:

{
	amount:150,
	_entityBuyer: {
		_id:'1234',
		name:'lee',
		handle:'lee'
	}
}

So I’m wanting to lookup all purchases by the _entityBuyer.name via the following:

{
	index: 'search-index-name',
	compound: {
		should: [
			{
				embeddedDocument: {
					path: '_entityBuyer',
					operator: {
						compound: {
							should: [
							{
								autocomplete: {
									query: 'lee',
									path: '_entityBuyer.name',
							},
						},
					],
				},
			},
		},
	},
	],
},

My Search Index is the following:

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

Currently running the above returns an empty result - however I have correctly seeded my database with _entityBuyer.name with the value of ‘lee’

Any help or guidance would be greatly appreciated :slight_smile:

Hi @Lee_Marshall and welcome to MongoDB community forums!!

Based the sample documents provided, below are a few examples that describe the difference between the use of autocomplete and embeddedDocuments operators.

I tried to create and autocomplete index on the _entityBuyer.name and used the following search query that returned the data.

Index Definition:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "_entityBuyer": {
        "fields": {
          "name": {
            "type": "autocomplete"
          }
        },
        "type": "document"
      }
    }
  }
}

and the search query:

[
    {
        '$search': {
            'autocomplete': {
                'query': 'le', 
                'path': '_entityBuyer.name'
            }
        }
    }
]

From the documents shared, it seems there is only one sub document inside the _entityBuyer field. Can you confirm if there are more subdocuments to the field or that all documents within your collection only have a single subdocument?
If there are multiple entities within the _entityBuyer field, you can also consider converting it to an array so that you can use the embeddedDocuments in the index definition. However, please ensure this suits your use case and test thoroughly before doing so.

For example:

If the sample document looks like:

{
	amount:150,
	_entityBuyer: [
       {
		_id:'1234',
		name:'lee',
		handle:'lee'
	},
      {
		_id:'8990',
		name:'xxx',
		handle:'xxx'
	}]
}

The index definition in this case would look like:

{
  "mappings": {
    "fields": {
      "_entityBuyer": {
        "fields": {
          "name": {
            "type": "autocomplete"
          }
        },
        "type": "embeddedDocuments"
      }
    }
  }
}

and the search query would be:

[
  {
    '$search': {
      'embeddedDocument': {
        'path': '_entityBuyer', 
        'operator': {
          'compound': {
            'should': [
              {
                'autocomplete': {
                  'path': '_entityBuyer.name', 
                  'query': 'lee'
                }
              }
            ]
          }
        }
      }
    }
  }
]

Let us know if you have further queries.

Regards
Aasawari