将单个关键字发送到 MongoDb 上的 Atlas Search 会比为每个查询发送不同的单词表现更好吗?

问题描述 投票:0回答:1

本质上发送一个关键字来搜索所有查询,我当前的实现基于各种查询参数的存在构建复合查询。鉴于数据集会增长并且查询数量也会增长,不确定哪种参数方法会执行得最好。

例如:

 let queryParams =`title=${keyword}&description=${keyword}&categorySearch=${keyword}&sortBy=${sortBy}&findCategory=${lookUpCategory}&seller=${seller}`;

let queryParams =`keyword=${keyword}`;

从而消除:

 if (
      req.query.soldBy ||
      req.query.seller ||
      req.query.description ||
      req.query.title ||
      req.query.categorySearch ||
      req.query.findCategory
    ) {
      const searchQuery: IProduct = {
        index: "default",
        compound: {
          must: [],
        },
      };

  if (req.query.seller) {
    searchQuery.compound.must.push({
      text: {
        query: req.query.seller,
        path: ["seller"],
        fuzzy: {
          maxEdits: 1,
        },
      },
    });
  }

  if (req.query.description) {
    searchQuery.compound.must.push({
      text: {
        query: req.query.description,
        path: ["description", "title", "categorySearch"],
        fuzzy: {
          maxEdits: 1,
        },
      },
    });
  }

等等...对于

  if (req.query.keyword) {
    searchQuery.compound.must.push({
      text: {
        query: req.query.keyword,
        path: ["description", "title", "categorySearch", "all the rest"],
        fuzzy: {
          maxEdits: 1,
        },
      },
    });
  }
mongodb mongoose mongodb-query mern
1个回答
0
投票

一般来说,越简单越好。

但这取决于您想要做什么,两个查询都会做不同的事情。

您的第一个查询必须在不同字段上使用,要求“关键字”位于所有字段中。必须是 AND

您的第二个查询只需要在其中一个字段中找到“关键字”(OR)。

https://www.mongodb.com/docs/atlas/atlas-search/compound/


编辑:

如果你想同时在不同的字段中搜索不同的关键字,最合适的方法是第一种方法,但为了使其不那么混乱,你可以使用for循环。

const LIST_OF_PARAMETERS = ["title","description"];

LIST_OF_PARAMETERS.forEach((param)=>{
 if (req.query[param]) must.push({"SEARCH OBJECT HERE"})
});

如果目标是在字段列表中搜索单个关键字,那么第二个选项更好。

© www.soinside.com 2019 - 2024. All rights reserved.