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. Alternatively use a keyword field instead."
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "ecommerce",
"node": "z1ryhoWRRHWIjLJV8oiKUQ",
"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. Alternatively use a keyword field instead."
}
}
]
},
"status": 400
}
执行结果:
{
"acknowledged": true
}
此时需要进行设置fielddata为true
PUT /ecommerce/_mapping/product
{
"properties": {
"tags":{
"type":"text",
"fielddata": true
}
}
}
再次查询
{
"took": 254,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"name": "jiajieshi yao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "4",
"_score": 1,
"_source": {
"name": "special meibai yao",
"desc": "special meibai",
"price": 50,
"producer": "special yagao producer",
"tags": [
"meibai"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"name": "gaolujie yao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"fangzhu",
"meibai"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 1,
"_source": {
"name": "zhonghua yao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags": [
"qingxing"
]
}
}
]
},
"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": "qingxing",
"doc_count": 1
}
]
}
}
}
解释一下:
上面部分是查询到的数据
下面红色部分是聚合结果
"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": "qingxing",
"doc_count": 1
}
]
}
如果不需要查询到的原数据,那么可以加上一个size属性
GET /ecommerce/product/_search
{
"size":0,
"aggs":{
"group_by_tags":{
"terms": {
"field": "tags",
"size": 10
}
}
}
}
2.对名称中包含yao的商品,计算每个tag下的商品数量
GET /ecommerce/product/_search
{
"query":{
"match":{
"name": "yao"
}
},
"size":0,
"aggs":{
"group_by_tags":{
"terms": {
"field": "tags"
}
}
}
}
执行结果:
{
"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": "qingxing",
"doc_count": 1
}
]
}
}
}
3.先分组,再计算每组的商品价格的平均值
GET /ecommerce/product/_search
{
"size":0,
"aggs":{
"group_by_tags":{
"terms": {
"field": "tags"
},
"aggs":{
"avg_price":{
"avg": {
"field": "price"
}
}
}
}
}
}
结果:
{
"took": 5,
"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": "qingxing",
"doc_count": 1,
"avg_price": {
"value": 40
}
}
]
}
}
}
在这个基础上,按照平均价格降序排列
GET /ecommerce/product/_search
{
"size":0,
"aggs":{
"all_tags":{
"terms": {
"field": "tags",
"order": {"avg_price":"desc"}
},
"aggs":{
"avg_price":{
"avg": {"field":"price"}
}
}
}
}
}
4.按照指定的价格范围区间进行分组,然后在魅族内在按照tag进行分组,然后再计算price平均值
GET /ecommerce/product/_search
{
"size":0,
"aggs":{
"group_by_range":{
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 20
},{
"from":20,
"to":40
},{
"from":40,
"to":60
}
]
},
"aggs":{
"group_by_tags":{
"terms": {
"field": "tags",
"order": {
"avg_price": "desc"
}
},
"aggs":{
"avg_price":{
"avg":{
"field": "price"
}
}
}
}
}
}
}
}
执行结果:
{
"took": 346,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_range": {
"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": "meibai",
"doc_count": 1,
"avg_price": {
"value": 30
}
},
{
"key": "fangzhu",
"doc_count": 2,
"avg_price": {
"value": 27.5
}
}
]
}
},
{
"key": "40.0-60.0",
"from": 40,
"to": 60,
"doc_count": 2,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "meibai",
"doc_count": 1,
"avg_price": {
"value": 50
}
},
{
"key": "qingxing",
"doc_count": 1,
"avg_price": {
"value": 40
}
}
]
}
}
]
}
}
}