Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the aggregation analysis of group by + avg + sort in ElasticSearch?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what the aggregate analysis such as group by + avg + sort in ElasticSearch is like, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Set the Fielddata property of the text fields to true

PUT http://{{es-host}}/ecommerce/_mapping/produce{ "properties": {"tags": {"type": "text" "fielddata": true}} 1. Calculate the number of goods under each tag GET http://{{es-host}}/ecommerce/produce/_search{ "size": 0 "aggs": {"group_by_tags": {"terms": {"field": "tags"}}

Group_by_tags stands for aggregate grouping name. You can write it at will and state the meaning clearly.

The value of field corresponds to the field to be aggregated

Results:

{"took": 43, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "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. Search and aggregate GET http://{{es-host}}/ecommerce/produce/_search{ by product name "query": {"match_phrase": {"name": "yagao"}} "aggs": {"group_by_tags": {"terms": {"field": "tags"}, "size": 0}

Search results:

{"took": 17, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "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} 3. Group first. Then calculate the average GET http://{{es-host}}/ecommerce/produce/_search{ "size": 0, "aggs": {"group_by_tags": {"terms": {"field": "tags"} "aggs": {"avg_price": {"avg": {"field": "price"} }}}

Results:

{"took": 83, "timed_out": false, "_ shards": {"total": 5, "successful": 5, "skipped": 0, "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} the above content is what the aggregation analysis such as group by + avg + sort in ElasticSearch looks like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report