ES分组聚合:计算每个tag下的商品数量且某个filed包含指定关键字,分组,平均,每个tags下的平均价格,排序,指定范围区间
生活随笔
收集整理的這篇文章主要介紹了
ES分组聚合:计算每个tag下的商品数量且某个filed包含指定关键字,分组,平均,每个tags下的平均价格,排序,指定范围区间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、第一個分析需求:計算每個tag下的商品數量
GET /ecommerce/product/_search {"aggs": {"group_by_tags": {"terms": { "field": "tags" }}} }執行之后的結果是:
{"error": {"root_cause": [{"type": "illegal_argument_exception","reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [tags] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}],"type": "search_phase_execution_exception","reason": "all shards failed","phase": "query","grouped": true,"failed_shards": [{"shard": 0,"index": "ecommerce","node": "urqovJ9yQPCO6fNM70Lc8w","reason": {"type": "illegal_argument_exception","reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [tags] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}}],"caused_by": {"type": "illegal_argument_exception","reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [tags] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory."}},"status": 400 }上面的報錯的意思是要將文本field的fielddata屬性設置為true
PUT /ecommerce/_mapping/product {"properties": {"tags": {"type": "text","fielddata": true}} }設置完成之后的效果是:
{"acknowledged": true }然后再執行下面的操作:
GET /ecommerce/product/_search {"aggs": {"group_by_tags": {"terms": {"field": "tags"}}} }執行,然后看最后面的結果:
"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": 2},{"key": "qingxin","doc_count": 1}]} }說明按照tags里面的內容進行了buckets分組統計,可以看到每個tags出現的次數。
GET /ecommerce/product/_search {"size": 0,"aggs": {"all_tags": {"terms": { "field": "tags" }}} } {"took": 20,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"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},{"key": "meibai","doc_count": 2},{"key": "qingxin","doc_count": 1}]}} }2、第二個聚合分析的需求:對名稱中包含yagao的商品,計算每個tag下的商品數量
GET /ecommerce/product/_search {"size": 0,"query": {"match": {"name": "yagao"}},"aggs": {"all_tags": {"terms": {"field": "tags"}}} }運行結果是:
{"took": 6,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"max_score": 0,"hits": []},"aggregations": {"all_tags": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "fangzhu","doc_count": 2},{"key": "meibai","doc_count": 2},{"key": "qingxin","doc_count": 1}]}} }3、第三個聚合分析的需求:先分組,再算每組的平均值,計算每個tag下的商品的平均價格
GET /ecommerce/product/_search {"size": 0,"aggs" : {"group_by_tags" : {"terms" : { "field" : "tags" },"aggs" : {"avg_price" : {"avg" : { "field" : "price" }}}}} } {"took": 8,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"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": 2,"avg_price": {"value": 40 }},{"key": "qingxin","doc_count": 1,"avg_price": {"value": 40 }}]}} }4、第四個數據分析需求:計算每個tag下的商品的平均價格,并且按照平均價格降序排序
GET /ecommerce/product/_search {"size": 0,"aggs" : {"all_tags" : {"terms" : { "field" : "tags", "order": { "avg_price": "desc" } },"aggs" : {"avg_price" : {"avg" : { "field" : "price" }}}}} }下面的語句的意思是:按照tags進行分組,并按照它里面的平均值進行降序排列
"terms" : { "field" : "tags", "order": { "avg_price": "desc" } }上面的運行結果是:
{"took": 3,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"max_score": 0,"hits": []},"aggregations": {"all_tags": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "meibai","doc_count": 2,"avg_price": {"value": 40 }},{"key": "qingxin","doc_count": 1,"avg_price": {"value": 40 }},{"key": "fangzhu","doc_count": 2,"avg_price": {"value": 27.5 }}]}} }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"}}}}}}} }最終的結果:
{"took": 61,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"max_score": 0,"hits": []},"aggregations": {"group_by_price": {"buckets": [{"key": "0.0-20.0","from": 0,"to": 20,"doc_count": 0,"group_by_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_by_tags": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [ { "key": "fangzhu", "doc_count": 2, "average_price": { "value": 27.5 } }, { "key": "meibai", "doc_count": 1, "average_price": { "value": 30 } } ] }},{"key": "40.0-50.0","from": 40,"to": 50,"doc_count": 1,"group_by_tags": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [ { "key": "qingxin", "doc_count": 1, "average_price": { "value": 40 } } ] }}]}} } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的ES分组聚合:计算每个tag下的商品数量且某个filed包含指定关键字,分组,平均,每个tags下的平均价格,排序,指定范围区间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 钱站提前还款能减免吗
- 下一篇: ES分布式机制的透明性,垂直扩容和水平扩