主要知识点:
- 搜索+聚合
- 过滤+聚合
需求是:找出所有价格大于1200元的电视,然后对之些电视求平均价格
语法:
GET /tvs/sales/_search
{
"size": 0,
"query": {
"constant_score": {"filter": {"range": {"price": {"gte": 1200}}}}
},
"aggs": {"avg_price": {"avg": {"field": "price"}}}
}
执行结果是:
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 0,
"hits": []
},
"aggregations": {
"avg_price": {
"value": 2885.714285714286
}
}
}