文章目录
ElasticSearch Aggregation(四)
桶聚合
Geo-distance
地理距离聚合。工作在geo_point
类型字段的多桶聚合。在概念上与range
聚合非常相似。用户可以定义一个源点和一组距离范围内的桶。这个聚合会计算源点到每个文档的距离,并且根据距离范围来确定文档数据哪个桶(如果源点到文档的距离在桶的距离范围内,则该文档数据这个桶)。利用比较直观的说法就是,在地图上一个点,距离这个点0-1km
的文档被分到一个桶,1km-2km
分到一个桶,2km-3km
分到一个桶。
索引测试数据:
curl -X PUT "localhost:9200/museums?pretty" -H 'Content-Type: application/json' -d'
{
"mappings": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
'
curl -X POST "localhost:9200/museums/_bulk?refresh&pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":1}}
{"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
{"index":{"_id":2}}
{"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":3}}
{"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":4}}
{"location": "51.222900,4.405200", "name": "Letterenhuis"}
{"index":{"_id":5}}
{"location": "48.861111,2.336389", "name": "Musée du Louvre"}
{"index":{"_id":6}}
{"location": "48.860000,2.327000", "name": "Musée d\u0027Orsay"}
'
查询例子:
curl -X POST "localhost:9200/museums/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {
"rings_around_amsterdam": {
"geo_distance": {
"field": "location",
"origin": "52.3760, 4.894",
"ranges": [
{ "to": 100000 },
{ "from": 100000, "to": 300000 },
{ "from": 300000 }
]
}
}
}
}
'
响应:
{
...
"aggregations": {
"rings_around_amsterdam": {
"buckets": [
{
"key": "*-100000.0",
"from": 0.0,
"to": 100000.0,
"doc_count": 3
},
{
"key": "100000.0-300000.0",
"from": 100000.0,
"to": 300000.0,
"doc_count": 1
},
{
"key": "300000.0-*",
"from": 300000.0,
"doc_count": 2
}
]
}
}
}
被指定的字段的数据类型必须是geo_point
。它还可以保存geo_point字段的数组,在这种情况下,在聚合期间将考虑所有字段。原点可以接受geo_point类型支持的所有格式:
默认情况下,距离单位是m(米),但它也可以接受:mi(英里),in(英寸),yd(码),km(公里),cm(厘米),mm(毫米)。
curl -X POST "localhost:9200/museums/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {
"rings": {
"geo_distance": {
"field": "location",
"origin": "52.3760, 4.894",
"unit": "km",
"ranges": [
{ "to": 100 },
{ "from": 100, "to": 300 },
{ "from": 300 }
]
}
}
}
}
'
将 keyed 标志设置为 true 会将唯一的字符串键与每个存储桶关联,并将范围作为散列而不是数组返回:
curl -X POST "localhost:9200/museums/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {
"rings_around_amsterdam": {
"geo_distance": {
"field": "location",
"origin": "52.3760, 4.894",
"ranges": [
{ "to": 100000 },
{ "from": 100000, "to": 300000 },
{ "from": 300000 }
],
"keyed": true
}
}
}
}
'
响应:
{
...
"aggregations": {
"rings_around_amsterdam": {
"buckets": {
"*-100000.0": {
"from": 0.0,
"to": 100000.0,
"doc_count": 3
},
"100000.0-300000.0": {
"from": 100000.0,
"to": 300000.0,
"doc_count": 1
},
"300000.0-*": {
"from": 300000.0,
"doc_count": 2
}
}
}
}
}
还可以为每个范围自定义键:
curl -X POST "localhost:9200/museums/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
{
"aggs": {
"rings_around_amsterdam": {
"geo_distance": {
"field": "location",
"origin": "52.3760, 4.894",
"ranges": [
{ "to": 100000, "key": "first_ring" },
{ "from": 100000, "to": 300000, "key": "second_ring" },
{ "from": 300000, "key": "third_ring" }
],
"keyed": true
}
}
}
}
'
响应:
{
...
"aggregations": {
"rings_around_amsterdam": {
"buckets": {
"first_ring": {
"from": 0.0,
"to": 100000.0,
"doc_count": 3
},
"second_ring": {
"from": 100000.0,
"to": 300000.0,
"doc_count": 1
},
"third_ring": {
"from": 300000.0,
"doc_count": 2
}
}
}
}
}
global聚合
定义搜索执行上下文中所有文档的单个bucket。此上下文由您正在搜索的索引和文档类型定义,但不受搜索查询本身的影响。
GET my-index-000001/_search
{
"query": {
"match": {
"name": "li"
}
},
"aggs": {
"all_avg": {
"global": {},
"aggs": {
"age_avg": {
"avg": {
"field": "age"
}
}
}
},
"query_age_avg":{
"avg": {
"field": "age"
}
}
}
}
以上例子中,all_avg
聚合不受query
语句的影响,因为对象中指定了"global": {}
语句。query_age_avg
的聚合受到了query
的影响,因为它没有指定"global": {}
语句。
响应:
{
...
"aggregations" : {
"query_age_avg" : {
"value" : 27.5
},
"all_avg" : {
"doc_count" : 4,
"age_avg" : {
"value" : 29.5
}
}
}
}
histogram聚合
直方图聚合。基于多桶值源的聚合,该聚合应用于从文档中提取到的数值或者数值范围上。他在值上动态的构建指定间隔的桶。例如,如果文档中有一个价格字段,该字段是数值类型,那么我们可以配置此聚合动态构建5
元间隔的桶。当执行聚合的时候,每个文档的价格字段会被计算并且四舍五入到最近的桶内。
对于范围值,文档可以分为多个存储桶。第一个bucket是从范围的下限计算的,计算方法与计算单个值的bucket相同。最后一个bucket的计算方法与范围上限的计算方法相同,并且范围将计入介于两者之间的所有bucket(包括这两个bucket)。
inteval
必须为正小数,而offset
必须为[0,interval)
中的小数(大于等于0且小于interval的小数)
以下例子是根据产品的价格按照50
的间隔来分桶:
GET my-index-000001/_search