"aggregations" : { "<aggregation_name>" : { "<aggregation_type>" : { <aggregation_body> } [,"aggregations" : { [<sub_aggregation>]+ } ]? } [,"<aggregation_name_2>" : { ... } ]* }
curl -XPUT "http://localhost:9200/sports/" -d' { "mappings": { "athlete": { "properties": { "birthdate": { "type": "date", "format": "dateOptionalTime" }, "location": { "type": "geo_point" }, "name": { "type": "string" }, "rating": { "type": "integer" }, "sport": { "type": "string" } } } } }' curl -XPOST "http://localhost:9200/sports/_bulk" -d' {"index":{"_index":"sports","_type":"athlete"}} {"name":"Michael", "birthdate":"1989-10-1", "sport":"Baseball", "rating": ["5", "4"], "location":"46.22,-68.45"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Bob", "birthdate":"1989-11-2", "sport":"Baseball", "rating": ["3", "4"], "location":"45.21,-68.35"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Jim", "birthdate":"1988-10-3", "sport":"Baseball", "rating": ["3", "2"], "location":"45.16,-63.58" } {"index":{"_index":"sports","_type":"athlete"}} {"name":"Joe", "birthdate":"1992-5-20", "sport":"Baseball", "rating": ["4", "3"], "location":"45.22,-68.53"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Tim", "birthdate":"1992-2-28", "sport":"Baseball", "rating": ["3", "3"], "location":"46.22,-68.85"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Alfred", "birthdate":"1990-9-9", "sport":"Baseball", "rating": ["2", "2"], "location":"45.12,-68.35"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Jeff", "birthdate":"1990-4-1", "sport":"Baseball", "rating": ["2", "3"], "location":"46.12,-68.55"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Will", "birthdate":"1988-3-1", "sport":"Baseball", "rating": ["4", "4"], "location":"46.25,-68.55" } {"index":{"_index":"sports","_type":"athlete"}} {"name":"Mick", "birthdate":"1989-10-1", "sport":"Baseball", "rating": ["3", "4"], "location":"46.22,-68.45"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Pong", "birthdate":"1989-11-2", "sport":"Baseball", "rating": ["1", "3"], "location":"45.21,-68.35"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Ray", "birthdate":"1988-10-3", "sport":"Baseball", "rating": ["2", "2"], "location":"45.16,-63.58" } {"index":{"_index":"sports","_type":"athlete"}} {"name":"Ping", "birthdate":"1992-5-20", "sport":"Baseball", "rating": ["4", "3"], "location":"45.22,-68.53"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Duke", "birthdate":"1992-2-28", "sport":"Baseball", "rating": ["5", "2"], "location":"46.22,-68.85"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Hal", "birthdate":"1990-9-9", "sport":"Baseball", "rating": ["4", "2"], "location":"45.12,-68.35"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Charge", "birthdate":"1990-4-1", "sport":"Baseball", "rating": ["3", "2"], "location":"46.12,-68.55"} {"index":{"_index":"sports","_type":"athlete"}} {"name":"Barry", "birthdate":"1988-3-1", "sport":"Baseball", "rating": ["5", "2"], "location":"46.25,-68.55" } {"index":{"_index":"sports","_type":"athlete"}} {"name":"Bank", "birthdate":"1988-3-1", "sport":"Golf", "rating": ["6", "4"], "location":"46.25,-68.55" } {"index":{"_index":"sports","_type":"athlete"}} {"name":"Bingo", "birthdate":"1988-3-1", "sport":"Golf", "rating": ["10", "7"], "location":"46.25,-68.55" } {"index":{"_index":"sports","_type":"athlete"}} {"name":"James", "birthdate":"1988-3-1", "sport":"Basketball", "rating": ["10", "8"], "location":"46.25,-68.55" } {"index":{"_index":"sports","_type":"athlete"}} {"name":"Wayne", "birthdate":"1988-3-1", "sport":"Hockey", "rating": ["10", "10"], "location":"46.25,-68.55" } {"index":{"_index":"sports","_type":"athlete"}} {"name":"Brady", "birthdate":"1988-3-1", "sport":"Football", "rating": ["10", "10"], "location":"46.25,-68.55" } {"index":{"_index":"sports","_type":"athlete"}} {"name":"Lewis", "birthdate":"1988-3-1", "sport":"Football", "rating": ["10", "10"], "location":"46.25,-68.55" }'
curl -XPOST "http://localhost:9200/sports/athlete/_search?pretty" -d' { "size": 0, "aggregations": { "the_name": { "terms": { "field": "sport" } } } }'
"size" : 0
(0 is replaced by Integer.MAX_VALUE automatically) says that we need all the documents without exception, in our case, speed is not important, but we must take into account that a more accurate result takes more time. { ... "aggregations" : { "the_name" : { "buckets" : [ { "key" : "baseball", "doc_count" : 16 }, { "key" : "golf", "doc_count" : 2 }, { "key" : "basketball", "doc_count" : 1 }, { "key" : "football", "doc_count" : 1 }, { "key" : "hockey", "doc_count" : 1 } ] } } }
curl -XPOST "http://localhost:9200/sports/athlete/_search?pretty" -d' { "size": 0, "aggregations": { "the_name": { "terms": { "field": "name", "order": { "rating_avg": "desc" } }, "aggregations": { "rating_avg": { "avg": { "field": "rating" } } } } } }'
"rating" : [10, 9]
) and easily calculates the average value. { ... "aggregations" : { "the_name" : { "buckets" : [ { "key" : "brady", "doc_count" : 1, "rating_avg" : { "value" : 10.0 } }, { "key" : "wayne", "doc_count" : 1, "rating_avg" : { "value" : 10.0 } }, { "key" : "james", "doc_count" : 1, "rating_avg" : { "value" : 9.0 } }, { "key" : "bingo", "doc_count" : 1, "rating_avg" : { "value" : 8.5 } }, ... {} ... { "key" : "duke", "doc_count" : 1, "rating_avg" : { "value" : 3.5 } }, { "key" : "bob", "doc_count" : 1, "rating_avg" : { "value" : 3.5 } } ] } } }
"script"
. For example: curl -XPOST "http://localhost:9200/sports/athlete/_search?pretty" -d' { "size": 0, "aggregations": { "age_ranges": { "range": { "script": "DateTime.now().year - doc[\"birthdate\"].date.year", "ranges": [ { "from": 22, "to": 25 } ] } } } }'
"46.12,-68.55"
curl -XPOST "http://localhost:9200/sports/athlete/_search?pretty" -d' { "size": 0, "aggregations": { "baseball_player_ring": { "geo_distance": { "field": "location", "origin": "46.12,-68.55", "unit": "mi", "ranges": [ { "from": 0, "to": 20 } ] }, "aggregations": { "sport": { "terms": { "field": "sport" }, "aggregations": { "rating_stats": { "stats": { "field": "rating" } } } } } } } } }'
{ ... "aggregations" : { "baseball_player_ring" : { "buckets" : [ { "key" : "*-20.0", "from" : 0.0, "to" : 20.0, "doc_count" : 13, "sport" : { "buckets" : [ { "key" : "baseball", "doc_count" : 8, "rating_stats" : { "count" : 14, "min" : 2.0, "max" : 5.0, "avg" : 3.357142857142857, "sum" : 47.0 } }, { "key" : "golf", "doc_count" : 2, "rating_stats" : { "count" : 4, "min" : 4.0, "max" : 10.0, "avg" : 6.75, "sum" : 27.0 } }, { "key" : "basketball", "doc_count" : 1, "rating_stats" : { "count" : 2, "min" : 8.0, "max" : 10.0, "avg" : 9.0, "sum" : 18.0 } }, { "key" : "football", "doc_count" : 1, "rating_stats" : { "count" : 1, "min" : 10.0, "max" : 10.0, "avg" : 10.0, "sum" : 10.0 } }, { "key" : "hockey", "doc_count" : 1, "rating_stats" : { "count" : 1, "min" : 10.0, "max" : 10.0, "avg" : 10.0, "sum" : 10.0 } } ] } } ] } } }
Source: https://habr.com/ru/post/227131/
All Articles