Understanding Elasticsearch Bool query

时间:2018-06-04 17:41:47

标签: elasticsearch

I am porting ( from ES 1.3 to ES 6) old query API's to use new Rest High Level Client APIshave following questions regarding the two queries mentioned below

  1. Are both the queries same. If not what is the difference ?
  2. If both queries are different, should I use the first query for and operation and to replicate the old AndFilterBuilder logic ( ES version 1.3 )

First Query :

{
  "bool" : {
    "filter" : [
      {
        "bool" : {
          "must" : [
            {
              "exists" : {
                "field" : "test",
                "boost" : 1.0
              }
            },
            {
              "term" : {
                "key" : {
                  "value" : "value",
                  "boost" : 1.0
                }
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

Second Query :

{
  "bool" : {
    "filter" : [
      {
        "exists" : {
          "field" : "test",
          "boost" : 1.0
        }
      },
      {
        "term" : {
          "key" : {
            "value" : "value",
            "boost" : 1.0
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

1 个答案:

答案 0 :(得分:0)

你需要得分吗?

如果忽略得分,它们是相同的,我会使用must而非must,因为这样可以进行缓存。

如果需要评分,请使用must

Elasticsearch文档以这种方式解释:

  

查询上下文中使用的查询子句回答了问题“此文档与此查询子句的匹配程度如何?”

     

在过滤器上下文中,查询子句回答问题“此文档是否与此查询子句匹配?”答案是简单的是或否 - 不计算分数。

filter使用查询上下文,npm i -g @schematics/angular 使用过滤器上下文。