通過 elasticsearch-sql 使用 SQL 語句聚合查詢 Elasticsearch 獲取各種 buckets 桶
阿新 • • 發佈:2019-02-11
Elasticsearch 的 buckets(桶)包含 Histogram、Date Histogram、Range、Date Range、Terms、IPv4 Range、Significant Terms 等;
1.Histogram:
SELECT * FROM INDEX-2017-12 GROUP BY (histogram('alias'='log_date.mthAggs', 'interval'='1', 'field'='log_date.mth'))
{ "from" : 0, "size" : 0, "aggregations" : { "log_date.mthAggs" : { "histogram" : { "field" : "log_date.mth", "interval" : 1 } } } }
2.Date Histogram:
SELECT * FROM INDEX-2017-12 GROUP BY (date_histogram('format'='yyyy-MM', 'alias'='@timestampAggs', 'interval'='1M', 'field'='@timestamp'))
{ "from" : 0, "size" : 0, "aggregations" : { "@timestampAggs" : { "date_histogram" : { "field" : "@timestamp", "interval" : "1M", "format" : "yyyy-MM" } } } }
3.Range:
SELECT * FROM INDEX-2017-12 GROUP BY (range(log_date.mth,1,6,7,12))
{ "from" : 0, "size" : 0, "aggregations" : { "range(log_date.mth,1,6,7,12)" : { "range" : { "field" : "log_date.mth", "ranges" : [ { "from" : 1.0, "to" : 6.0 }, { "from" : 6.0, "to" : 7.0 }, { "from" : 7.0, "to" : 12.0 } ] } } } }
4.Date Range:
SELECT * FROM INDEX-2017-12 GROUP BY (date_range('format'='yyyy-MM-dd', 'alias'='dateRangeAggs', 'field'='@timestamp','2017-01-01','2017-06-01','now-1M','now-1w','now-2d','now'))
{
"from" : 0,
"size" : 0,
"aggregations" : {
"dateRangeAggs" : {
"date_range" : {
"field" : "@timestamp",
"ranges" : [ {
"from" : "2017-01-01",
"to" : "2017-06-01"
}, {
"from" : "2017-06-01",
"to" : "now-1M"
}, {
"from" : "now-1M",
"to" : "now-1w"
}, {
"from" : "now-1w",
"to" : "now-2d"
}, {
"from" : "now-2d",
"to" : "now"
} ],
"format" : "yyyy-MM-dd"
}
}
}
}
5.Terms:
SELECT * FROM INDEX-2017-12 GROUP BY (terms('alias'='methodAggs', 'field'='method', 'size'=6))
{
"from" : 0,
"size" : 0,
"aggregations" : {
"methodAggs" : {
"terms" : {
"field" : "method",
"size" : 6
}
}
}
}
6.IPv4 Range:
稍後補充...
7.Significant Terms:
暫時沒找到求 Significant Terms 的 SQL 語句,只能用原生 ES 查詢語句獲取了;
ES 原生查詢語句如下:
{
"size": 0,
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "*",
"analyze_wildcard": true
}
},
{
"range": {
"@timestamp": {
"gte": 1451297220869,
"lte": 1514455620869,
"format": "epoch_millis"
}
}
}
],
"must_not": []
}
},
"_source": {
"excludes": []
},
"aggs": {
"2": {
"significant_terms": {
"field": "log.client.system",
"size": 4
}
}
}
}