Elasticsearch (9) --- aggregate queries (Bucket polymerization)
Previous spoke Elasticsearch polymerization query Metric aggregation : Elasticsearch (8) --- aggregate queries (Metric polymerization)
说明
In this paper, with reference to Elasticsearch official document version 7.3. Bucket Aggregations
概念
: Bucket can be understood as a bucket, it goes through the contents of the document, who meet certain requirements to put in a bucket, bucket comparable points in the SQL Group by .
This blog talking about the key barrel Terms Aggregation
are: Filter Aggregation
, Histogram Aggregation
, Range Aggregation
, Date Aggregation
, .
First, create an index, data
1, to create an index
DELETE cars
PUT cars
{
"mappings": {
"properties": {
"price": {
"type":"long"
},
"color": {
"type":"keyword"
},
"brand": {
"type":"keyword"
},
"sellTime": {
"type":"date"
}
}
}
}
Property fields: price, color, brand, sales time
2, add index data
POST /cars/_bulk
{ "index": {}}
{ "price" : 80000, "color" : "red", "brand" : "BMW", "sellTime" : "2014-01-28" }
{ "index": {}}
{ "price" : 85000, "color" : "green", "brand" : "BMW", "sellTime" : "2014-02-05" }
{ "index": {}}
{ "price" : 120000, "color" : "green", "brand" : "Mercedes", "sellTime" : "2014-03-18" }
{ "index": {}}
{ "price" : 105000, "color" : "blue", "brand" : "Mercedes", "sellTime" : "2014-04-02" }
{ "index": {}}
{ "price" : 72000, "color" : "green", "brand" : "Audi", "sellTime" : "2014-05-19" }
{ "index": {}}
{ "price" : 60000, "color" : "red", "brand" : "Audi", "sellTime" : "2014-06-05" }
{ "index": {}}
{ "price" : 40000, "color" : "red", "brand" : "Audi", "sellTime" : "2014-07-01" }
{ "index": {}}
{ "price" : 35000, "color" : "blue", "brand" : "Honda", "sellTime" : "2014-08-12" }
3, to see if success
command
GET /_cat/count/cars?v
We can see that the index exists, and there are eight document data.
二、Terms Aggregation
7.3 official document : Terms Aggregation
概念
: The polymerization for each unique value of an item.
1, according to the sub-brand bucket
GET cars/_search?size=0
{
"aggs" : {
"genres" : {
"terms" : { "field" : "brand" }
}
}
}
Back to Results
2, points barrels barrels before only shows the number of documents 3
GET cars/_search?size=0
{
"aggs" : {
"cars" : {
"terms" : {
"field" : "brand",
"size" : 3
}
}
}
}
return
The number of documents can be seen from the figure in the first three buckets.
3, sub-bucket sort
GET cars/_search?size=0
{
"aggs" : {
"genres" : {
"terms" : {
"field" : "brand",
"order" : { "_count" : "asc" }
}
}
}
}
4, shows the number of document is larger than the tub 3
GET cars/_search?size=0
{
"aggs" : {
"brands" : {
"terms" : {
"field" : "brand",
"min_doc_count": 3
}
}
}
}
5, using a precisely specified entry points for the barrel
GET /cars/_search?size=0
{
"aggs" : {
"JapaneseCars" : {
"terms" : {
"field" : "brand",
"include" : ["BMW", "Audi"]
}
}
}
}
Here only to show some common, more Terms Aggregation cafe Tell me about it.
三、 Filter Aggregation
Official documents : the Filter Aggregation and Filters Aggregation
Filter概念
: Refers to a specific field and a specific value, it can be said to have been filtered on the basis of Terms Aggregation, only the specific value of the polymerization.
1, the filter for the BMW brand get buckets, barrels and find the average value
GET /cars/_search?size=0
{
"aggs" : {
"brands" : {
"filter" : { "term": { "brand": "BMW" } },
"aggs" : {
"avg_price" : { "avg" : { "field" : "price" } }
}
}
}
}
return
2, for the BMW brand filter to obtain or color is green bucket
Filters概念
: Filter Aggreagtion can specify only one filter, in response to only a single bucket. If you want only a plurality of specific values for the polymerization, using only Filter Aggreagtion multiple requests.
Filters Aggreagation can be used to solve the above problems, it can specify more filters, also can be polymerized to said plurality of specific values.
GET /cars/_search?size=0
{
"size": 0,
"aggs" : {
"cars" : {
"filters" : {
"filters" : {
"colorBucket" : { "match" : { "color" : "red" }},
"brandBucket" : { "match" : { "brand" : "Audi" }}
}
}
}
}
}
return
四、Histogram Aggreagtion
The official document : Histogram Aggreagtion
概念
Histogram polymerization Terms and similar, are data packets, in accordance with the difference value of the packet it is Terms Field, and Field Histogram can be grouped according to specified interval
1, according to the price range of 10,000 barrels points
GET /cars/_search?size=0
{
"aggs" : {
"prices" : {
"histogram" : {
"field" : "price",
"interval" : 10000
}
}
}
}
return
2, according to the price range of 10,000 barrels of points, and if the bucket is not the document is not displayed barrel
The above sub-barrel prices we can find no documents also show that in 5000 to 6000 to zero, if we want the bucket is not a document that is not displayed barrel
GET /cars/_search?size=0
{
"aggs" : {
"prices" : {
"histogram" : {
"field" : "price",
"interval" : 10000,
"min_doc_count" : 1
}
}
}
}
return
五、Range Aggregation
Official documents : the Range Aggregation
概念
: The range of parameters of the user passed as a tank, accordingly the polymerization. In the same request, a plurality of sets can pass range, each range as a bucket.
1, in accordance with sub-barrel price range
GET /cars/_search?size=0
{
"aggs" : {
"price_ranges" : {
"range" : {
"field" : "price",
"ranges" : [
{ "to" : 50000 },
{ "from" : 5000, "to" : 80000 },
{ "from" : 80000 }
]
}
}
}
}
return
We can also specify the name of the key
GET /cars/_search?size=0
{
"aggs" : {
"price_ranges" : {
"range" : {
"field" : "price",
"ranges" : [
{ "key" : "xiaoyu", "to" : 50000 },
{ "key" : "baohan", "from" : 5000, "to" : 80000 },
{ "key" : "dayu", "from" : 80000 }
]
}
}
}
}
return
六、 Date Aggregation
Official documents : a Date Histogram Aggregation and Date Range Aggregation
Date Histogram概念
For the polymerization in histogram format, the time data, characteristics substantially coincides with Histogram Aggregation.
1, show sales by month barrels per month
注意
This is not an official document interval but calendar_interval, but an error in accordance with this action, do not know why
POST /cars/_search?size=0
{
"aggs" : {
"sales_over_time" : {
"date_histogram" : {
"field" : "sellTime",
"interval" : "1M",
"format" : "yyyy-MM-dd"
}
}
}
}
return
2. A kit of parts specified time interval
Date Range概念
: Time-range for the data format of the polymerization, substantially coincides with the characteristic Range Aggreagtion.
POST /cars/_search?size=0
{
"aggs": {
"range": {
"date_range": {
"field": "sellTime",
"format": "MM-yyyy",
"ranges": [
{ "to": "now-10M/M" },
{ "from": "now-10M/M" }
]
}
}
}
}
The above means that a divided barrel 10 months ago, is divided into a pail after 10 months ago
reference
1, Elasticsearch core technology and combat --- Ruan Yiming (eBay Pronto platform technical director
2, version ES7.3 official aggregate query API
. 3, elasticsearch polymerization --Bucket Aggregations
. 4, ElasticSearch- polymerization bucket
我相信,无论今后的道路多么坎坷,只要抓住今天,迟早会在奋斗中尝到人生的甘甜。抓住人生中的一分一秒,胜过虚度中的一月一年!(14)