Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,不少概念与MySQL相似的。html
对比关系:mysql
索引(indices)--------------------------------Databases 数据库sql
类型(type)-----------------------------Table 数据表数据库
文档(Document)----------------Row 行json
字段(Field)-------------------Columns 列数组
详细说明:服务器
概念 | 说明 |
---|---|
索引库(indices) | indices是index的复数,表明许多的索引, |
类型(type) | 类型是模拟mysql中的table概念,一个索引库下能够有不一样类型的索引,好比商品索引,订单索引,其数据格式不一样。不过这会致使索引库混乱,所以将来版本中会移除这个概念 |
文档(document) | 存入索引库原始的数据。好比每一条商品信息,就是一个文档 |
字段(field) | 文档中的属性 |
映射配置(mappings) | 字段的数据类型、属性、是否索引、是否存储等特性 |
是否是与Lucene和solr中的概念相似。app
另外,在SolrCloud中,有一些集群相关的概念,在Elasticsearch也有相似的:elasticsearch
要注意的是:Elasticsearch自己就是分布式的,所以即使你只有一个节点,Elasticsearch默认也会对你的数据进行分片和副本操做,当你向集群添加新数据时,数据也会在新加入的节点中进行平衡。分布式
Elasticsearch采用Rest风格API,所以其API就是一次http请求,你能够用任何工具发起http请求
建立索引的请求格式:
请求方式:PUT
请求路径:/索引库名
请求参数:json格式:
{ "settings": { "number_of_shards": 3, "number_of_replicas": 2 } }
咱们先用RestClient来试试
响应:
能够看到索引建立成功了。
kibana的控制台,能够对http请求进行简化,示例:
至关因而省去了elasticsearch的服务器地址
并且还有语法提示,很是舒服。
语法
Get请求能够帮咱们查看索引信息,格式:
GET /索引库名
或者,咱们可使用*来查询全部索引库配置:
删除索引使用DELETE请求
语法
DELETE /索引库名
示例
再次查看heima2:
固然,咱们也能够用HEAD请求,查看索引是否存在:
索引有了,接下来确定是添加数据。可是,在添加数据以前必须定义映射。
什么是映射?
映射是定义文档的过程,文档包含哪些字段,这些字段是否保存,是否索引,是否分词等
只有配置清楚,Elasticsearch才会帮咱们进行索引库的建立(不必定)
语法
请求方式依然是PUT
PUT /索引库名/_mapping/类型名称 { "properties": { "字段名": { "type": "类型", "index": true, "store": true, "analyzer": "分词器" } } }
ik_max_word
即便用ik分词器示例
发起请求:
PUT heima/_mapping/goods { "properties": { "title": { "type": "text", "analyzer": "ik_max_word" }, "images": { "type": "keyword", "index": "false" }, "price": { "type": "float" } } }
响应结果:
{ "acknowledged": true }
语法:
GET /索引库名/_mapping
示例:
GET /heima/_mapping
响应:
{ "heima": { "mappings": { "goods": { "properties": { "images": { "type": "keyword", "index": false }, "price": { "type": "float" }, "title": { "type": "text", "analyzer": "ik_max_word" } } } } } }
Elasticsearch中支持的数据类型很是丰富:
咱们说几个关键的:
String类型,又分两种:
Numerical:数值类型,分两类
Date:日期类型
elasticsearch能够对日期格式化为字符串存储,可是建议咱们存储为毫秒值,存储为long,节省空间。
index影响字段的索引状况。
index的默认值就是true,也就是说你不进行任何配置,全部字段都会被索引。
可是有些字段是咱们不但愿被索引的,好比商品的图片信息,就须要手动设置index为false。
是否将数据进行额外存储。
在学习lucene和solr时,咱们知道若是一个字段的store设置为false,那么在文档列表中就不会有这个字段的值,用户的搜索结果中不会显示出来。
可是在Elasticsearch中,即使store设置为false,也能够搜索到结果。
缘由是Elasticsearch在建立文档索引时,会将文档中的原始数据备份,保存到一个叫作_source
的属性中。并且咱们能够经过过滤_source
来选择哪些要显示,哪些不显示。
而若是设置store为true,就会在_source
之外额外存储一份数据,多余,所以通常咱们都会将store设置为false,事实上,store的默认值就是false。
激励因子,这个与lucene中同样
其它的再也不一一讲解,用的很少,你们参考官方文档:
经过POST请求,能够向一个已经存在的索引库中添加数据。
语法:
POST /索引库名/类型名 { "key":"value" }
示例:
POST /heima/goods/ { "title":"小米手机", "images":"http://image.leyou.com/12479122.jpg", "price":2699.00 }
响应:
{ "_index": "heima", "_type": "goods", "_id": "r9c1KGMBIhaxtY5rlRKv", "_version": 1, "result": "created", "_shards": { "total": 3, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 2 }
经过kibana查看数据:
get _search { "query":{ "match_all":{} } }
{ "_index": "heima", "_type": "goods", "_id": "r9c1KGMBIhaxtY5rlRKv", "_version": 1, "_score": 1, "_source": { "title": "小米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2699 } }
_source
:源文档信息,全部的数据都在里面。_id
:这条文档的惟一标示,与文档本身的id字段没有关联若是咱们想要本身新增的时候指定id,能够这么作:
POST /索引库名/类型/id值 { ... }
示例:
POST /heima/goods/2 { "title":"大米手机", "images":"http://image.leyou.com/12479122.jpg", "price":2899.00 }
获得的数据:
{ "_index": "heima", "_type": "goods", "_id": "2", "_score": 1, "_source": { "title": "大米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2899 } }
在学习Solr时咱们发现,咱们在新增数据时,只能使用提早配置好映射属性的字段,不然就会报错。
不过在Elasticsearch中并无这样的规定。
事实上Elasticsearch很是智能,你不须要给索引库设置任何mapping映射,它也能够根据你输入的数据来判断类型,动态添加数据映射。
测试一下:
POST /heima/goods/3 { "title":"超米手机", "images":"http://image.leyou.com/12479122.jpg", "price":2899.00, "stock": 200, "saleable":true }
咱们额外添加了stock库存,和saleable是否上架两个字段。
来看结果:
{ "_index": "heima", "_type": "goods", "_id": "3", "_version": 1, "_score": 1, "_source": { "title": "超米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2899, "stock": 200, "saleable": true } }
在看下索引库的映射关系:
{ "heima": { "mappings": { "goods": { "properties": { "images": { "type": "keyword", "index": false }, "price": { "type": "float" }, "saleable": { "type": "boolean" }, "stock": { "type": "long" }, "title": { "type": "text", "analyzer": "ik_max_word" } } } } } }
stock和saleable都被成功映射了。
若是存储的是String类型数据,ES无智能判断,他就会存入两个字段。例如:
存入一个name字段,智能造成两个字段:
把刚才新增的请求方式改成PUT,就是修改了。不过修改必须指定id,
好比,咱们把id为3的数据进行修改:
PUT /heima/goods/3 { "title":"超大米手机", "images":"http://image.leyou.com/12479122.jpg", "price":3899.00, "stock": 100, "saleable":true }
结果:
{ "took": 17, "timed_out": false, "_shards": { "total": 9, "successful": 9, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "heima", "_type": "goods", "_id": "3", "_score": 1, "_source": { "title": "超大米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 3899, "stock": 100, "saleable": true } } ] } }
删除使用DELETE请求,一样,须要根据id进行删除:
语法
DELETE /索引库名/类型名/id值
示例:
咱们从4块来说查询:
_source
过滤基本语法
GET /索引库名/_search { "query":{ "查询类型":{ "查询条件":"查询条件值" } } }
这里的query表明一个查询对象,里面能够有不一样的查询属性
match_all
, match
,term
, range
等等示例:
GET /heima/_search { "query":{ "match_all": {} } }
query
:表明查询对象match_all
:表明查询全部结果:
{ "took": 2, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "heima", "_type": "goods", "_id": "2", "_score": 1, "_source": { "title": "大米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2899 } }, { "_index": "heima", "_type": "goods", "_id": "r9c1KGMBIhaxtY5rlRKv", "_score": 1, "_source": { "title": "小米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2699 } } ] } }
咱们先加入一条数据,便于测试:
PUT /heima/goods/3 { "title":"小米电视4A", "images":"http://image.leyou.com/12479122.jpg", "price":3899.00 }
如今,索引库中有2部手机,1台电视:
match
类型查询,会把查询条件进行分词,而后进行查询,多个词条之间是or的关系
GET /heima/_search { "query":{ "match":{ "title":"小米电视" } } }
结果:
"hits": { "total": 2, "max_score": 0.6931472, "hits": [ { "_index": "heima", "_type": "goods", "_id": "tmUBomQB_mwm6wH_EC1-", "_score": 0.6931472, "_source": { "title": "小米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2699 } }, { "_index": "heima", "_type": "goods", "_id": "3", "_score": 0.5753642, "_source": { "title": "小米电视4A", "images": "http://image.leyou.com/12479122.jpg", "price": 3899 } } ] }
在上面的案例中,不只会查询到电视,并且与小米相关的都会查询到,多个词之间是or
的关系。
某些状况下,咱们须要更精确查找,咱们但愿这个关系变成and
,能够这样作:
GET /heima/_search { "query":{ "match": { "title": { "query": "小米电视", "operator": "and" } } } }
结果:
{ "took": 2, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.5753642, "hits": [ { "_index": "heima", "_type": "goods", "_id": "3", "_score": 0.5753642, "_source": { "title": "小米电视4A", "images": "http://image.leyou.com/12479122.jpg", "price": 3899 } } ] } }
本例中,只有同时包含小米
和电视
的词条才会被搜索到。
在 or
与 and
间二选一有点过于非黑即白。 若是用户给定的条件分词后有 5 个查询词项,想查找只包含其中 4 个词的文档,该如何处理?将 operator 操做符参数设置成 and
只会将此文档排除。
有时候这正是咱们指望的,但在全文搜索的大多数应用场景下,咱们既想包含那些可能相关的文档,同时又排除那些不太相关的。换句话说,咱们想要处于中间某种结果。
match
查询支持 minimum_should_match
最小匹配参数, 这让咱们能够指定必须匹配的词项数用来表示一个文档是否相关。咱们能够将其设置为某个具体数字,更经常使用的作法是将其设置为一个百分数
,由于咱们没法控制用户搜索时输入的单词数量:
GET /heima/_search { "query":{ "match":{ "title":{ "query":"小米曲面电视", "minimum_should_match": "75%" } } } }
本例中,搜索语句能够分为3个词,若是使用and关系,须要同时知足3个词才会被搜索到。这里咱们采用最小品牌数:75%,那么也就是说只要匹配到总词条数量的75%便可,这里3*75% 约等于2。因此只要包含2个词条就算知足条件了。
结果:
multi_match
与match
相似,不一样的是它能够在多个字段中查询
GET /heima/_search { "query":{ "multi_match": { "query": "小米", "fields": [ "title", "subTitle" ] } } }
本例中,咱们会在title字段和subtitle字段中查询小米
这个词
term
查询被用于精确值 匹配,这些精确值多是数字、时间、布尔或者那些未分词的字符串
GET /heima/_search { "query":{ "term":{ "price":2699.00 } } }
结果:
{ "took": 2, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "heima", "_type": "goods", "_id": "r9c1KGMBIhaxtY5rlRKv", "_score": 1, "_source": { "title": "小米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2699 } } ] } }
terms
查询和 term 查询同样,但它容许你指定多值进行匹配。若是这个字段包含了指定值中的任何一个值,那么这个文档知足条件:
GET /heima/_search { "query":{ "terms":{ "price":[2699.00,2899.00,3899.00] } } }
结果:
{ "took": 4, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": [ { "_index": "heima", "_type": "goods", "_id": "2", "_score": 1, "_source": { "title": "大米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2899 } }, { "_index": "heima", "_type": "goods", "_id": "r9c1KGMBIhaxtY5rlRKv", "_score": 1, "_source": { "title": "小米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2699 } }, { "_index": "heima", "_type": "goods", "_id": "3", "_score": 1, "_source": { "title": "小米电视4A", "images": "http://image.leyou.com/12479122.jpg", "price": 3899 } } ] } }
默认状况下,elasticsearch在搜索的结果中,会把文档中保存在_source
的全部字段都返回。
若是咱们只想获取其中的部分字段,咱们能够添加_source
的过滤
示例:
GET /heima/_search { "_source": ["title","price"], "query": { "term": { "price": 2699 } } }
返回的结果:
{ "took": 12, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "heima", "_type": "goods", "_id": "r9c1KGMBIhaxtY5rlRKv", "_score": 1, "_source": { "price": 2699, "title": "小米手机" } } ] } }
咱们也能够经过:
两者都是可选的。
示例:
GET /heima/_search { "_source": { "includes":["title","price"] }, "query": { "term": { "price": 2699 } } }
与下面的结果将是同样的:
GET /heima/_search { "_source": { "excludes": ["images"] }, "query": { "term": { "price": 2699 } } }
bool
把各类其它查询经过must
(与)、must_not
(非)、should
(或)的方式进行组合
GET /heima/_search { "query":{ "bool":{ "must": { "match": { "title": "大米" }}, "must_not": { "match": { "title": "电视" }}, "should": { "match": { "title": "手机" }} } } }
结果:
{ "took": 10, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.5753642, "hits": [ { "_index": "heima", "_type": "goods", "_id": "2", "_score": 0.5753642, "_source": { "title": "大米手机", "images": "http://image.leyou.com/12479122.jpg", "price": 2899 } } ] } }
range
查询找出那些落在指定区间内的数字或者时间
GET /heima/_search { "query":{ "range": { "price": { "gte": 1000.0, "lt": 2800.00 } } } }
range
查询容许如下字符:
操做符 | 说明 |
---|---|
gt | 大于 |
gte | 大于等于 |
lt | 小于 |
lte | 小于等于 |
咱们新增一个商品:
POST /heima/goods/4 { "title":"apple手机", "images":"http://image.leyou.com/12479122.jpg", "price":6899.00 }
fuzzy
查询是 term
查询的模糊等价。它容许用户搜索词条与实际词条的拼写出现误差,可是误差的编辑距离不得超过2:
GET /heima/_search { "query": { "fuzzy": { "title": "appla" } } }
上面的查询,也能查询到apple手机
咱们能够经过fuzziness
来指定容许的编辑距离:
GET /heima/_search { "query": { "fuzzy": { "title": { "value":"appla", "fuzziness":1 } } } }
条件查询中进行过滤
全部的查询都会影响到文档的评分及排名。若是咱们须要在查询结果中进行过滤,而且不但愿过滤条件影响评分,那么就不要把过滤条件做为查询条件来用。而是使用filter
方式:
GET /heima/_search { "query":{ "bool":{ "must":{ "match": { "title": "小米手机" }}, "filter":{ "range":{"price":{"gt":2000.00,"lt":3800.00}} } } } }
注意:filter
中还能够再次进行bool
组合条件过滤。
无查询条件,直接过滤
若是一次查询只有过滤,没有查询条件,不但愿进行评分,咱们可使用constant_score
取代只有 filter 语句的 bool 查询。在性能上是彻底相同的,但对于提升查询简洁性和清晰度有很大帮助。
GET /heima/_search { "query":{ "constant_score": { "filter": { "range":{"price":{"gt":2000.00,"lt":3000.00}} } } }
sort
可让咱们按照不一样的字段进行排序,而且经过order
指定排序的方式
GET /heima/_search { "query": { "match": { "title": "小米手机" } }, "sort": [ { "price": { "order": "desc" } } ] }
假定咱们想要结合使用 price和 _score(得分) 进行查询,而且匹配的结果首先按照价格排序,而后按照相关性得分排序:
GET /goods/_search { "query":{ "bool":{ "must":{ "match": { "title": "小米手机" }}, "filter":{ "range":{"price":{"gt":200000,"lt":300000}} } } }, "sort": [ { "price": { "order": "desc" }}, { "_score": { "order": "desc" }} ] }
聚合可让咱们极其方便的实现对数据的统计、分析。例如:
实现这些统计功能的比数据库的sql要方便的多,并且查询速度很是快,能够实现实时搜索效果。
Elasticsearch中的聚合,包含多种类型,最经常使用的两种,一个叫桶
,一个叫度量
:
桶(bucket)
桶的做用,是按照某种方式对数据进行分组,每一组数据在ES中称为一个桶
,例如咱们根据国籍对人划分,能够获得中国桶
、英国桶
,日本桶
……或者咱们按照年龄段对人进行划分:0~10,10~20,20~30,30~40等。
Elasticsearch中提供的划分桶的方式有不少:
综上所述,咱们发现bucket aggregations 只负责对数据进行分组,并不进行计算,所以每每bucket中每每会嵌套另外一种聚合:metrics aggregations即度量
度量(metrics)
分组完成之后,咱们通常会对组中的数据进行聚合运算,例如求平均值、最大、最小、求和等,这些在ES中称为度量
比较经常使用的一些度量聚合方式:
为了测试聚合,咱们先批量导入一些数据
建立索引:
PUT /cars { "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "transactions": { "properties": { "color": { "type": "keyword" }, "make": { "type": "keyword" } } } } }
注意:在ES中,须要进行聚合、排序、过滤的字段其处理方式比较特殊,所以不能被分词。这里咱们将color和make这两个文字类型的字段设置为keyword类型,这个类型不会被分词,未来就能够参与聚合
导入数据
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" }
首先,咱们按照 汽车的颜色color
来划分桶
GET /cars/_search { "size" : 0, "aggs" : { "popular_colors" : { "terms" : { "field" : "color" } } } }
结果:
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": 8, "max_score": 0, "hits": [] }, "aggregations": { "popular_colors": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "red", "doc_count": 4 }, { "key": "blue", "doc_count": 2 }, { "key": "green", "doc_count": 2 } ] } } }
经过聚合的结果咱们发现,目前红色的小车比较畅销!
前面的例子告诉咱们每一个桶里面的文档数量,这颇有用。 但一般,咱们的应用须要提供更复杂的文档度量。 例如,每种颜色汽车的平均价格是多少?
所以,咱们须要告诉Elasticsearch使用哪一个字段
,使用何种度量方式
进行运算,这些信息要嵌套在桶
内,度量
的运算会基于桶
内的文档进行
如今,咱们为刚刚的聚合结果添加 求价格平均值的度量:
GET /cars/_search { "size" : 0, "aggs" : { "popular_colors" : { "terms" : { "field" : "color" }, "aggs":{ "avg_price": { "avg": { "field": "price" } } } } } }
度量
也是一个聚合,度量是在桶内的聚合结果:
... "aggregations": { "popular_colors": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "red", "doc_count": 4, "avg_price": { "value": 32500 } }, { "key": "blue", "doc_count": 2, "avg_price": { "value": 20000 } }, { "key": "green", "doc_count": 2, "avg_price": { "value": 21000 } } ] } } ...
能够看到每一个桶中都有本身的avg_price
字段,这是度量聚合的结果
刚刚的案例中,咱们在桶内嵌套度量运算。事实上桶不只能够嵌套运算, 还能够再嵌套其它桶。也就是说在每一个分组中,再分更多组。
好比:咱们想统计每种颜色的汽车中,分别属于哪一个制造商,按照make
字段再进行分桶
GET /cars/_search { "size" : 0, "aggs" : { "popular_colors" : { "terms" : { "field" : "color" }, "aggs":{ "avg_price": { "avg": { "field": "price" } }, "maker":{ "terms":{ "field":"make" } } } } } }
部分结果:
... {"aggregations": { "popular_colors": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "red", "doc_count": 4, "maker": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "honda", "doc_count": 3 }, { "key": "bmw", "doc_count": 1 } ] }, "avg_price": { "value": 32500 } }, { "key": "blue", "doc_count": 2, "maker": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "ford", "doc_count": 1 }, { "key": "toyota", "doc_count": 1 } ] }, "avg_price": { "value": 20000 } }, { "key": "green", "doc_count": 2, "maker": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "ford", "doc_count": 1 }, { "key": "toyota", "doc_count": 1 } ] }, "avg_price": { "value": 21000 } } ] } } } ...
maker
被嵌套在原来每个color
的桶中。make
字段进行了分组前面讲了,划分桶的方式有不少,例如:
刚刚的案例中,咱们采用的是Terms Aggregation,即根据词条划分桶。
接下来,咱们再学习几个比较实用的:
原理:
histogram是把数值类型的字段,按照必定的阶梯大小进行分组。你须要指定一个阶梯值(interval)来划分阶梯大小。
举例:
好比你有价格字段,若是你设定interval的值为200,那么阶梯就会是这样的:
0,200,400,600,...
上面列出的是每一个阶梯的key,也是区间的启点。
若是一件商品的价格是450,会落入哪一个阶梯区间呢?计算公式以下:
bucket_key = Math.floor((value - offset) / interval) * interval + offset
value:就是当前数据的值,本例中是450
offset:起始偏移量,默认为0
interval:阶梯间隔,好比200
所以你获得的key = Math.floor((450 - 0) / 200) * 200 + 0 = 400
操做一下:
好比,咱们对汽车的价格进行分组,指定间隔interval为5000:
GET /cars/_search { "size":0, "aggs":{ "price":{ "histogram": { "field": "price", "interval": 5000 } } } }
结果:
{ "took": 21, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 8, "max_score": 0, "hits": [] }, "aggregations": { "price": { "buckets": [ { "key": 10000, "doc_count": 2 }, { "key": 15000, "doc_count": 1 }, { "key": 20000, "doc_count": 2 }, { "key": 25000, "doc_count": 1 }, { "key": 30000, "doc_count": 1 }, { "key": 35000, "doc_count": 0 }, { "key": 40000, "doc_count": 0 }, { "key": 45000, "doc_count": 0 }, { "key": 50000, "doc_count": 0 }, { "key": 55000, "doc_count": 0 }, { "key": 60000, "doc_count": 0 }, { "key": 65000, "doc_count": 0 }, { "key": 70000, "doc_count": 0 }, { "key": 75000, "doc_count": 0 }, { "key": 80000, "doc_count": 1 } ] } } }
你会发现,中间有大量的文档数量为0 的桶,看起来很丑。
咱们能够增长一个参数min_doc_count为1,来约束最少文档数量为1,这样文档数量为0的桶会被过滤
示例:
GET /cars/_search { "size":0, "aggs":{ "price":{ "histogram": { "field": "price", "interval": 5000, "min_doc_count": 1 } } } }
结果:
{ "took": 15, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 8, "max_score": 0, "hits": [] }, "aggregations": { "price": { "buckets": [ { "key": 10000, "doc_count": 2 }, { "key": 15000, "doc_count": 1 }, { "key": 20000, "doc_count": 2 }, { "key": 25000, "doc_count": 1 }, { "key": 30000, "doc_count": 1 }, { "key": 80000, "doc_count": 1 } ] } } }
完美,!
若是你用kibana将结果变为柱形图,会更好看:
范围分桶与阶梯分桶相似,也是把数字按照阶段进行分组,只不过range方式须要你本身指定每一组的起始和结束大小。
原文出处:https://www.cnblogs.com/jimlau/p/12143251.html