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
}
}
}
}
2018/2/13 ElasticSearch学习笔记三 自动映射以及创建自动映射模版,ElasticSearch聚合查询的更多相关文章
- Elasticsearch学习笔记三
PS:前面两章已经介绍了ES的基础及REST API,本文主要介绍ES常用的插件安装及使用. Elasticsearch-Head Head是一个用于管理Elasticsearch的web前端插件,该 ...
- 学习笔记(三)--->《Java 8编程官方参考教程(第9版).pdf》:第十章到十二章学习笔记
回到顶部 注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法 ...
- ElasticSearch学习笔记(超详细)
文章目录 初识ElasticSearch 什么是ElasticSearch ElasticSearch特点 ElasticSearch用途 ElasticSearch底层实现 ElasticSearc ...
- VSTO学习笔记(三) 开发Office 2010 64位COM加载项
原文:VSTO学习笔记(三) 开发Office 2010 64位COM加载项 一.加载项简介 Office提供了多种用于扩展Office应用程序功能的模式,常见的有: 1.Office 自动化程序(A ...
- Elasticsearch学习笔记一
Elasticsearch Elasticsearch(以下简称ES)是一款Java语言开发的基于Lucene的高效全文搜索引擎.它提供了一个分布式多用户能力的基于RESTful web接口的全文搜索 ...
- elasticsearch学习笔记——相关插件和使用场景
logstash-input-jdbc学习 ES(elasticsearch缩写)的一大优点就是开源,插件众多.所以扩展起来非常的方便,这也造成了它的生态系统越来越强大.这种开源分享的思想真是与天朝格 ...
- ES6学习笔记<三> 生成器函数与yield
为什么要把这个内容拿出来单独做一篇学习笔记? 生成器函数比较重要,相对不是很容易理解,单独做一篇笔记详细聊一聊生成器函数. 标题为什么是生成器函数与yield? 生成器函数类似其他服务器端语音中的接口 ...
- Go语言学习笔记三: 常量
Go语言学习笔记三: 常量 定义常量 常量就是在声明后不能再修改的量. const x int = 100 const y string = "abc" const z = &qu ...
- openresty 学习笔记三:连接redis和进行相关操作
openresty 学习笔记三:连接redis和进行相关操作 openresty 因其非阻塞的调用,令服务器拥有高性能高并发,当涉及到数据库操作时,更应该选择有高速读写速度的redis进行数据处理.避 ...
随机推荐
- 【CC2530入门教程-增强版】基础技能综合实训案例(基础版)-上位机源码
[CC2530入门教程-增强版]基础技能综合实训案例(基础版)-上位机源码 广东职业技术学院 欧浩源 一.需求分析 按照指定参数打开串口,与测控终端建立数据传输通道,并根据应用要求实现程序逻辑,具体 ...
- [国嵌笔记][033-034][设置svc模式]
[设置svc模式] 设置CPU为SVC模式 1.因为初始化系统需要有很高的权限,SVC模式具有该权限,所以首先要使系统工作在SVC(0b10011)模式 2.设置cprs为0xd3(0b1101001 ...
- 最小生成数之Kruskal算法
描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用了--但是幸运的是,经过计算机的分析,小Hi已经筛选出了一些比较适合建造道路的路线,这个数量并没有特别的大. 所以问题变成 ...
- Element类型知识大全
Element类型 除了Document类型之外,Element类型就要算是Web编程中最常用的类型了.Element类型用于表现XML或HTML元素,提供了对元素标签名.子节点及特性的访问. 要 ...
- UE4 AsnycTask
使用AsnycTask可以将制定代码放在指定线程中执行,例如更新文理必须放在游戏线程. AsyncTask(ENamedThreads::GameThread, [=](){ updateT ...
- 您是不是奇怪为什么 <script> 标签中没有 type="text/javascript" 属性?
在 HTML5 中该属性不是必需的.JavaScript 是 HTML5 以及所有现代浏览器中的默认脚本语言!
- 阿里云Maven配置,Maven仓库配置,Maven镜像配置
阿里云Maven配置,Maven仓库配置,Maven镜像配置 ======================== 蕃薯耀 2018年1月29日 http://www.cnblogs.com/fanshu ...
- NumPy-矩阵部分
NumPy-矩阵部分 [TOC] NumPy 简介 numpy可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多. 安装NumPy pi ...
- 基于 HTML5 的 3D 工控隧道案例
隧道的项目我目前是第一次接触,感觉做起来的效果还蛮赞的,所以给大家分享一下.这个隧道项目的主要内容包括:照明.风机.车道指示灯.交通信号灯.情报板.消防.火灾报警.车行横洞.风向仪.COVI.微波车检 ...
- char (*p)[]和char *p[]的区别
理解的关键在于: 1. []的优先级高于*,(*p)[]理解为指向一个数组,*(p[])存放指针的数组 2. char (*p)[SIZE]:指向一维数组的指针,一维数组只能有SIZE个元素 char ...