数据库中的count/min/max/avg group by在全文检索中称为聚合查询
查询条件中使用aggregations属性:
"aggregations" : {
"<aggregation_name>" : {
"<aggregation_type>" : {
<aggregation_body>
}
[,"meta" : { [<meta_data_body>] } ]?
[,"aggregations" : { [<sub_aggregation>]+ } ]?
}
[,"<aggregation_name_2>" : { ... } ]*
}
例如:
"aggregations": { "STOREID": { "terms": { "field": "bill.STOREID", "order": { "_term": "desc" } }, "aggregations": { "COUNTStore": { "value_count": { "field": "_index" } }, "MAXGOODSID": { "max": { "field": "id" } } } } }
- 根据storeID进行了分组查询
- 分组后,查询了所有匹配的记录条数,和其中最大的索引ID
- 具体说明,见官网 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html
- 聚合查询中文说明,注意英文官网说明比较全面https://www.elastic.co/guide/cn/elasticsearch/guide/current/_filter_bucket.html
"max": {
"filed": "_score"
}
如何但是如果想要得到score也是可行的,只要如此:
- aggregations的指标属性(Metrics)设置为:
"max": { "script": "_score" }
- 光这样可能查询报错:scripts of type [inline], operation [aggs] and lang [groovy] are disabled,那么只需在配置文件elasticsearch.yml里添加一些参数:
script.inline: on script.indexed: on script.engine.groovy.inline.aggs: on script.engine.groovy.inline.update: on
就能在聚合查询是,对score做max,min,avg了 可以参考https://elasticsearch.cn/question/794