如何在elasticsearch查询中添加更多查询?

ftf50wuq  于 5个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(74)
const searchParams = {
        index: "products", // Replace with your actual Elasticsearch index name
        body: {
          from: offset,
          size: limit,
          query: {
            bool: {
              should: [
                {
                  wildcard: {
                    name: `*${fullTextSearch}*`,
                  },
                },
                {
                  wildcard: {
                    code: `*${fullTextSearch}*`,
                  },
                },
              ],
            },
          },
        },
      };
 if (category) {
   //add and query ( category = category)
}

字符串
如果category!= null,我想将查询类别添加到此searchParams中
如何在elasticsearch查询中添加更多查询?

6ju8rftf

6ju8rftf1#

你可以这样做:

const searchParams = {
        index: "products", // Replace with your actual Elasticsearch index name
        body: {
          from: offset,
          size: limit,
          query: {
            bool: {
              filter: [],
              should: [
                {
                  wildcard: {
                    name: `*${fullTextSearch}*`,
                  },
                },
                {
                  wildcard: {
                    code: `*${fullTextSearch}*`,
                  },
                },
              ],
            },
          },
        },
      };
 }

 if (category) {
   searchParams.body.query.filter.push({'term': {'category': category}})
 }

字符串

相关问题