文章目录
演示前先往es写入三条商品数据用来演示查询
PUT /product/book/1
{
"product_name": "yuwen shu",
"price": 20,
"all_tags": ["bad","ugly"]
}
PUT /product/book/2
{
"product_name": "shuxue shu",
"price": 10,
"all_tags": ["good","ugly"]
}
1、统计每个标签对应的商品数量
对all_tags字段做聚合/排序/脚本访问我们需要先把all_tags字段默认关闭的fielddata开启
PUT /product/_mapping/book
{
"properties": {
"all_tags": {
"type": "text",
"fielddata": true
}
}
}
开始演示
GET /product/book/_search
{
"size": 0,
"aggs": {
"terms_all_tags": {
"terms": { "field": "all_tags" }
}
}
}