一、 先将文本field的fielddata属性设置为true
PUT /ecommerce/_mapping
{
"properties":{
"tags":{
"type":"text",
"fielddata":true
}
}
}
二、统计每个tag下的商品数量
A、GET /ecommerce/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
三、 对名称中包含yagao的商品,计算每个tag下的数量
GET /ecommerce/_search
{
"query": {
"match": {
"name": "yagao"
}
}
, "aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
四、计算每个tag下的平均价格
GET /ecommerce/_search
{
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags",
"order": {
"avg_price": "desc"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
五、 按照指定价格区间进行分组,然后在每组内按照tag进行分组,最后再计算每组的平均价格
GET /ecommerce/_search
{
"size": 0,
"aggs": {
"group_by_range": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 30
},
{
"from": 30,
"to":60
}
]
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
原文地址:https://www.cnblogs.com/DjanFey/p/12114578.html