简体   繁体   English

聚合,查询上下文和过滤上下文在Elasticsearch 5.1中不起作用

[英]Aggregation, Query Context and filter Context not working in Elasticsearch 5.1

I am facing issue in migrating from elastic search 1.5 to 5.1. 我从弹性搜索1.5迁移到5.1时遇到问题。 Following is my elastic search - 1.5 Query: 以下是我的弹性搜索-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}}
            }
        }
    }
}

This query is giving me correct doc_count in aggregations. 此查询给我正确的doc_count聚合。 This doc_count is calculated over result set returned by query context and it ignores filter context. 此doc_count是根据查询上下文返回的结果集计算得出的,它会忽略过滤器上下文。

I have written same query in elastic search 5.1: 我在弹性搜索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}}
            }
        }
    }

}

But in elastic search 5.1, it is returning me wrong doc_count in aggregation. 但是在弹性搜索5.1中,聚合返回了错误的doc_count。 I think it is taking filter in query context and hence, it is returning wrong doc_cout. 我认为它正在查询上下文中使用过滤器,因此,它返回错误的doc_cout。 Can someone tell me correct way to separate query and filter in elastic search 5.1? 有人可以告诉我在弹性搜索5.1中分离查询和过滤器的正确方法吗?

Your 1.5 query uses post_filter which you have removed in your 5.1 query. 您的1.5查询使用了已在5.1查询中删除的post_filter

The equivalent query in ES 5.1 is the following ( filtered/filter simply gets replaced as bool/filter and the top-level filter renamed to post_filter ): ES 5.1中的等效查询如下( filtered/filter替换为bool/filter ,并将顶级filter post_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
          }
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM