第一個(gè)分析需求:計(jì)算每個(gè)tag下的商品數(shù)量
首先得將文本field的fielddata屬性設(shè)置為true
PUT /ecommerce/_mapping/product
{
"properties": {
"tags": {
"type": "text",
"fielddata": true
}
}
}
aggs碎赢、terms是關(guān)鍵詞荚虚,group_by_tags可以名字任取
GET /ecommerce/product/_search
{
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
第二個(gè)分析需求:對(duì)名稱中包含yagao的商品羞海,計(jì)算每個(gè)tag下的商品數(shù)量
GET /ecommerce/product/_search
{
"size": 0,
"query": {
"match": {
"name": "yagao"
}
},
"aggs": {
"all_tags": {
"terms": {
"field": "tags"
}
}
}
}
第三個(gè)分析需求:先分組,再算每組的平均值曲管,計(jì)算每個(gè)tag下的商品的平均價(jià)格
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
第四個(gè)分析需求:計(jì)算每個(gè)tag下的商品的平均價(jià)格却邓,并且按照平均價(jià)格降序排序
GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"all_tags": {
"terms": {
"field": "tags",
"order": {
"avg_price": "desc"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
第五個(gè)分析需求:按指定的價(jià)格范圍區(qū)間進(jìn)行分組,然后每組內(nèi)再按照tag進(jìn)行分組院水,最后計(jì)算每組的平均價(jià)格
{
"size": 0,
"aggs": {
"group_by_price": {
"range": {
"field": "price",
"ranges": [{
"from": 0,
"to": 20
}, {
"from": 20,
"to": 40
}, {
"from": 40,
"to": 50
}]
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}