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"]
}
对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" }
}
}
}
aggregations.terms_all_tags.buckets.key:标签
aggregations.terms_all_tags.buckets.doc_count:该标签对应的商品数量
GET /product/book/_search
{
"size": 0,
"query": {
"match": {
"product_name": "shu"
}
},
"aggs": {
"terms_all_tags": {
"terms": {
"field": "all_tags"
}
}
}
}
GET /product/book/_search
{
"size": 0,
"aggs" : {
"terms_all_tags" : {
"terms" : { "field" : "all_tags" },
"aggs" : {
"avg_price" : {
"avg" : { "field" : "price" }
}
}
}
}
}
GET /product/book/_search
{
"size": 0,
"aggs" : {
"terms_all_tags" : {
"terms" : { "field" : "all_tags", "order": { "avg_price": "desc" } },
"aggs" : {
"avg_price" : {
"avg" : { "field" : "price" }
}
}
}
}
}
GET /product/book/_search
{
"size": 0,
"aggs": {
"range_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 50
},
{
"from": 50,
"to": 100
}
]
},
"aggs": {
"terms_all_tags": {
"terms": {
"field": "all_tags"
},
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}