2018/2/13 ElasticSearch学习笔记三 自动映射以及创建自动映射模版,ElasticSearch聚合查询

终于把这些命令全敲了一遍,话说ELK技术栈L和K我今天花了一下午全部搞定,学完后还都是花式玩那种。。。E却学了四天(当然主要是因为之前上班一直没时间学,还有安装服务时出现的各种error真是让我扎心了,这绝对是我学编程以来针对某个特定技术花的时间最长的一次学习)。

#删除school索引
DELETE /school
#静态映射
#format日期格式默认:strict_date_optional_time||epoch_millis
PUT /school
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"student": {
"properties": {
"age": { "type": "long"},
"course": { "type": "text"},
"name": {"type": "keyword"},
"study_date": {"type": "date", "format": "yyyy-MM-dd"}
}
}
}
}

#日期格式不对无法写入
PUT /school/student/1
{
"name": "zhangsan",
"age": 25,
"course": "elasticsearch",
"study_date": "2017-06-15 23:00:00"
}
#可以写入
PUT /school/student/1
{
"name": "zhangsan",
"age": 25,
"course": "elasticsearch",
"study_date": "2017-06-15"
}

#动态映射
DELETE /school
PUT /school
{
"mappings": {
"student": {
"dynamic": "strict",
"properties": {
"age": { "type": "long"},
"course": { "type": "text"},
"name": {"type": "keyword"},
"study_date": {"type": "date", "format": "yyyy-MM-dd"},
"other": {
"type": "object",
"properties": {
"field01":{"type":"text"}
},
"dynamic": true
}
}
}
}
}
#不能动态增加字段,无法写入
POST /school/student
{
"name":"zhangsan",
"sex":"male"
}
#
POST /school/student/1
{
"name":"zhangsan",
"other":{
"field01":"value1",
"field02":"value2"
}
}

GET /school/_mapping

#mapping是不允许修改,但是可以新增字段类型
#在已建立的索引下,添加字段mapping
PUT /school/_mapping/student
{
"properties": {
"a_new_filed": {
"type": "keyword"
}
}
}

#在已建立的索引下,新增type的mapping
#注意,不同type下,相同的字段名的类型要保持一致
PUT /school/_mapping/new_type
{
"properties": {
"a_new_filed": {
"type": "keyword"
}
}
}

#在已建立的索引下,添加一个object类型字段的mapping
PUT /school/_mapping/student
{
"properties": {
"name_all": {
"properties":{
"first":{"type":"keyword"},
"last":{"type":"keyword"}
}
}
}
}
#在已建立的索引下,在object类型字段下添加子字段的mapping
PUT /school/_mapping/student
{
"properties": {
"name_all": {
"properties":{
"all":{"type":"keyword"}
}
}
}
}

#修改当前索引的字段mapping,增加ignore_above属性(注意:只有keyword类型有ignore_above)
PUT /school/_mapping/student
{
"properties": {
"name": {
"type":"keyword",
"ignore_above":100
}
}
}

# 获取某个索引的映射信息
GET /school/_mapping

#获取某个索引下某个type的映射信息
GET /school/_mapping/student

#获取某个索引下指定type的某个字段的mapping
GET /school/_mapping/student/field/name

#获取某个索引下指定type,多个字段的mapping
GET /school/_mapping/student/field/name,course

#获取某个索引下所有type,多个字段的mapping
GET /school/_mapping/field/name,course

#获取多个索引内的字段的mapping
GET /school,school2/_mapping/field/name,course

#获取多个索引下的mapping(通配符)
GET /sc*/_mapping/

#获取多个索引下多个type的mapping(通配符)
GET /sc*/_mapping/stud*

#获取多个索引下多个type的,多个字段的mapping(通配符)
GET /sc*/_mapping/stud*/field/na*

#获取集群内所有的映射信息
GET /_all/_mapping

#获取集群内多个type的映射信息
GET /_all/_mapping/student,student2

#获取集群内多个type的字段映射信息
GET /_all/_mapping/student,student2/field/name


#创建别名
PUT /school/_alias/school_info

#查看别名
GET /_alias
#查看某个索引下的别名
GET /school/_alias/

#添加移除别名
PUT /school_new
POST /_aliases
{
"actions": [
{
"remove": {
"index": "school",
"alias": "school_info"
}
},
{
"add": {
"index": "school_new",
"alias": "school_info"
}
}
]
}

#-----------------nested类型-----------------------
DELETE school
PUT school/class/1
{
"name": "xxx-class",
"users": [
{
"age": 25,
"name": "zhangsan"
},
{
"age": 26,
"name": "lisi"
}
]
}

#存储在ES中,是这个样子的
#{
# "users.age":[25,26],
# "users.name":["zhangsan","lisi"]
#}


GET school/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"users.age": 25
}
},
{
"match": {
"user.name": "zhangsan"
}
}
]
}
}
}
#使用nested类型

DELETE school
PUT school
{
"mappings": {
"class": {
"properties": {
"users": {
"type":"nested"
}
}
}
}
}
PUT school/class/1
{
"name": "xxx-class",
"users": [
{
"age": 25,
"name": "zhangsan"
},
{
"age": 26,
"name": "lisi"
}
]
}

GET school/_search
{
"query": {
"nested": {
"path": "users",
"query":{
"bool": {
"must": [
{ "match": { "users.age": 25 }},
{ "match": { "users.name":"zhangsan" }}
]
}
}
}
}
}


#-----------------Geo-point类型-----------------------
DELETE my_index
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}
#指定经纬度
PUT my_index/my_type/1
{
"text": "Geo-point as an object",
"location": { 
"lat": 41.12,
"lon": -71.34
}
}

#格式lat纬度,lon经度
PUT my_index/my_type/2
{
"text": "Geo-point as a string",
"location": "41.12,-71.34" 
}

PUT my_index/my_type/3
{
"text": "Geo-point as a geohash",
"location": "drm3btev3e86" 
}

#格式[lon经度,lat纬度]
PUT my_index/my_type/4
{
"text": "Geo-point as an array",
"location": [ -71.34, 41.12 ] 
}

GET my_index/_search
{
"query": {
"geo_bounding_box": { 
"location": {
"top_left": {
"lat": 42,
"lon": -71.4
},
"bottom_right": {
"lat": 40,
"lon": -71.3
}
}
}
}
}
#距离搜索
GET /my_index/_search
{
"query": {
"bool" : {
"must" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "200km",
"location" : {
"lat" : 40,
"lon" : -70
}
}
}
}
}
}

#特殊区域
GET /my_index/_search
{
"query": {
"bool" : {
"must" : {
"match_all" : {}
},
"filter" : {
"geo_polygon" : {
"location" : {
"points" : [
{"lat" : 40, "lon" : -80},
{"lat" : 50, "lon" : -75},
{"lat" : 40, "lon" : -70}
]
}
}
}
}
}
}

#-----------------默认mapping属性-----------------------

DELETE school

PUT school
{
"mappings": {
"_default_": { 
"_source": {
"enabled": true
}
},
"student": {
"_source": {
"enabled": false
},
"properties": {
"name": {
"type": "text",
"store": false,
"index": true
},
"course": {
"type": "text"
},
"study_date":{
"type":"date",
"format": "yyyy-MM-dd HH:mm:ss||strict_date_optional_time||epoch_millis"
}
}
}
}
}
GET school/_mapping

PUT /school/student/1
{
"name": "zhangsan",
"age": 25,
"course": "elasticsearch",
"study_date": "2017-06-15 23:00:00"
}

GET /school/student/1


#默认动态mapping映射
DELETE /school
PUT /school
{
"mappings": {
"_default_": {
"dynamic_templates": [
{
"message_field": {
"mapping": {
"store": false,
"type": "text"
},
"match": "*msg",
"match_mapping_type": "string"
}
},
{
"string_fields": {
"mapping": {
"ignore_above": 256,
"store": false,
"type": "keyword"
},
"match": "*",
"match_mapping_type": "string"
}
}
],
"properties":{}
}
}
}

PUT /school/student/1
{
"name": "zhangsan",
"age": 25,
"course": "elasticsearch",
"study_date": "2017-06-15 23:00:00"
}
GET /school/_mapping


#-----------------模板----------------
DELETE /school

PUT _template/student_template
{
"template": "sc*",
"settings": {
"number_of_shards": 2,
"number_of_replicas":2
},
"mappings": {
"student": {
"_source": {
"enabled": false
},
"properties": {
"name": {
"type": "text",
"store": false,
"index": true
},
"course": {
"type": "text"
},
"study_date":{
"type":"date",
"format": "yyyy-MM-dd HH:mm:ss||strict_date_optional_time||epoch_millis"
}
}
}
}
}

PUT /school/student/1
{
"name": "zhangsan",
"age": 25,
"course": "elasticsearch",
"study_date": "2017-06-15T20:30:50+0800"
}
GET /school/_mapping,_settings

#删除模板
DELETE /_template/student_template

#获取模板
GET /_template/student_template


#模板的优先级,order数字越大,优先级越高
DELETE /school
PUT /_template/template_1
{
"template" : "*",
"order" : 0,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"student" : {
"_source" : { "enabled" : false }
}
}
}

PUT /_template/template_2
{
"template" : "sc*",
"order" : 1,
"settings" : {
"number_of_shards" : 2
},
"mappings" : {
"student" : {
"_source" : { "enabled" : true }
}
}
}
PUT /school/student/1
{
"name": "zhangsan",
"age": 25,
"course": "elasticsearch",
"study_date": "2017-06-15T20:30:50+0800"
}

GET /school/_mapping,_settings

#动态模板
PUT /_template/template_3
{
"template": "my_*",
"order": 1,
"mappings": {
"_default_": {
"dynamic_templates": [
{
"message_field": {
"mapping": {
"store": false,
"type": "text"
},
"match": "*msg",
"match_mapping_type": "string"
}
},
{
"string_fields": {
"mapping": {
"ignore_above": 256,
"store": false,
"type": "keyword"
},
"match": "*",
"match_mapping_type": "string"
}
}
],
"properties": {}
}
}
}

DELETE /my_index
PUT /my_index/doc/1
{
"name":"zhangsan",
"msg":"this is a message!"
}

GET /my_index/_mapping


#-----------------分词器------------------
#内置标准分词器
POST _analyze
{
"analyzer": "standard",
"text": "I'am a Teacher 666."
}

#内置简单分词器
POST _analyze
{
"analyzer": "simple",
"text": "I'am a Teacher 666."
}

#内置停止词分词器
POST _analyze
{
"analyzer": "stop",
"text": "I'am a Teacher 666."
}


#测试自定义组合分词器
DELETE my_index
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_custom_analyzer": {
"type": "custom",
"tokenizer": "standard",
"char_filter": ["html_strip"],
"filter": ["lowercase","stop"]
}
}
}
}
}

POST my_index/_analyze
{
"analyzer": "my_custom_analyzer",
"text": "I'am a <b>Teacher</b> 666."
}


#设置mapping时为字段指定分词器
DELETE my_index

PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"std_english": { 
"type": "standard",
"stopwords": "_english_"
}
}
}
},
"mappings": {
"my_type": {
"properties": {
"my_text": {
"type": "text",
"analyzer": "standard", 
"fields": {
"stop": {
"type": "text",
"analyzer": "std_english" 
}
}
}
}
}
}
}

PUT my_index/my_type/1
{
"my_text":"today is the good"
}


#标准分析器查询
GET my_index/my_type/_search
{
"query": {
"match": {
"my_text": "the"
}
}
}
#测试用停止词分析器查询(查询不出来数据)
GET my_index/my_type/_search
{
"query": {
"match": {
"my_text.stop": "the"
}
}
}

#测试用了停止词分析器查询(可以查询出来数据)
GET my_index/my_type/_search
{
"query": {
"match": {
"my_text.stop": "good"
}
}
}

#查看自定义的分词器
POST my_index/_analyze
{
"analyzer": "std_english",
"text": "today is the good"
}


#动态更新分词器
POST /school/_close
PUT /school/_settings
{
"analysis" : {
"analyzer":{
"content":{
"type":"custom",
"tokenizer":"whitespace"
}
}
}
}
POST /school/_open

聚合

#elasticsearch-聚合bucket
DELETE cars
PUT cars
{
"mappings": {
"transactions": {
"properties": {
"price": {
"type":"long"
},
"color": {
"type":"keyword"
},
"make": {
"type":"keyword"
},
"sold": {
"type":"date"
}
}
}
}
}

POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }

#----------Filter Aggregation-------
#红色车的数量
POST /cars/transactions/_search?size=0
{
"aggs" : {
"red_cars" : {
"filter" : { "term": { "color": "red" } }
}
}
}

#----------Filters Aggregation-------
#统计红色车、蓝色车各多少个
POST /cars/transactions/_search
{
"size": 0,
"aggs" : {
"cars" : {
"filters" : {
"filters" : {
"red_cars" : { "match" : { "color" : "red" }},
"blue_cars" : { "match" : { "color" : "blue" }}
}
}
}
}
}

#统计红色车、蓝色车各多少个,并计算两种颜色车的平均价格
POST /cars/transactions/_search
{
"size": 0,
"aggs" : {
"cars" : {
"filters" : {
"filters" : {
"red_cars" : { "match" : { "color" : "red" }},
"blue_cars" : { "match" : { "color" : "blue" }}
}
},
"aggs" : {
"avg_price" : { "avg" : { "field" : "price" } }
}
}
}
}

#----------Date Histogram Aggregation-------
#每月销售多少台汽车
#interval参数: year, quarter, month, week, day, hour, minute, second
#interval参数:还可以写具体的时间,比如24h,90m
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"sales_over_time": {
"date_histogram": {
"field": "sold",
"interval": "month", 
"format": "yyyy-MM-dd" 
}
}
}
}

#指定周期90分钟
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"sales_over_time": {
"date_histogram": {
"field": "sold",
"interval": "90m", 
"format": "yyyy-MM-dd HH:mm:ss" 
}
}
}
}

#加入keyed参数,使返回的buckets不作为一个数组返回
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"sales_over_time": {
"date_histogram": {
"field": "sold",
"interval": "month", 
"format": "yyyy-MM-dd",
"keyed":true
}
}
}
}

#----------Date Range Aggregation-------
#按照售卖日期范围统计车辆数量
GET /cars/transactions/_search
{
"size": 0,
"aggs": {
"range": {
"date_range": {
"field": "sold",
"format": "yyyy-MM-dd",
"ranges": [
{"from": "now-36M/M"},
{"to": "now-24M/M"},
{"from": "now-36M/M","to": "now-12M/M"}
]
}
}
}
}

#加入keyed参数,使返回的buckets不作为一个数组返回,并指定key值
GET /cars/transactions/_search
{
"size": 0,
"aggs": {
"range": {
"date_range": {
"field": "sold",
"format": "yyyy-MM-dd",
"ranges": [
{"from": "now-36M/M","key":"36months"},
{"to": "now-24M/M","key":"2years_ago"},
{"from": "now-36M/M","to": "now-12M/M"}
],
"keyed":true
}
}
}
}

#按照售卖日期范围统计车辆数量,并计算该周期内的平均售卖价格
GET /cars/transactions/_search
{
"size": 0,
"aggs": {
"range": {
"date_range": {
"field": "sold",
"format": "yyyy-MM-dd",
"ranges": [
{"from": "now-36M/M","key":"36months"},
{"to": "now-24M/M","key":"2years_ago"},
{"from": "now-36M/M","to": "now-12M/M"}
],
"keyed":true
},
"aggs" : {
"avg_price" : { "avg" : { "field" : "price" } }
}
}
}
}

#----------Histogram Aggregation-------
#直方图,按照20000为区间进行分桶
GET /cars/transactions/_search
{
"size" : 0,
"aggs" : {
"colors" : {
"histogram" : {
"field" : "price",
"interval": 20000
}
}
}
}

#min_doc_count参数,限制桶内至少有几个才显示
GET /cars/transactions/_search
{
"size" : 0,
"aggs" : {
"colors" : {
"histogram" : {
"field" : "price",
"interval": 20000,
"min_doc_count": 1
}
}
}
}

#extended_bounds参数,扩展显示范围
GET /cars/transactions/_search
{
"size" : 0,
"aggs" : {
"colors" : {
"histogram" : {
"field" : "price",
"interval": 20000,
"extended_bounds": {
"min" : 0,
"max" : 200000
}
}
}
}
}

#增加排序,按照桶名降序
GET /cars/transactions/_search
{
"size" : 0,
"aggs" : {
"colors" : {
"histogram" : {
"field" : "price",
"interval": 20000,
"order" : { "_key" : "desc" }
}
}
}
}

#增加排序,按照统计数量排序
GET /cars/transactions/_search
{
"size" : 0,
"aggs" : {
"colors" : {
"histogram" : {
"field" : "price",
"interval": 20000,
"order" : { "_count" : "desc" }
}
}
}
}


#直方图,按照20000为区间进行分桶,并进行汇总
GET /cars/transactions/_search
{
"size" : 0,
"aggs":{
"price":{
"histogram":{ 
"field": "price",
"interval": 20000
},
"aggs":{
"price_sum": {
"sum": { 
"field" : "price"
}
}
}
}
}
}

#直方图,按照20000为区间进行分桶,并进行汇总
#按照子聚合的指标进行排序
GET /cars/transactions/_search
{
"size" : 0,
"aggs":{
"price":{
"histogram":{ 
"field": "price",
"interval": 20000,
"order":{ "price_sum.value" : "desc" }
},
"aggs":{
"price_sum": {
"sum": { 
"field" : "price"
}
}
}
}
}
}

#增加keyed参数
GET /cars/transactions/_search
{
"size" : 0,
"aggs":{
"price":{
"histogram":{ 
"field": "price",
"interval": 20000,
"order":{ "price_sum.value" : "desc" }
,"keyed":true
},
"aggs":{
"price_sum": {
"sum": { 
"field" : "price"
}
}
}

}
}
}
#----------Terms Aggregation-------
#按照某个字段的词条进行分桶
#在每个分片上先获取前几个数量最多的词条,然后再整体二次重排,所以可能会有误差
GET /cars/transactions/_search
{
"size": 0, 
"aggs" : {
"make_terms" : {
"terms" : {
"field" : "make"
}
}
}
}

#按照词条的字母顺序排序
GET /cars/transactions/_search
{
"size": 0, 
"aggs" : {
"make_terms" : {
"terms" : {
"field" : "make",
"order" : { "_term" : "asc" }
}
}
}
}

#min_doc_count:用于限制只提取出现次数大于多少次的词条
GET /cars/transactions/_search
{
"size": 0, 
"aggs" : {
"make_terms" : {
"terms" : {
"field" : "make",
"min_doc_count": 3
}
}
}
}

#使用脚本,进行修改field内容
GET /cars/transactions/_search
{
"size": 0, 
"aggs" : {
"make_terms" : {
"terms" : {
"script" : {
"inline": "'make:'+doc['make'].value",
"lang": "painless"
}
}
}
}
}
#同上
GET /cars/transactions/_search
{
"size": 0, 
"aggs" : {
"make_terms" : {
"terms" : {
"field" : "make",
"script" : {
"inline" : "'make: ' +_value",
"lang" : "painless"
}
}
}
}
}
#使用正则表达式过滤词条
GET /cars/transactions/_search
{
"size": 0, 
"aggs" : {
"make_terms" : {
"terms" : {
"field" : "make",
"include" : ".*o.*",
"exclude" : "f.*"
}
}
}
}

#使用精确指定的词条进行分桶
GET /cars/transactions/_search
{
"size": 0, 
"aggs" : {
"make_terms" : {
"terms" : {
"field" : "make",
"include" : ["mazda", "honda"]
}
}
}
}

#----------Range Aggregation-------

#按照指定的范围区间分桶,并计算数量
GET /cars/transactions/_search
{
"size": 0, 
"aggs" : {
"price_ranges" : {
"range" : {
"field" : "price",
"ranges" : [
{ "to" : 20000 },
{ "from" : 20000, "to" : 50000 },
{ "from" : 50000 }
]
}
}
}
}

#用script脚本指定field
GET /cars/transactions/_search
{
"size": 0, 
"aggs" : {
"price_ranges" : {
"range" : {
"script" : {
"lang": "painless",
"inline": "doc['price'].value"
},
"ranges" : [
{ "to" : 20000 },
{ "from" : 20000, "to" : 50000 },
{ "from" : 50000 }
]
}
}
}
}

#在分桶前,通过脚本更改值
GET /cars/transactions/_search
{
"size": 0, 
"aggs" : {
"price_ranges" : {
"range" : {
"field" :"price",
"script" : {
"lang": "painless",
"inline": "_value * params.rate",
"params" : {
"rate" : 2.5
}
},
"ranges" : [
{ "to" : 20000 },
{ "from" : 20000, "to" : 50000 },
{ "from" : 50000 }
]
}
}
}
}

#----------Global Aggregation-------
#用global来计算所有的文档
GET /cars/transactions/_search?size=0
{
"query" : {
"match" : { "make" : "honda" }
},
"aggs" : {
"all_makes" : {
"global" : {}, 
"aggs" : { 
"avg_price" : { "avg" : { "field" : "price" } }
}
},
"honda_make": { "avg" : { "field" : "price" } }
}
}

#验证一下global计算是否正确
GET /cars/transactions/_search?size=0
{
"query" : {
"match_all" : { }
},
"aggs" : {
"all_make": { "avg" : { "field" : "price" } }
}
}

#----------IP Range Aggregation-------
DELETE ips
PUT ips
{
"mappings": {
"transactions": {
"properties": {
"ip": {
"type":"ip"
}
}
}
}
}

POST /ips/doc/_bulk
{ "index": {}}
{ "ip" : "192.168.1.1"}
{ "index": {}}
{ "ip" : "192.168.1.10"}
{ "index": {}}
{ "ip" : "192.168.1.102"}
{ "index": {}}
{ "ip" : "192.168.1.150"}
{ "index": {}}
{ "ip" : "192.168.1.160"}
{ "index": {}}
{ "ip" : "192.168.1.250"}

#按照指定的ip范围分桶,并统计数量
GET /ips/doc/_search
{
"size": 0, 
"aggs" : {
"ip_ranges" : {
"ip_range" : {
"field" : "ip",
"ranges" : [
{"from" : "192.168.1.1" },
{"to" : "192.168.2.1" },
{"from" : "192.168.1.1","to" : "192.168.3.200" }
]
}
}
}
}

#通过子网掩码范围分桶
#192.168.1.0/24表示:192.168.1.1至192.168.1.254
#192.168.2.0/25:192.168.2.1至192.168.2.126
GET /ips/doc/_search
{
"size": 0, 
"aggs" : {
"ip_ranges" : {
"ip_range" : {
"field" : "ip",
"ranges" : [
{ "mask" : "192.168.1.0/24" },
{ "mask" : "192.168.2.0/25" }
]
}
}
}
}

#加入keyed参数
GET /ips/doc/_search
{
"size": 0, 
"aggs" : {
"ip_ranges" : {
"ip_range" : {
"field" : "ip",
"ranges" : [
{ "mask" : "192.168.1.0/24" },
{ "mask" : "192.168.2.0/25" }
],
"keyed": true
}
}
}
}

#----------Geo Distance Aggregation-------
DELETE /museums
PUT /museums
{
"mappings": {
"doc": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
}

POST /museums/doc/_bulk?refresh
{"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'Orsay"}

#指定坐标点多少距离范围内的分桶文档,默认单位:m(米)
POST /museums/_search?size=0
{
"aggs" : {
"rings_around_amsterdam" : {
"geo_distance" : {
"field" : "location",
"origin" : "52.3760, 4.894",
"ranges" : [
{ "to" : 100000 },
{ "from" : 100000, "to" : 300000 },
{ "from" : 300000 }
]
}
}
}
}


#指定单位为公里
#可以使用:mi (miles英里), in (inches英寸), yd (yards码尺), km (kilometers), cm (centimeters), mm (millimeters).
POST /museums/_search?size=0
{
"aggs" : {
"rings_around_amsterdam" : {
"geo_distance" : {
"field" : "location",
"origin" : "52.3760, 4.894",
"unit" : "km",
"ranges" : [
{ "to" : 100000 },
{ "from" : 100000, "to" : 300000 },
{ "from" : 300000 }
]
}
}
}
}

#指定距离模式
#distance_type:arc弧度(默认,精度高,计算准确),plane(性能更好,速度更快,但精度稍差)
POST /museums/_search?size=0
{
"aggs" : {
"rings" : {
"geo_distance" : {
"field" : "location",
"origin" : "52.3760, 4.894",
"unit" : "km",
"distance_type" : "plane",
"ranges" : [
{ "to" : 100 },
{ "from" : 100, "to" : 300 },
{ "from" : 300 }
]
}
}
}
}

#使用keyed
POST /museums/_search?size=0
{
"aggs" : {
"rings_around_amsterdam" : {
"geo_distance" : {
"field" : "location",
"origin" : "52.3760, 4.894",
"ranges" : [
{ "to" : 100000 },
{ "from" : 100000, "to" : 300000 },
{ "from" : 300000 }
],
"keyed": true
}
}
}
}

原文地址:https://www.cnblogs.com/yangfeiORfeiyang/p/8447690.html