Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save divideby0/57a0e3dbedba2d1f25380627f1029b92 to your computer and use it in GitHub Desktop.
Save divideby0/57a0e3dbedba2d1f25380627f1029b92 to your computer and use it in GitHub Desktop.
# Clear our example index
DELETE /vehicle
# Post a few car records with make, model and year
POST /vehicle/car/_bulk
{"index":{}}
{"make":"Saturn","model":"LW","year":2005}
{"index":{}}
{"make":"Honda","model":"Civic","year":2005}
{"index":{}}
{"make":"Honda","model":"Civic","year":2007}
{"index":{}}
{"make":"Honda","model":"Accord","year":2012}
{"index":{}}
{"make":"Saturn","model":"Ion","year":2001}
{"index":{}}
{"make":"Toyota","model":"Prius","year":2012}
{"index":{}}
{"make":"Toyota","model":"Corolla","year":2007}
{"index":{}}
{"make":"Ford","model":"Focus","year":2005}
# Display simple term aggregations for each field
GET /vehicle/car/_search
{
"size": 10,
"aggs": {
"make": {
"terms": {
"field": "make"
}
},
"model": {
"terms": {
"field": "model"
}
},
"year": {
"terms": {
"field": "year"
}
}
}
}
# If "honda" and "saturn" are selected for the "make" field filter...
#
# * In the result set, return the union of of cars with either "honda" OR "saturn"
# as the make
# * For the make aggregation, apply a pipeline that a) sets the global aggregation to
# escape the context of primary query filter b) aggregates the resulting documents by
# the "make" term. We label the nested aggregations with an underscore for convenience
# when parsing the result.
# * For the model and year aggregations, just do a simple terms agg as we can apply the
# primary query filters
GET /vehicle/car/_search
{
"size": 10,
"query": {
"constant_score": {
"filter": {
"terms": {
"make": ["honda", "saturn"]
}
}
}
},
"aggs": {
"make": {
"global": {},
"aggs": {
"_": {
"terms": {
"field": "make"
}
}
}
},
"model": {
"terms": {
"field": "model"
}
},
"year": {
"terms": {
"field": "year"
}
}
}
}
# If "honda" and "saturn" are selected for "make" and "2005" and "2001" are selected
# for "year"...
#
# * In the result set, return all cars that are either "honda" or "saturn" and
# made either in "2001" or "2005"
# * For the make aggregation, apply a pipeline that a) sets the global aggregation to
# escape the normal query context b) filters the result to match only vehicles
# made in "2001" or "2005", c) aggregates the resulting documents by the "make" term.
# * For the model aggregation, just do a simple terms agg to apply the primary query
# filters
# * For the year aggregation, apply a pipeline that a) sets the global aggregation to
# escape the normal query context b) filters the result to match only vehicles
# with a make of "honda" or "saturn", c) aggregates the resulting documents by the
# "year" term
GET /vehicle/car/_search
{
"size": 10,
"query": {
"constant_score": {
"filter": {
"and": {
"filters": [
{
"terms": {
"make": ["honda", "saturn"]
}
},
{
"terms": {
"year": [2001, 2005]
}
}
]
}
}
}
},
"aggs": {
"make": {
"global": {},
"aggs": {
"_": {
"filter": {
"terms": {
"year": [2001, 2005]
}
},
"aggs": {
"_": {
"terms": {
"field": "make"
}
}
}
}
}
},
"model": {
"terms": {
"field": "model"
}
},
"year": {
"global": {},
"aggs": {
"_": {
"filter": {
"terms": {
"make": ["honda", "saturn"]
}
},
"aggs": {
"_": {
"terms": {
"field": "year"
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment