【问题标题】:elasticsearch filter don't work with aggregation弹性搜索过滤器不适用于聚合
【发布时间】:2014-07-14 10:44:46
【问题描述】:

我正在使用 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 并且城市是阿拉木图,然后我想对过滤后的数据进行一些聚合。

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
}

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

【问题讨论】:

标签: elasticsearch filtering aggregate


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-20
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 2019-09-10
    相关资源
    最近更新 更多