计算每个tag下的商品数量
按照 tags 进行分组
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": "-_dfHkkASWCagwSHZacgoA","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}
大概意思是聚合分析要提前生成正排索引,你需要提前把需要聚合对字段设置为 fielddata = true,才会生成对应对正排索引。
所以需要调用这个接口将 tags 对 fielddata 设置为 true。
GET /ecommerce/_mapping/product{"properties": {"tags": {"type": "text","fielddata": true}}}
再执行聚合分析,返回
{"took": 2,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"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": "4","_score": 1,"_source": {"name": "heiren yagao","desc": "hei","price": 38,"producer": "heiren producer","tags": ["hei"]}},{"_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": "heiren yagao","desc": "hei","price": 38,"producer": "heiren producer","tags": ["hei"]}}]},"aggregations": {"group_by_tags": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "fangzhu","doc_count": 2},{"key": "hei","doc_count": 2},{"key": "meibai","doc_count": 1}]}}}{"took": 65,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"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": "4","_score": 1,"_source": {"name": "heiren yagao","desc": "hei","price": 38,"producer": "heiren producer","tags": ["hei"]}},{"_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": "heiren yagao","desc": "hei","price": 38,"producer": "heiren producer","tags": ["hei"]}}]},"aggregations": {"group_by_tags": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "fangzhu","doc_count": 2},{"key": "hei","doc_count": 2},{"key": "meibai","doc_count": 1}]}}}
对名称中包含yagao的商品,计算每个tag下的商品数量
GET /ecommerce/product/_search{"size": 0,"query": {"match": {"name": "yagao"}},"aggs": {"group_by_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": 13,"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": "hei","doc_count": 2,"avg_price": {"value": 38}},{"key": "meibai","doc_count": 1,"avg_price": {"value": 30}}]}}}
计算每个tag下的商品的平均价格并排序
GET /ecommerce/product/_search{"size": 0,"aggs": {"group_by_tags": {"terms": {"field": "tags","order": {"avg_price": "desc"}},"aggs": {"avg_price": {"avg": {"field": "price"}}}}}}
按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再算每组的平均价格
GET /ecommerce/product/_search{"size": 0,"aggs": {"group_by_price": {"range": {"field": "price","ranges": [{"from": 10,"to": 20},{"from": 20,"to": 30},{"from": 30,"to": 40}]},"aggs": {"group_by_tags": {"terms": {"field": "tags"},"aggs": {"average_price": {"avg": {"field": "price"}}}}}}}}// 返回结果{"took": 11,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 4,"max_score": 0,"hits": []},"aggregations": {"group_by_price": {"buckets": [{"key": "10.0-20.0","from": 10,"to": 20,"doc_count": 0,"group_by_tags": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": []}},{"key": "20.0-30.0","from": 20,"to": 30,"doc_count": 1,"group_by_tags": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "fangzhu","doc_count": 1,"average_price": {"value": 25}}]}},{"key": "30.0-40.0","from": 30,"to": 40,"doc_count": 3,"group_by_tags": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "hei","doc_count": 2,"average_price": {"value": 38}},{"key": "fangzhu","doc_count": 1,"average_price": {"value": 30}},{"key": "meibai","doc_count": 1,"average_price": {"value": 30}}]}}]}}}
