es界面的分组,求平均值的操作

更新时间:2023-07-14 22:44:07 阅读: 评论:0

es界⾯的分组,求平均值的操作
第⼀个分析需求:计算每个tag下的商品数量
平凡的世界小说默认情况下,⼤部分字段都是被索引的(有个倒排索引),以使得他们可以被搜索。
然⽽,在脚本中排序、聚合和访问字段的值,需要不同的搜索访问模式。
搜索需要回答的问题是“哪些⽂档包含这些搜索的内容?”,⽽排序和聚合需要回答的问题是“这个⽂档中这个字段的值是什么?”
⼤部分字段都可以使⽤index-time,磁盘上的doc_values⽤于这个数据的访问模式;
然⽽,text字段不⽀持doc_values。
代替的是,text字段使⽤⼀个叫做fielddata的数据结构,该数据结构含义是查询时内存数据结构。该数据结构是按需求⾸次构建在⼀个被⽤于聚合、排序和在脚本的字段上。
它是通过读取从磁盘每段的整个倒排索引来构建的,倒排搜索的内容<->⽂档关系,其存储在jvm堆上的内存上。
默认情况下text字段是没有开启的:
聚合时需要对相应的字段做处理如下,否则会报错:
Fielddata is disabled on text fields by default.
Set fielddata=true on [your_field_name] in order to load fielddata
in memory by uninverting the inverted index. Note that this can however u significant memory.
PUT my_index/_mapping/my_type
{
"properties": {
"my_field": { ①
"type": "text",
"fielddata": true
}
}
}
GET /ecommerce/product/_arch
{
"aggs": {
"group_by_tags": {
"terms": { "field": "tags" }
}
}
}
将⽂本field的fielddata属性设置为true
PUT /ecommerce/_mapping/product
{
"properties": {
"tags": {
茶艺课"type": "text",
"fielddata": true
}
}
}
GET /ecommerce/product/_arch
{
"aggs": {
"group_by_aggs": {
"terms": {
"field": "tags"
}
}
}
}
{
"took": 2,
"timed_out": fal,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"name": "jiajieshi yagao", "desc": "youxiao fangzhu", "price": 25,
"producer": "jiajieshi producer", "tags": [
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"name": "gaolujie yagao", "desc": "gaoxiao meibai", "price": 30,
"producer": "gaolujie producer", "tags": [
"meibai",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 1,
"_source": {
"name": "zhonghua yagao", "desc": "caoben zhiwu", "price": 40,
"producer": "zhonghua producer", "tags": [
"qingxin"
]
}
}
]
},张翰古力娜扎
"aggregations": {
"group_by_aggs": {
"doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [
{
"key": "fangzhu",
"doc_count": 2
},
{
"key": "meibai",
"doc_count": 1
},
{
"key": "qingxin",
"doc_count": 1梦见鲜花
}
]
}
}
}
----------------------------------------------------------------------------------------------------------------
第⼆个聚合分析的需求:对名称中包含yagao的商品,计算每个tag下的商品数量
GET /ecommerce/product/_arch
{
"size": 0,
"query": {
"match": {
"name": "yagao"
}
},
"aggs": {
"all_tags": {
"terms": {
"field": "tags"
}
}
}
}
----------------------------------------------------------------------------------------------------------------
第三个聚合分析的需求:先分组,再算每组的平均值,计算每个tag下的商品的平均价格GET /ecommerce/product/_arch
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},旋律的英文
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
{
"took": 62,
"timed_out": fal,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},每个月的14号
"aggregations": {
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "fangzhu",
"doc_count": 2,
"avg_price": {
"value": 27.5
}
},
{
"key": "meibai",
"doc_count": 1,
"avg_price": {
"value": 30
}
},
{
"key": "qingxin",
"doc_count": 1,
"avg_price": {
"value": 40
}
}
]
}
}
值得读的书
}
----------------------------------------------------------------------------------------------------------------
第四个数据分析需求:计算每个tag下的商品的平均价格,并且按照平均价格降序排序:terms 条件的意思
GET /ecommerce/product/_arch
{
"size": 0,
"aggs" : {
"all_tags" : {
"terms" : { "field" : "tags", "order": { "avg_price": "desc" } },
"aggs" : {
"avg_price" : {
"avg" : { "field" : "price" }
}
}
}
}
}
----------------------------------------------------------------------------------------------------------------
第五个数据分析需求:按照指定的价格范围区间进⾏分组,然后在每组内再按照tag进⾏分组,最后再计算每组的平均价格GET /ecommerce/product/_arch
{
"size": 0,
"aggs": {
"goup_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 20
},{
"from": 20,
"to": 40
},{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
结果:
{
"took": 72,
"timed_out": fal,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"goup_by_price": { "buckets": [
{
"key": "0.0-20.0",
"from": 0,
"to": 20,
"doc_count": 0,
"group_tags": {
"doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": []
}
},
{
"key": "20.0-40.0",
"from": 20,
"to": 40,
"doc_count": 2,
圣诞老人
"group_tags": {
"doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [
{
"key": "fangzhu",
"doc_count": 2,
"avg_price": {
"value": 27.5
}
},
{
"key": "meibai",
"doc_count": 1,
"avg_price": {

本文发布于:2023-07-14 22:44:07,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1096851.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字段   聚合   搜索   数据结构   需求   需要
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图