es总结

Elasticsearch是什么?跟mysql的区别

1.传统数据库在全文检索方面很鸡肋,海量数据下的查询很慢,对非结构化文本数据的不支持,ES支持非结构化数据的存储和查询。
2.ES支持分布式文档存储。
3.ES是分布式实时搜索,并且响应时间比关系型数据库快。
4.ES在分词方面比关系型好,能做到精确分词。
5.ES对已有的数据,在数据匹配性方面比关系型数据库好, 例如:搜索“星巴克咖啡”,ES会优先返回带有“星巴克咖啡”的数据,不会优先返回带有“咖啡”的数据。
Elasticsearch中的类型都是什么?
query and fetch 优点:只需要查询一次 缺点:返回的数据量不准确, 可能返回(N分片数量)的数据并且数据排名也不准确
query then fetch( es 默认的搜索方式)优点:返回的数据量是准确的 缺点:性能一般,并且数据排名不准确
DFS query and fetch优点:数据排名准确 缺点:性能一般 返回的数据量不准确, 可能返回(N
分片数量)的数据
DFS query then fetch优点:返回的数据量是准确的数据排名准确 缺点:性能最差【 这个最差只是表示在这四种查询方式中性能最慢, 也不至于不能忍受,如果对查询性能要求不是非常高, 而对查询准确度要求比较高的时候可以考虑这个】
**总结:**从性能考虑 QUERY_AND_FETCH 是最快的, DFS_QUERY_THEN_FETCH 是最慢的。从搜索的准确度来说, DFS 要比非 DFS 的准确度更高。

Elasticsearch怎么创建索引库,怎么删除索引库,怎么查看索引库

1.2.创建索引
Elasticsearch采用Rest风格API,因此其API就是一次http请求,你可以用任何工具发起http请求
创建索引的请求格式:
• 请求方式:PUT
• 请求路径:/索引库名
• 请求参数:json格式:
• {
• “settings”: {
• “number_of_shards”: 3,
• “number_of_replicas”: 2
• }
• }
• settings:索引库的设置
• number_of_shards:分片数量
• number_of_replicas:副本数量
1.3.查看索引设置
语法
Get请求可以帮我们查看索引信息,格式:
GET /索引库名
1.4.删除索引
删除索引使用DELETE请求
语法
DELETE /索引库名
Elasticsearch怎么创建映射,怎么查看映射,怎么删除映射
1.5.映射配置
索引有了,接下来肯定是添加数据。但是,在添加数据之前必须定义映射。
什么是映射?
映射是定义文档的过程,文档包含哪些字段,这些字段是否保存,是否索引,是否分词等
只有配置清楚,Elasticsearch才会帮我们进行索引库的创建(不一定)
1.5.1.创建映射字段
语法
请求方式依然是PUT
PUT /索引库名/_mapping/类型名称
{
“properties”: {
“字段名”: {
“type”: “类型”,
“index”: true,
“store”: true,
“analyzer”: “分词器”
},
“字段名”: {
“type”: “类型”,
“index”: true,
“store”: true,
“analyzer”: “分词器”
}
}
}
• 类型名称:就是前面将的type的概念,类似于数据库中的不同表
字段名:任意填写 ,可以指定许多属性,例如:
• type:类型,可以是text、long、short、date、integer、object等
• index:是否索引,默认为true
• store:是否存储,默认为false
• 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
}

1.5.2.查看映射关系
语法:
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怎么插入数据,怎么修改数据,怎么删除数据
1.6.新增数据
1.6.1.随机生成id
通过POST请求,可以向一个已经存在的索引库中添加数据。
语法:
POST /索引库名/类型名
{
“key”:“value”
}
S示例:
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字段没有关联
1.6.2.自定义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
}
}
1.6.3.智能判断
在学习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都被成功映射了。
1.7.修改数据
把刚才新增的请求方式改为PUT,就是修改了。不过修改必须指定id,
• id对应文档存在,则修改
• 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
}
}
]
}
}
1.8.删除数据
删除使用DELETE请求,同样,需要根据id进行删除:
语法
DELETE /索引库名/类型名/id值

Elasticsearch中搜索都有哪几种模式,分别介绍一下
2.查询
我们从4块来讲查询:
• 基本查询
• _source过滤
• 结果过滤
• 高级查询
• 排序
2.1.基本查询
基本语法
GET /索引库名/_search
{
“query”:{
“查询类型”:{
“查询条件”:“查询条件值”
}
}
}
这里的query代表一个查询对象,里面可以有不同的查询属性
• 查询类型:
• 例如:match_all, match,term , range 等等
• 查询条件:查询条件会根据类型的不同,写法也有差异,后面详细讲解
2.1.1 查询所有(match_all)
示例:
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
}
}
]
}
}
• took:查询花费时间,单位是毫秒
• time_out:是否超时
• _shards:分片信息
• hits:搜索结果总览对象
• total:搜索到的总条数
• max_score:所有结果中文档得分的最高分
• hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
• _index:索引库
• _type:文档类型
• _id:文档id
• _score:文档得分
• _source:文档的源数据
2.1.2 匹配查询(match)
我们先加入一条数据,便于测试:
PUT /heima/goods/3
{
“title”:“小米电视4A”,
“images”:“http://image.leyou.com/12479122.jpg”,
“price”:3899.00
}
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关系
某些情况下,我们需要更精确查找,我们希望这个关系变成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之间?
在 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个词条就算满足条件了。
2.1.3 多字段查询(multi_match)
multi_match与match类似,不同的是它可以在多个字段中查询
GET /heima/_search
{
“query”:{
“multi_match”: {
“query”: “小米”,
“fields”: [ “title”, “subTitle” ]
}
}
}
本例中,我们会在title字段和subtitle字段中查询小米这个词
2.1.4 词条匹配(term)
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
}
}
]
}
}
2.1.5 多词条精确匹配(terms)
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
}
}
]
}
}
2.2.结果过滤
默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source的所有字段都返回。
如果我们只想获取其中的部分字段,我们可以添加_source的过滤
2.2.1.直接指定字段
示例:
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”: “小米手机”
}
}
]
}
}
2.2.2.指定includes和excludes
我们也可以通过:
• includes:来指定想要显示的字段
• excludes:来指定不想要显示的字段
二者都是可选的。
示例:
GET /heima/_search
{
“_source”: {
“includes”:[“title”,“price”]
},
“query”: {
“term”: {
“price”: 2699
}
}
}
与下面的结果将是一样的:
GET /heima/_search
{
“_source”: {
“excludes”: [“images”]
},
“query”: {
“term”: {
“price”: 2699
}
}
}
2.3 高级查询
3.3.1 布尔组合(bool)
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
}
}
]
}
}
3.3.2 范围查询(range)
range 查询找出那些落在指定区间内的数字或者时间
GET /heima/_search
{
“query”:{
“range”: {
“price”: {
“gte”: 1000.0,
“lt”: 2800.00
}
}
}
}
range查询允许以下字符:
操作符 说明
gt 大于
gte 大于等于
lt 小于
lte 小于等于
3.3.3 模糊查询(fuzzy)
我们新增一个商品:
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
}
}
}
}
2.4 过滤(filter)
条件查询中进行过滤
所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用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}}
}
}
}
2.5 排序
2.4.1 单字段排序
sort 可以让我们按照不同的字段进行排序,并且通过order指定排序的方式
GET /heima/_search
{
“query”: {
“match”: {
“title”: “小米手机”
}
},
“sort”: [
{
“price”: {
“order”: “desc”
}
}
]
}
2.4.2 多字段排序
假定我们想要结合使用 price和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序:
GET /goods/_search
{
“query”:{
“bool”:{
“must”:{ “match”: { “title”: “小米手机” }},
“filter”:{
“range”:{“price”:{“gt”:200000,“lt”:300000}}
}
}
},
“sort”: [
{ “price”: { “order”: “desc” }},
{ “_score”: { “order”: “desc” }}
]
}

Elasticsearch中聚合是什么意思,桶是什么意思,度量是什么意思
聚合可以让我们极其方便的实现对数据的统计、分析。实现这些统计功能的比数据库的sql要方便的多,而且查询速度非常快,可以实现实时搜索效果。
3.1 基本概念
Elasticsearch中的聚合,包含多种类型,最常用的两种,一个叫桶,一个叫度量:
桶(bucket)
桶的作用,是按照某种方式对数据进行分组,每一组数据在ES中称为一个桶,例如我们根据国籍对人划分,可以得到中国桶、英国桶,日本桶……或者我们按照年龄段对人进行划分:010,1020,2030,3040等。
Elasticsearch中提供的划分桶的方式有很多:
• Date Histogram Aggregation:根据日期阶梯分组,例如给定阶梯为周,会自动每周分为一组
• Histogram Aggregation:根据数值阶梯分组,与日期类似
• Terms Aggregation:根据词条内容分组,词条内容完全匹配的为一组
• Range Aggregation:数值和日期的范围分组,指定开始和结束,然后按段分组
• ……
bucket aggregations 只负责对数据进行分组,并不进行计算,因此往往bucket中往往会嵌套另一种聚合:metrics aggregations即度量
度量(metrics)
分组完成以后,我们一般会对组中的数据进行聚合运算,例如求平均值、最大、最小、求和等,这些在ES中称为度量
比较常用的一些度量聚合方式:
• Avg Aggregation:求平均值
• Max Aggregation:求最大值
• Min Aggregation:求最小值
• Percentiles Aggregation:求百分比
• Stats Aggregation:同时返回avg、max、min、sum、count等
• Sum Aggregation:求和
• Top hits Aggregation:求前几
• Value Count Aggregation:求总数
• ……
为了测试聚合,我们先批量导入一些数据
创建索引:
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” }
3.2 聚合为桶
首先,我们按照 汽车的颜色color来划分桶
GET /cars/_search
{
“size” : 0,
“aggs” : {
“popular_colors” : {
“terms” : {
“field” : “color”
}
}
}
}
• size: 查询条数,这里设置为0,因为我们不关心搜索到的数据,只关心聚合结果,提高效率
• aggs:声明这是一个聚合查询,是aggregations的缩写
• popular_colors:给这次聚合起一个名字,任意。
• terms:划分桶的方式,这里是根据词条划分
• field:划分桶的字段
结果:
{
“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
}
]
}
}
}
• hits:查询结果为空,因为我们设置了size为0
• aggregations:聚合的结果
• popular_colors:我们定义的聚合名称
• buckets:查找到的桶,每个不同的color字段值都会形成一个桶
• key:这个桶对应的color字段的值
• doc_count:这个桶中的文档数量
通过聚合的结果我们发现,目前红色的小车比较畅销!
3.3 桶内度量
前面的例子告诉我们每个桶里面的文档数量,这很有用。 但通常,我们的应用需要提供更复杂的文档度量。 例如,每种颜色汽车的平均价格是多少?
因此,我们需要告诉Elasticsearch使用哪个字段,使用何种度量方式进行运算,这些信息要嵌套在桶内,度量的运算会基于桶内的文档进行
现在,我们为刚刚的聚合结果添加 求价格平均值的度量:
GET /cars/_search
{
“size” : 0,
“aggs” : {
“popular_colors” : {
“terms” : {
“field” : “color”
},
“aggs”:{
“avg_price”: {
“avg”: {
“field”: “price”
}
}
}
}
}
}
• aggs:我们在上一个aggs(popular_colors)中添加新的aggs。可见度量也是一个聚合
• avg_price:聚合的名称
• avg:度量的类型,这里是求平均值
• field:度量运算的字段
结果:

“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字段,这是度量聚合的结果
3.4 桶内嵌套桶
刚刚的案例中,我们在桶内嵌套度量运算。事实上桶不仅可以嵌套运算, 还可以再嵌套其它桶。也就是说在每个分组中,再分更多组。
比如:我们想统计每种颜色的汽车中,分别属于哪个制造商,按照make字段再进行分桶
GET /cars/_search
{
“size” : 0,
“aggs” : {
“popular_colors” : {
“terms” : {
“field” : “color”
},
“aggs”:{
“avg_price”: {
“avg”: {
“field”: “price”
}
},
“maker”:{
“terms”:{
“field”:“make”
}
}
}
}
}
}
• 原来的color桶和avg计算我们不变
• maker:在嵌套的aggs下新添一个桶,叫做maker
• terms:桶的划分类型依然是词条
• filed:这里根据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字段进行了分组
• 我们能读取到的信息:
• 红色车共有4辆
• 红色车的平均售价是 $32,500 美元。
• 其中3辆是 Honda 本田制造,1辆是 BMW 宝马制造。
3.5.划分桶的其它方式
前面讲了,划分桶的方式有很多,例如:
• Date Histogram Aggregation:根据日期阶梯分组,例如给定阶梯为周,会自动每周分为一组
• Histogram Aggregation:根据数值阶梯分组,与日期类似
• Terms Aggregation:根据词条内容分组,词条内容完全匹配的为一组
• Range Aggregation:数值和日期的范围分组,指定开始和结束,然后按段分组
刚刚的案例中,我们采用的是Terms Aggregation,即根据词条划分桶。
接下来,我们再学习几个比较实用的:
3.5.1.阶梯分桶Histogram
原理:
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将结果变为柱形图,会更好看:
3.5.2.范围分桶range
范围分桶与阶梯分桶类似,也是把数字按照阶段进行分组,只不过range方式需要你自己指定每一组的起始和结束大小。
请写出一个聚合查询的语句,并解释含义
聚合查询
聚合提供了用户进行分组和数理统计的能力,可以把聚合理解成SQL中的GROUP BY和分组函数
指标聚合/桶聚合
Metrics(度量/指标):简单的对过滤出来的数据集进行avg,max操作,是一个单一的数值
Bucket(桶):将过滤出来的数据集按条件分成多个小数据集,然后Metrics会分别作用在这些小数据集上
max/min/avg/sum/stats
{
‘aggs’:{c
‘group_sum’:{
‘sum’:{
‘field’:‘money’
}
}
}
}

{
“aggs”:{
“avg_fees”:{
“avg”:{
“field”:“fees”
}
}
}
}
terms聚合
terms根据字段值项分组聚合.field按什么字段分组,size指定返回多少个分组,shard_size指定每个分片上返回多少个分组,order排序方式.可以指定include和exclude正则筛选表达式的值,指定missing设置缺省值
{
‘aggs’:{
‘group_by_type’:{
‘terms’:{
‘field’:’_type’
}
}
}
}

{
“size”: 0,
“aggs”: {
“terms”:{
“terms”: {
“field”: “__type”,
“size”: 10
}
}
}
}
{
“size”: 0,
“aggs”: {
“terms”:{
“terms”: {
“field”: “__type”,
“size”: 10,
“order”: {
“_count”: “asc”
}
}
}
}
}
{
“size”: 0,
“aggs”: {
“agg_terms”: {
“terms”: {
“field”: “cost”,
“order”: {
“_count”: “asc”
}
},
“aggs”: {
“max_balance”: {
“max”: {
“field”: “cost”
}
}
}
}
}
}
{
“size”: 0,
“aggs”: {
“agg_terms”: {
“terms”: {
“field”: “cost”,
“include”: “.",
“exclude”: ".

}
}
}
}
cardinality去重
{
“size”: 0,
“aggs”: {
“count_type”: {
“cardinality”: {
“field”: “__type”
}
}
}
}
cardinality

percentiles百分比
percentiles对指定字段(脚本)的值按从小到大累计每个值对应的文档数的占比(占所有命中文档数的百分比),返回指定占比比例对应的值。默认返回[ 1, 5, 25, 50, 75, 95, 99 ]分位上的值
{
“size”: 0,
“aggs”: {
“age_percents”:{
“percentiles”: {
“field”: “age”,
“percents”: [
1,
5,
25,
50,
75,
95,
99
]
}
}

}

}

{
“size”: 0,
“aggs”: {
“states”: {
“terms”: {
“field”: “gender”
},
“aggs”: {
“banlances”: {
“percentile_ranks”: {
“field”: “balance”,
“values”: [
20000,
40000
]
}
}
}
}
}
percentiles rank
统计小于等于指定值得文档比
{
“size”: 0,
“aggs”: {
“tests”: {
“percentile_ranks”: {
“field”: “age”,
“values”: [
10,
15
]
}
}
}
}
filter聚合
filter对满足过滤查询的文档进行聚合计算,在查询命中的文档中选取过滤条件的文档进行聚合,先过滤在聚合
{
“size”: 0,
“aggs”: {
“agg_filter”:{
“filter”: {
“match”:{“gender”:“F”}
},
“aggs”: {
“avgs”: {
“avg”: {
“field”: “age”
}
}
}
}
}
}
filtters聚合
多个过滤组聚合计算
{
“size”: 0,
“aggs”: {
“message”: {
“filters”: {

      "filters": {
        "errors": {
          "exists": {
            "field": "__type"
          }
        },
        "warring":{
          "term": {
            "__type": "info"
          }
        }
      }
    }
  }
}

}
range聚合
{
“aggs”: {
“agg_range”: {
“range”: {
“field”: “cost”,
“ranges”: [
{
“from”: 50,
“to”: 70
},
{
“from”: 100
}
]
},
“aggs”: {
“bmax”: {
“max”: {
“field”: “cost”
}
}
}
}
}
}

date_range聚合
{
“aggs”: {
“date_aggrs”: {
“date_range”: {
“field”: “accepted_time”,
“format”: “MM-yyy”,
“ranges”: [
{
“from”: “now-10d/d”,
“to”: “now”
}
]
}
}
}
}
date_histogram
时间直方图聚合,就是按天、月、年等进行聚合统计。可按 year (1y), quarter (1q), month (1M), week (1w), day (1d), hour (1h), minute (1m), second (1s) 间隔聚合或指定的时间间隔聚合
{
“aggs”: {
“sales_over_time”: {
“date_histogram”: {
“field”: “accepted_time”,
“interval”: “quarter”,
“min_doc_count” : 0, //可以返回没有数据的月份
“extended_bounds” : { //强制返回数据的范围
“min” : “2014-01-01”,
“max” : “2014-12-31”
}
}
}
}
}
missing聚合
{

“aggs”: {
“account_missing”: {
“missing”: {
“field”: “__type”
}
}
}
}

请写出一个嵌套聚合查询的语句,并解释含义
一个嵌套聚合查询的语句
GET /student/_search
{
“size” : 0,
“aggs” : {
“数学专业的学生都有谁” : {
“terms” : {
“专业” : “数学”
},
“性别”:{
“terms”:{
“性别”:“男”
}
}
}
}
}
}
含义 查询学数学专业的男学生都有谁

Elasticsearch中的端口号都有哪些,kinbana是干什么的?
Kibana 是一款适用于 Elasticsearch 的数据可视化和管理工具,可以提供实时的直方图、线形图、饼状图和地图。两个端口:
9300:作为 Tcp 协议,jar 之间就是通过 tcp 协议通讯。ES 集群之间是通过 9300 进行通讯。
9200:9200 作为 Http 协议,主要用于外部通讯客户端访问接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值