How to specify custom index params in compound search atlas

router.get('/searchuser', userController.userNameCitySearchAutocomplete);

//autocomplete search on user name and city
exports.userNameCitySearchAutocomplete = async function (req, res) {
  try {
    const { userNameCityQueryparam } = req.query;
    console.log("search query param", userNameCityQueryparam);
    const agg = [
      {
        $search: {
          'compound': {
            "should": [{
              //search on user name
             index: "userName",
              autocomplete: {
                query: userNameCityQueryparam,
                path: 'name',
                fuzzy: {
                  maxEdits: 2,
                  prefixLength: 3
                }
              },
              //search on user city
              index: "userCity",
              autocomplete: {
                query: userNameCityQueryparam,
                path: 'city',
                fuzzy: {
                  maxEdits: 2,
                  prefixLength: 3
                }
              },
            }]
          }
        }
      }
    ]
    const response = await User.aggregate(agg);
    return res.json(response);
    // res.send(response);
  } catch (error) {
    console.log("autocomplete search error", error);
    return res.json([]);
  }
};

1 Like

Instead of two indexes, created one index on both these fields

Hi @Manoranjan_Bhol

  • You have created a single key by combining username and city and created an index on the key?
    OR
  • You specified username and city key while creating an index?

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.