问题
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"
}'
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
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.
回答1:
Thanks to @nKandel for his comment it was really helpful.
The final query looks like:
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
来源:https://stackoverflow.com/questions/24734765/elasticsearch-filter-dont-work-with-aggregation