What is the aggregation analysis of group by + avg + sort in ElasticSearch?
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.