【问题标题】:Aggregation, Query Context and filter Context not working in Elasticsearch 5.1聚合、查询上下文和过滤上下文在 Elasticsearch 5.1 中不起作用
【发布时间】:2017-09-12 22:53:12
【问题描述】:

我在从弹性搜索 1.5 迁移到 5.1 时遇到问题。 以下是我的弹性搜索 - 1.5 查询:

{
    "_source":["_id","spotlight"],
    "query":{
        "filtered":{
            "filter":{
                "and":[
                    {"term":{"gender":"female"}},
                    {"range":{"lastlogindate":{"gte":"2016-10-19 12:39:57"}}}
                ]
            }
        }
    },
    "filter":{
        "and":[
            {"term":{"maritalstatus":"1"}}
        ]
    },
    "sort":[{"member2_dummy7":{"order":"desc"}}],
    "size":"0",
    "aggs": {

        "maritalstatus": {

            "filter": {},
            "aggs" : {

                "filtered_maritalstatus": {"terms":{"field":"maritalstatus","size":5000}}
            }
        }
    }
}

此查询在聚合中为我提供了正确的 doc_count。此 doc_count 是根据查询上下文返回的结果集计算得出的,它会忽略过滤器上下文。

我在弹性搜索 5.1 中编写了相同的查询:

{
    "_source":["_id","spotlight"],
    "query":{
        "bool":{
            "must":[
                {"term":{"gender":"female"}},
                {"range":{"lastlogindate":{"gte":"2016-10-19 12:39:57"}}}
            ],
            "filter":{
                "bool":{
                    "must":[
                        {"term":{"maritalstatus":"1"}}
                    ]
                }
            }
        }
    },
    "sort":[{"member2_dummy7":{"order":"DESC"}}],
    "size":"0",
    "aggs": {

        "maritalstatus": {

            "filter": {},
            "aggs" : {

                "filtered_maritalstatus": {"terms":{"field":"maritalstatus","size":5000}}
            }
        }
    }

}

但在弹性搜索 5.1 中,它在聚合中返回错误的 doc_count。我认为它在查询上下文中使用过滤器,因此它返回错误的 doc_cout。有人能告诉我在弹性搜索 5.1 中分离查询和过滤的正确方法吗?

【问题讨论】:

  • 检查您在 1.5 中的第一个术语查询,您正在寻找 female,在 5.1 中您正在寻找 Female。这在term 查询中很重要。因此,将5.1 中的术语查询从Female 更改为female 并检查结果。另请查看5.1 查询中的时间戳5 seconds 大于1.5 查询中的时间戳。

标签: elasticsearch aggregation elasticsearch-5


【解决方案1】:

您的 1.5 查询使用您在 5.1 查询中删除的 post_filter

ES 5.1 中的等效查询如下(filtered/filter 被简单地替换为 bool/filter 并且顶级 filter 重命名为 post_filter):

{
  "_source": [
    "_id",
    "spotlight"
  ],
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "gender": "female"
          }
        },
        {
          "range": {
            "lastlogindate": {
              "gte": "2016-10-19 12:39:57"
            }
          }
        }
      ]
    }
  },
  "post_filter": {
    "term": {
      "maritalstatus": "1"
    }
  },
  "sort": [
    {
      "member2_dummy7": {
        "order": "desc"
      }
    }
  ],
  "size": "0",
  "aggs": {
    "maritalstatus": {
      "filter": {},
      "aggs": {
        "filtered_maritalstatus": {
          "terms": {
            "field": "maritalstatus",
            "size": 5000
          }
        }
      }
    }
  }
}

【讨论】:

  • 很高兴它有帮助!
猜你喜欢
  • 2017-05-06
  • 2016-11-24
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 2015-07-02
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多