erez victor
erez victor

Reputation: 31

Mongoose with search autocomplete index

I want an autocomplete search on a name field in my collection on MongoDB Atlas. I've configured a search index in MongoDB atlas as following:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "foldDiacritics": false,
        "maxGrams": 8,
        "type": "autocomplete"
      }
    }
  }
}

and I'm trying to search via mongoose a substring in the entire collection like this:

collection.aggregate([
    {
      $search: {
        autocomplete: {
          path: 'name',
          query: query,
        },
      },
    },
    {
      $limit: 10,
    },
    {
      $project: {
        _id: 0,
        name: 1,
      },
    },
  ]);

I always get 0 results, does anyone can help me understand what am I doing wrong?

Thanks, Erez

Upvotes: 1

Views: 1051

Answers (1)

erez victor
erez victor

Reputation: 31

The problem was that I've changed the default index name (which is 'default') and in that case, I needed to specify the new index name in the query or change the index to be named 'default'. Didn't found any mention of that in the MongoDB documentation though.

Upvotes: 2

Related Questions