【问题标题】:ElasticSearch : constant_score query vs function_score queryElasticSearch:constant_score 查询 vs function_score 查询
【发布时间】:2022-01-24 05:40:14
【问题描述】:

我最近将我的 ElasticSearch 版本从 5.3 版升级到了 5.6 版

 "query" : {
    "constant_score" : {
      "query" : {
        "bool" : {
          "must" : {
            "terms" : {
              "customerId" : [ "ASERFE", "7004567457" ]
            }
          },
          "must_not" : {
            "terms" : {
              "useCase" : [ "PAY", "COLLECT" ]
            }
          }
        },
        "bool" : {
          "must" : {
            "match" : {
               "cardProductGroupName" : {
                  "query" : "Pre-fill Test birthday Present",
                  "type" : "phrase"
                }
             }
          }
        }
      }
    }
  }

执行上面提到的查询给了我以下错误-

{"root_cause":[{"type":"parsing_exception","re​​ason":"[constant_score] 查询不支持 [query]","line":1,"col":37}]," type":"parsing_exception","re​​ason":"[constant_score] 查询不支持 [query]","line":1,"col":37}

所以,我搜索了解决方案并找到了这个 function_score 查询。在执行下面提到的查询时,我得到的结果与使用 constant_score 得到的结果相同。

"query" : {
    "function_score" : {
      "query" : {
        "bool" : {
          "must" : {
            "terms" : {
              "customerId" : [ "ASERFE", "7004567457" ]
            }
          },
          "must_not" : {
            "terms" : {
              "useCase" : [ "PAY", "COLLECT" ]
            }
          }
        },
         "bool" : {
          "must" : {
            "match" : {
               "groupName" : {
                  "query" : "Pre-fill Test birthday Present",
                  "type" : "phrase"
                }
             }
          }
        }
      },
      "functions" : [ {
        "script_score" : {
          "script" : "1"
        }
      } ],
      "boost_mode" : "replace"
    }
  }

所以我的问题是,这是否意味着带有 script 的 function_score : "1" 会给出与 constant_function 相同的结果?

【问题讨论】:

    标签: elasticsearch computer-science distributed-system aws-elasticsearch opensearch


    【解决方案1】:

    它确实会给出相同的结果,但如果它仍然为每个匹配的文档运行“脚本”,性能可能会更差。

    另一方面,constant_score still exists in 5.6 虽然您必须使用 filter+boost 而不是 query

    【讨论】:

    • 感谢@ilvar 的回答,使用constant_scorefilter+boost 肯定是一个正确的解决方案。但我的查询有matchPhraseQuery,据我所知,过滤器查询不支持过​​滤器内的 matchQuery 之类的东西。因此,我不能这样做。是否有任何 match 查询的替代方法可以与 constant_score 中的过滤器一起使用。
    • 你试过了吗?我手头没有 5.6,但我认为在 5.x 中应该可以在过滤器上下文中使用查询...
    猜你喜欢
    • 2018-12-11
    • 2016-02-01
    • 2018-02-12
    • 1970-01-01
    • 2011-02-10
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    相关资源
    最近更新 更多