Skip to content

Instantly share code, notes, and snippets.

@spinscale
Created March 16, 2016 15:47
Show Gist options
  • Save spinscale/dad449d73cba68cfa224 to your computer and use it in GitHub Desktop.
Save spinscale/dad449d73cba68cfa224 to your computer and use it in GitHub Desktop.
Aggregations using filtering
DELETE /hotels
POST /hotels/hotel/_bulk
{ "index" : { "_id" : 1 }}
{ "features": [ "wifi", "parking" ], "stars" : 1 }
{ "index" : { "_id" : 2 }}
{ "features": [ "pool", "parking" ], "stars" : 4 }
{ "index" : { "_id" : 3 }}
{ "features": [ "wifi", "pool" ], "stars" : 5 }
GET /hotels/_search
{
"query": {
"match_all": {}
},
"aggs": {
"stars": {
"terms": {
"field": "stars",
"size": 10
}
},
"features": {
"terms": {
"field": "features",
"size": 10
}
}
}
}
GET /hotels/_search
{
"query": {
"match_all": {}
},
"post_filter": {
"term": {
"features": "wifi"
}
},
"aggs": {
"stars": {
"terms": {
"field": "stars",
"size": 10
}
},
"features": {
"terms": {
"field": "features",
"size": 10
}
}
}
}
GET /hotels/_search
{
"query": {
"bool": {
"filter": {
"term": {
"features": "wifi"
}
}
}
},
"aggs": {
"stars": {
"terms": {
"field": "stars",
"size": 10
}
},
"features": {
"terms": {
"field": "features",
"size": 10
}
}
}
}
GET /hotels/_search
{
"query": {
"match_all": {}
},
"aggs": {
"stars": {
"filter": {
"term": {
"features": "wifi"
}
},
"aggs": {
"terms_agg": {
"terms": {
"field": "stars",
"size": 10
}
}
}
},
"features": {
"terms": {
"field": "features",
"size": 10
}
}
}
}
PUT /hotels/.hotel/1
{ "a":"b" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment