Skip to content

Instantly share code, notes, and snippets.

@sajidzaman
Last active August 17, 2017 07:06
Show Gist options
  • Save sajidzaman/009d1acb3f1e433c6e852cb249956c5f to your computer and use it in GitHub Desktop.
Save sajidzaman/009d1acb3f1e433c6e852cb249956c5f to your computer and use it in GitHub Desktop.
Aggregation in Elastic search
Each Aggregation makes buckets, it shows the number of documents
GET /vehicles/cars/_search
{
"aggs": {
"popular_cars" : {
"terms" : {
"field" : "make.keyword"
},
"aggs" : {
"avg_price": {
"avg": {
"field" : "price"
}
}
}
}
},
"sort" :[
]
}
GET /vehicles/cars/_search
{
"size": 0,
"query" :{
"match": {"color": "red"}
},
"aggs": {
"popular_cars" : {
"terms" : {
"field" : "make.keyword"
},
"aggs" : {
"stats_on_price": {
"stats": {
"field" : "price"
}
}
}
}
}
}
GET /vehicles/cars/_search
{
"query" :{
"match": {"color": "red"}
},
"aggs": {
"popular_cars" : {
"terms" : {
"field" : "make.keyword"
},
"aggs" : {
"avg_price": {
"avg": {
"field" : "price"
}
},
"max_price": {
"max": {
"field" : "price"
}
},
"min_price": {
"min": {
"field" : "price"
}
}
}
}
}
}
GET /vehicles/cars/_search
{
"size": 0,
"query" :{
"match": {"color": "red"}
},
"aggs": {
"popular_cars" : {
"terms" : {
"field" : "make.keyword"
},
"aggs" : {
"sold_date_ranges": {
"range": {
"field" : "sold",
"ranges": [
{ "from" : "2016-01-01", "to": "2016-05-18" },
{ "from" : "2016-05-08", "to": "2017-01-01" }
]
},
"aggs" :{
"avg_price" :{
"avg" : {
"field": "price"
}
}
}
}
}
}
}
}
GET /vehicles/cars/_search
{
"size": 0,
"query" :{
"match": {"color": "red"}
},
"aggs": {
"popular_cars" : {
"terms" : {
"field" : "make.keyword",
"order": {"avg_price": "desc"}
},
"aggs" :{
"avg_price" :{
"avg" : {
"field": "price"
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment