Need Help with filter on Autocomplete

Hello all !

I would like to perform autocompletion on the name but filtered on a specific city with mongoose and nodejs. I have a mongodb collection like this :

{
    "_id" : ObjectId("6007fd9d984e2507ad452cf3"),
    "name" : "John",
    "city" : "A",
},
{
    "_id" : ObjectId("6007ff6844d9e517e1ec0976"),
    "name" : "Jack",
    "city" : "B",
}

What i have done so far :
I have setup MongoDB Atlas with a Search Index (with the help of search doc) And set up the autocomplete like that :

router.get('/search', async (request, response) => {
  try {
    let result = await Client.aggregate([
      {
        "$search": {
          "autocomplete": {
            "query": `${request.query.term}`,
            "path": "name",
            "fuzzy": {
              "maxEdits": 2,
              "prefixLength": 3,
            },
          },
        },
      },
      {
        $limit: 3
      },
      {
        $project: {
          "_id": 0,
        }
      }
    ]);
    response.send(result);
  } catch (e) {
    response.status(500).send({message: e.message});
  }
});

In front-end, with autocompleteJs :

  const autoCompleteJS = new autoComplete({
    data: {
      src: async () => {
        const query = document.querySelector("#autoComplete").value;
        const source = await fetch(`${window.location.origin}/search?term=${query}`);
        const data = await source.json();
        return data;
      },
      key: ["name"],
    },
    trigger: {
      event: ["input", "focus"],
    },
    searchEngine: "strict",
    highlight: true,
  });

So far it is working well. But I don’t know how to make the autocomplete result filtered based on city. It seems that the documentation does not mention this. Do you have any leads.

Thank you

PS : I have tried with filter but it return me an empty array, i don’t know why :frowning:

        "$search": {
          "compound": {
            "filter" : [{
              "text" : { path: "city", query: "A" }
            }],
            "must": [{
              "autocomplete": {
                "query": `${request.query.term}`,
                "path": "name",
                "fuzzy": {
                  "maxEdits": 2,
                  "prefixLength": 3,
                },
              }
            }]
          }
        }

@ Rudy_Z
Have you resolved this?
I have the same issue, getting empty array

Can you please help

Hello, everyone! :wave:

@Rudy_Z , your code should work if Atlas Search index is setup properly.

I have used the following aggregation pipeline with $search autocomplete:

db.residents.aggregate([
  {
    $search: {
      compound: {
        filter: [
          {
            text: {
              path: 'city',
              query: 'n'
            }
          },
        ],
        must: [
          {
            autocomplete: {
              query: 'o',
              path: 'name'
            }
          }
        ],
      }
    }
  }
]);

On this dataset:

db.residents.insertMany([
  {
    _id: 'A',
    name: 'Yaroslav',
    city: 'N',
  },
  {
    _id: 'B',
    name: 'Vladislav',
    city: 'M',
  },
  {
    _id: 'C',
    name: 'Oleg',
    city: 'N',
  }
]);

Result (just as expected):

[ 
  { _id: 'C', name: 'Oleg', city: 'N' } 
]

Index configuration:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "city": {
        "type": "string"
      },
      "name": {
        "analyzer": "lucene.standard",
        "foldDiacritics": false,
        "maxGrams": 5,
        "minGrams": 1,
        "tokenization": "edgeGram",
        "type": "autocomplete"
      }
    }
  }
}