【问题标题】:Elasticsearch should query without computing relevance (_score)Elasticsearch 应该在不计算相关性的情况下进行查询 (_score)
【发布时间】:2018-02-21 20:14:51
【问题描述】:

我正在创建对两个字段进行操作的过滤查询。我想避免通过 Elasticsearch 计算相关性。如何在不移动到查询上下文的情况下实现 OR 语句。

我的简化模型有两个布尔字段:

{
   is_opened,
   is_send
}

我想用逻辑准备查询:

(is_opened == true AND is_send == true) OR (is_opened == false) 

换句话说,我想排除带有字段的文档:

is_opened == true AND is_send == false

我的查询是这样的:

GET documents/default/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool":{
            "must":[
                {"term": {"is_opened":true}},
                {"term": {"is_send":true}}
              ]
          }
        },
        {
          "bool":{
            "must":[
                {"term": {"is_opened":false}}
              ]
          }
        }
      ]
    }
  }
}

从逻辑上讲,它按我的预期工作,但 Elasticsearch 计算 相关性。 我不需要它,因为最后我按另一个字段对结果进行排序,所以它是优化查询的地方。 我问这个是因为Frequently used filters will be cached automatically by Elasticsearch, to speed up performance.

我的结果计算了 _score 字段,所以我认为上面的查询是在查询上下文中执行的,所以 Elasticsearch 不会自动缓存它。

将来我想创建对 status 字段进行操作的查询,其中逻辑会更复杂。我仍然需要知道如何阻止计算_score

我注意到将 should 更改为 filter 会阻止计算 _score 但可以作为 must 运算符。是否可以改变 filter 的行为?

是否可以使用其他查询? 如何强制Elasticserach停止计算_score

【问题讨论】:

    标签: performance elasticsearch dsl


    【解决方案1】:

    只需将您的查询包装在constant_score query

    GET documents/default/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "bool": {
              "should": [
                {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "is_opened": true
                        }
                      },
                      {
                        "term": {
                          "is_send": true
                        }
                      }
                    ]
                  }
                },
                {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "is_opened": false
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    【讨论】:

    • 我尝试使用它,但我的语法错误。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-05-21
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多