elasticsearch过滤器不适用于聚合 [英] elasticsearch filter don't work with aggregation

查看:108
本文介绍了elasticsearch过滤器不适用于聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与elasticsearch一起使用,并创建以下映射,创建地理索引,用于比较的长字段(gte),以及用于识别性别的布尔值归档。

I am working with elasticsearch and create the following mapping, creating geo index, long field to compare it (gte) ,and boolean filed to identify gender.

curl -XDELETE 'http://localhost:9165/locations' && echo
curl -XPUT 'http://localhost:9165/locations' && echo
curl -XPUT 'http://localhost:9165/locations/location/_mapping' -d '
{
    "location": {
        "properties": {
            "location": {"type" : "geo_point"},
            "gender" : {"type" : "boolean"},
            "last_appearance": {"type": "long"}
        }
    }
}'  && echo "Mapping done"

然后我在索引中进行以下插入:

then I am doing the following insertion to index:

curl -XPUT 'http://localhost:9165/locations/location/1' -d '{
   "location":
   {
    "lat": "47.785346",
    "lon": "67.701857"
   },
    "category_id": "5",
    "gender": "true",
    "city": "Almaty",
    "last_appearance": "3333333",
    "venue_id": "5229774711d2a3ec9e333d00"
}'

最后,我尝试过滤所有数据,其中用户的最后一次出现大于某些测试值,在我的情况下,它将是222222222222222222,城市是阿拉木图,那么我想对过滤后的数据进行一些汇总。

finally I try to filter all data where last appearance of user is bigger then some test value, in my case it will be 222222222222222222 and city is Almaty, then I want to do some aggregation with filtered data.

curl -X GET http://localhost:9165/locations/location/_search -d '
{
    "filter" : {
      "and" : [
        {
          "range" :
          {
            "last_appearance" :
            {
                "gte" : "222222222222222222"
              }
          }
        },
        {
            "term" : {"city" : "Almaty"}
        }
      ]
    },

    "aggs" : {
        "venues" : {
            "terms" : { "field" : "venue_id"},
            "aggs": {
                "genders" : {
                    "terms" : {"field" : "gender"}
                }
            }
        }
    }
}' | python -mjson.tool

在这种情况下,我不想汇总任何内容,因为333333小于2222222222222222222,但是弹性开始聚合:

I don't want to aggregate anything in that case because 333333 is less then 2222222222222222222, but elastic starts aggregation:

{
    "_shards": {
        "failed": 0, 
        "successful": 1, 
        "total": 1
    }, 
    "aggregations": {
        "venues": {
            "buckets": [
                {
                    "doc_count": 1, 
                    "genders": {
                        "buckets": [
                            {
                                "doc_count": 1, 
                                "key": "true"
                            }
                        ]
                    }, 
                    "key": "5229774711d2a3ec9e333d00"
                }
            ]
        }
    }, 
    "hits": {
        "hits": [], 
        "max_score": null, 
        "total": 0
    }, 
    "timed_out": false, 
    "took": 1
}

请帮助我处理。感谢您阅读这个冗长的问题。

Please help me to deal with is. Thanks for reading this long question.

推荐答案

感谢@nKandel的评论,它确实很有帮助。

Thanks to @nKandel for his comment it was really helpful.

最终查询如下:

curl -X GET http://localhost:9165/locations/location/_search -d '
{
    "size" : 0,
    "aggregations" : {
        "cities" : {
            "filter" : {
                "term" : {
                    "city": "Almaty"
                }
            },
            "aggregations": {
                "lasts" : {
                    "filter" : {
                        "range" : {
                            "last_appearance" : {
                                "gte" : 100
                            }
                        }
                    },
                    "aggregations": {
                        "venues" : {
                            "terms" : { "field" : "venue_id"},
                            "aggregations": {
                                "genders" : {
                                    "terms" : {"field" : "gender"}
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
'| python -mjson.tool

这篇关于elasticsearch过滤器不适用于聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆