es进行聚合查询时,需要对结果进行排序,默认是对聚合结果的数量排序的,难以满足需求,可以通过嵌套的聚合解决,部分代码如下:
-
{
-
"track_scores":true,
-
"from":0,
-
"size":10,
-
"min_score": 0.2,
-
"query" : {
-
"filtered":{
-
"query":{
-
"query_string":{
-
"query": "'.$keyword.'",
-
"fields":["name1","name2^3","name3^3"]
-
}
-
}
-
}
-
},
-
"aggs":
-
{
-
"id1s":{
-
"terms":{
-
"field": "id1",
-
"size":15,
-
"order":{"max_score":"desc"}
-
},
-
"aggs":{
-
"max_score":{
-
"max":{
-
"script": "doc.rank"
-
}
-
}
-
}
-
},
-
"id2s":{
-
"terms":{
-
"field": "id2",
-
"size":10,
-
"order":{"max_score":"desc"}
-
},
-
"aggs":{
-
"max_score":{
-
"max":{
-
"script": "doc.rank"
-
}
-
}
-
}
-
}
-
},
-
"sort": [
-
{ "rank": { "order": "desc" }},
-
{ "_score": { "order": "desc" }}
-
]
-
}
因使用了script,会出现 “nested: ScriptException[dynamic scripting for [groovy] disabled]; }],[status] => 400,”,需要动态脚本支持:
在配置文件elasticsearch.yml文件末尾加上 “script.groovy.sandbox.enabled: true” 即可。
reference:
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
阅读(3682) | 评论(0) | 转发(0) |