博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Elasticsearch聚合分析
阅读量:6038 次
发布时间:2019-06-20

本文共 3785 字,大约阅读时间需要 12 分钟。

预先设置

在进行聚合分析的是皇后首先把文本的field的fielddata属性设置为true

PUT /ecommerce/_mapping/product{  "properties": {    "tags": {      "type": "text",      "fielddata": true    }  }}

 计算每个tag下的商品数量

GET /ecommerce/product/_search{  "aggs": {    "group_by_tags": {      "terms": { "field": "tags" }    }  }}

 结果

{  "took": 11,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "failed": 0  },  "hits": {    "total": 3,    "max_score": 1,    "hits": [  ......  "aggregations": {    "group_by_tags": {      "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/_search{  "size": 0,  "query": {    "match": {      "name": "yagao"    }  },  "aggs": {    "all_tags": {      "terms": {        "field": "tags"      }    }  }}

 先分组,再算每组的平均值,计算每个tag下的商品的平均价格

GET /ecommerce/product/_search{    "size": 0,    "aggs" : {        "group_by_tags" : {            "terms" : { "field" : "tags" },            "aggs" : {                "avg_price" : {                    "avg" : { "field" : "price" }                }            }        }    }}

 结果

{  "took": 22,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "failed": 0  },  "hits": {    "total": 3,    "max_score": 0,    "hits": []  },  "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下的商品的平均价格,并且按照平均价格降序排序

GET /ecommerce/product/_search{    "size": 0,    "aggs" : {        "all_tags" : {            "terms" : { "field" : "tags", "order": { "avg_price": "desc" } },            "aggs" : {                "avg_price" : {                    "avg" : { "field" : "price" }                }            }        }    }}

 结果

{  "took": 26,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "failed": 0  },  "hits": {    "total": 3,    "max_score": 0,    "hits": []  },  "aggregations": {    "all_tags": {      "doc_count_error_upper_bound": 0,      "sum_other_doc_count": 0,      "buckets": [        {          "key": "qingxin",          "doc_count": 1,          "avg_price": {            "value": 40          }        },        {          "key": "meibai",          "doc_count": 1,          "avg_price": {            "value": 30          }        },        {          "key": "fangzhu",          "doc_count": 2,          "avg_price": {            "value": 27.5          }        }      ]    }  }}

 按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再计算每组的平均价格

GET /ecommerce/product/_search{  "size": 0,  "aggs": {    "group_by_price": {      "range": {        "field": "price",        "ranges": [          {            "from": 0,            "to": 20          },          {            "from": 20,            "to": 40          },          {            "from": 40,            "to": 50          }        ]      },      "aggs": {        "group_by_tags": {          "terms": {            "field": "tags"          },          "aggs": {            "average_price": {              "avg": {                "field": "price"              }            }          }        }      }    }  }}

 

转载于:https://www.cnblogs.com/fmgao-technology/p/10410329.html

你可能感兴趣的文章
六款值得推荐的android(安卓)开源框架简介
查看>>
HT for Web可视化QuadTree四叉树碰撞检测
查看>>
http之get与post
查看>>
max_element( )
查看>>
CSS Grid 布局
查看>>
接口的幂等性
查看>>
java中的类
查看>>
android 自定义文字跑马灯 支持拖拽,按住停止滚动,自定义速度
查看>>
SpringMVC完成文件上传的基本步骤
查看>>
实例168 使用指针输出数组元素
查看>>
bind 与unbind
查看>>
CSS: Flexbox
查看>>
Python学习
查看>>
Java并发_volatile实现可见性但不保证原子性
查看>>
百度地图添加带数字标注
查看>>
【luogu 1908】逆序对
查看>>
pthread_create线程创建的过程剖析(转)
查看>>
android存储访问框架Storage Access Framework
查看>>
周总结
查看>>
Spring Boot 要点--启动类和热部署
查看>>