如果我在elasticsearch的filter子句中使用query呢?

qvtsj1bj  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(2)|浏览(296)

如果我在elasticsearch的filter子句中使用query呢?es会计算分数吗?例如,案例1:

{
    "query": {
        "bool": {
            "filter": {
                "bool":{
                    "should":{

                    }
                }
            }
        }
    }
}

案例2:

{
    "query": {
        "bool": {
            "should": {
                "bool":{
                    "filte":{

                    }
                }
            }
        }
    }
}

es会计算这两种情况下的分数吗?

7lrncoxx

7lrncoxx1#

筛选子句(查询)必须出现在匹配的文档中。但是,与must不同的是,查询的分数将被忽略。filter子句是在filter上下文中执行的,这意味着评分被忽略,子句被考虑用于缓存。
请参阅有关bool查询的elasticsearch文档,以了解更多信息
添加索引数据、搜索查询和搜索结果的工作示例
索引数据:

{
  "name": "milk",
  "cost": 40
}
{
  "name": "bread",
  "cost": 55
}

搜索查询1:
在这种情况下 bool 查询被 Package 在外部 filter 子句,所以 should 忽略子句

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": {
            "match": {
              "name": "bread"
            }
          }
        }
      }
    }
  }
}

搜索结果1:

"hits": [
      {
        "_index": "64505740",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.0,
        "_source": {
          "name": "bread",
          "cost": 55
        }
      }
    ]

搜索查询2:
在这种情况下,内部bool查询被 Package 在 filter 子句,所以外部 bool should 条款,不会对分数产生任何影响

{
  "query": {
    "bool": {
      "should": {
        "bool": {
          "filter": {
            "term": {
              "name": "bread"
            }
          }
        }
      }
    }
  }
}

搜索结果2:

"hits": [
      {
        "_index": "64505740",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.0,
        "_source": {
          "name": "bread",
          "cost": 55
        }
      }
    ]

所以两个搜索查询都将返回一个 0.0 分数,表示由于 filter 条款

ohfgkhjo

ohfgkhjo2#

在elasticsearch中,filter部分下的每个查询都不会参与分数计算。这意味着在两个查询中,如果在过滤器中添加逻辑,elasticsearch将不会计算分数。但是如果你在“必须”、“应该”或“不应该”部分添加了部分逻辑,elasticsearch将计算分数。

相关问题