ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Elastic Search的聚合搜索

2019-11-23 12:01:54  阅读:254  来源: 互联网

标签:index Search 聚合 Elastic tags price field type


就是使用ES提供的aggs语法结果,使用DSL搜索的语法,实现聚合数据的统计,查询。
ES中,如果新增document数据的时候,对应的index和type不存在,则自动创建。


1 准备源数据
PUT /products_index/phone_type/1
{
"name":"IPHONE 8",
"remark":"64G",
"price":548800,
"producer":"APPLE",
"tags" : [ "64G", "red color", "Nano SIM" ]
}
PUT /products_index/phone_type/2
{
"name":"IPHONE 8",
"remark":"64G",
"price":548800,
"producer":"APPLE",
"tags" : [ "64G", "golden color", "Nano SIM" ]
}
PUT /products_index/phone_type/3
{
"name":"IPHONE 8 PLUS",
"remark":"128G",
"price":748800,
"producer":"APPLE",
"tags" : [ "128G", "red color", "Nano SIM" ]
}
PUT /products_index/phone_type/4
{
"name":"IPHONE 8 PLUS",
"remark":"256G",
"price":888800,
"producer":"APPLE",
"tags" : [ "256G", "golden color", "Nano SIM" ]
}

## 将文本类型的field的fielddata设置为true。用于设置ES中对倒排索引的设置,将倒排索引内容重设一份正排索引,并提供内存存储计算能力。
## 正排索引,类似数据库中的普通索引。依赖倒排索引中的数据,不做二次解析,将倒排索引解析的数据信息,建立一个索引,索引用于内存计算,如:分析,分组,字符串排序等。
PUT /products_index/_mapping/phone_type
{
"properties" : {
"tags" : {
"type" : "text",
"fielddata" : true
}
}
}

 

2 聚合统计
计算每个tag中的Document数量
terms : 检索词组的,安装标准词组分组,统计数据document的数量。类似数据库中的count。
聚合搜索,语法的大体结构和DSL搜索语句类似。类似数据库中的count。 Select count(*) from table
GET /products_index/phone_type/_search
{
"size" : 0, # 查多少数据。
"aggs" : { # 开始聚合,类似query,是一个命令。或api
"group_by_tags":{ # 给聚合数据,加一个命名。自定义。
"terms" : { # 是一个聚合api,类似数据库中的聚合函数。解析某字段中的词条。如:a字段的值是 test field.假设解析后词条为test和field。那么就是根据a字段的解析词条,test和field来统计每个数据在多少个document中存在。
"field" : "tags"
}
}
}
}
"size":0代表显示多少计算源数据Document


3 增加搜索匹配条件的聚合统计
搜索名称中包含PLUS的Document,并计算每个tag中的Document数量。统计是search中的一部分。一般在DSL query中使用。所以经常和条件搜索配合完成统计。
GET /products_index/phone_type/_search
{
"size" : 0,
"query" : {
"match" : { "name" : "PLUS" }
},
"aggs" : {
"group_by_tags":{
"terms" : { "field" : "tags" }
}
}
}


4 聚合后实现计算
聚合嵌套
# 计算name中包含plus的document数据中的price字段平均值。
GET /products_index/phone_type/_search
{
"query": {
"match": {
"name": "plus"
}
},
"aggs": {
"avg_by_price" : {
"avg": {
"field": "price"
}
}
}
}

# 搜索包含plus的document,根据tags做词条统计,在统计结果中,计算price平均值。聚合是可以嵌套的,内层聚合是依托于外层聚合的结果之上,实现聚合计算的。
GET /products_index/phone_type/_search
{
"query": {
"match": {
"name": "plus"
}
},
"aggs": {
"group_by_tags":{
"terms": {
"field": "tags"
},
"aggs": {
"avg_by_price": {
"avg": {
"field": "price"
}
}
}
}
}
}


5 聚合的排序
类似SQL - select * from group by .. order by ..
聚合aggs中如果使用order排序的话,要求排序字段必须是一个aggs聚合相关的字段。聚合相关字段代表的含义是:当前聚合的子聚合命名。如:外部聚合是使用terms实现的聚合,命名为group_by_tags,其内层聚合是使用avg计算平均值,聚合名称为avg_by_price,那么avg_by_price称为聚合相关字段。
计算每个tag中的Document数据的price平均值,并根据price字段数据排序
GET /products_index/phone_type/_search
{
"size" : 0,
"aggs" : {
"group_by_tags" : {
"terms" : { "field" : "tags", "order":{"avg_price" : "desc"} },
"aggs" : {
"avg_price" : {
"avg" : { "field" : "price" }
}
}
}
}
}


6 范围分组并计算
使用price取值范围分组,再计算分组Document中price的平均值
GET /products_index/phone_type/_search
{
"query": {
"match_all": {}
},
"_source": "price",
"aggs": {
"range_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 500000,
"to": 600000
},
{
"from": 600001,
"to": 800000
},
{
"from": 800001,
"to": 1000000
}
]
},
"aggs": {
"avg_by_price": {
"avg": {
"field": "price"
}
}
}
}
}
}

 

标签:index,Search,聚合,Elastic,tags,price,field,type
来源: https://www.cnblogs.com/yucongblog/p/11917058.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有