如何在 ElasticQuery 函数的过滤器内添加 should 子句

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

我有一个弹性查询,它在函数内部有一个过滤器。我需要添加一个条件,例如它应该与部门过滤器匹配,或者它应该与任何product_id 匹配,以便召回必须包含传递的product_ids。下面是我想在弹性召回中看到的product_ids,需要在ES查询的函数中添加。

{
    "terms": {
        "product_ids": [
            "166168",
            "753547",
            "156835",
            "90112"
         ]
     }
}

下面是实际的ES查询

  "track_total_hits": true,
  "_source": {},
  "query": {
    "boosting": {
      "positive": {
        "function_score": {
          "query": {},
          "boost_mode": "avg",
          "score_mode": "sum",
          "functions": [
            {
              "filter": {
                "match": {
                  "Departments": {
                    "query": "1/lipstick",
                    "operator": "OR",
                    "fuzzy_transpositions": true,
                    "auto_generate_synonyms_phrase_query": true,
                    "boost": 1
                  }
                }
              },
              "weight": 99
            }

//I need to add the product_ids terms here

          ]
        }
      },
      "negative": {},
      "negative_boost": "1.0E-4"
    }
  },
java spring-boot elasticsearch
1个回答
0
投票

您可以在

bool
子句中创建
filter
查询。

{
  "query": {
    "boosting": {
      "positive": {
        "function_score": {
          "query": {
            "match_all": {}
          },
          "boost_mode": "avg",
          "score_mode": "sum",
          "functions": [
            {
              "filter": {
                "bool": {
                  "should": [
                    {
                      "match": {
                        "Departments": {
                          "query": "1/lipstick",
                          "operator": "OR",
                          "fuzzy_transpositions": true,
                          "auto_generate_synonyms_phrase_query": true,
                          "boost": 1
                        }
                      }
                    },
                    {
                      "terms": {
                        "product_ids": [
                          "166168",
                          "753547",
                          "156835",
                          "90112"
                        ]
                      }
                    }
                  ]
                }
              },
              "weight": 99
            }
          ]
        }
      },
      "negative": {},
      "negative_boost": "1.0E-4"
    }
  }
}

您还可以添加多个函数,并且仅当文档与此处所述的给定过滤查询匹配时,只有一个可以选择应用该函数。

GET /_search
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "filter": {
            "match": {
              "Departments": {
                "query": "1/lipstick",
                "operator": "OR",
                "fuzzy_transpositions": true,
                "auto_generate_synonyms_phrase_query": true,
                "boost": 1
              }
            }
          },
          "weight": 99
        },
        {
          "filter": {
            "terms": {
              "product_ids": [
                "166168",
                "753547",
                "156835",
                "90112"
              ]
            }
          },
          "weight": 42
        }
      ],
      "boost_mode": "avg",
      "score_mode": "sum"
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.