Autocomplete on nested array of documents

I’m playing with the schools collection in the nested array query example in the Atlas Search Playground, and I’m attempting to user an autocomplete search to retrieve every teacher whose first or last name begins with “Jo”.

I’m using this to create my index:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "teachers": {
        "fields": {
          "first": [{
            "analyzer": "lucene.whitespace",
            "type": "autocomplete"
          },
          {
            "type": "string"
          }],
          "last": [{
            "analyzer": "lucene.whitespace",
            "type": "autocomplete"
          },
          {
            "type": "string"
          }]
        },
        "type": "embeddedDocuments"
      }
    }
  },
  "storedSource": {
    "include": [
      "teachers.first",
      "teachers.last"
    ]
  }
}

And this is my query:

[{
  "$search": {
    "embeddedDocument": {
      "path": "teachers",
      "operator": {
        "compound": {
          "should": [
            {
              "autocomplete":
              {
                "query": "John",
                "path": "teachers.first",
                "tokenOrder": "sequential"
              }
            },
            {
              "autocomplete":
              {
                "query": "John",
                "path": "teachers.last",
                "tokenOrder": "sequential"
              }
            }
          ]
        }
      }
    },
    "returnStoredSource": true
  }
}]

Instead of retrieving only teachers with a first or last name starting with “Jo”, I’m getting every teacher in the school, if one teacher is a match. For example:

[
  {
    "_id": 1,
    "teachers": [
      {
        "first": "Jane",
        "last": "Earwhacker"
      },
      {
        "first": "John",
        "last": "Smith"
      }
    ]
  },
  {
    "_id": 2,
    "teachers": [
      {
        "first": "Jane",
        "last": "Smith"
      },
      {
        "first": "John",
        "last": "Redman"
      }
    ]
  }
]

Thanks for any help on this,
Mike

$search returns top-level/main documents that match the criteria. In your playground example, there is only one document which matches the criteria and thus it is returned. See the tip at the bottom of this page about extracting matching embedded documents - https://www.mongodb.com/docs/atlas/atlas-search/embedded-document/

Thanks for your reply, I can add a $project stage and use $filter to return only teachers that match my query. But I guess the autocomplete search isn’t really helping me now, since the $search stage returns the entire top-level document. Now $project/$filter is doing the work.

It seems I should omit the $search stage - maybe autocomplete isn’t the right approach on an array of embedded documents. Or am I missing something?

Thanks,
Mike

Data modeling for this kind of search use case might best be done by creating a collection of the nested documents as top-level documents, then do suggest on those. The best approach needs to factor in the scale and end result needed. But if you want Teachers to be suggested, Teachers make sense to be the main data model.