elasticsearch过滤器不支持聚合

时间:2014-07-14 10:44:47

标签: elasticsearch filtering aggregate

我正在使用elasticsearch并创建以下映射,创建地理索引,长字段来比较它(gte)和布尔字段来识别性别。

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"

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

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并且city是Almaty,然后我想用过滤数据进行一些聚合。

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,但弹性开始聚合:

{
    "_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
}

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

1 个答案:

答案 0 :(得分:1)

感谢@nKandel的评论,这真的很有帮助。

最终查询如下:

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