[英]Elasticsearch aggregation using top_hits field with script ordering
I have a set of documents with src
, txt
and flt
fields. 我有一组带有src
, txt
和flt
字段的文档。 I want to query by txt
field in the following way: 我想通过以下方式通过txt
字段进行查询:
src
; 通过src
分组(存储桶); _score * doc.flt
value. 按_score * doc.flt
值对每个存储_score * doc.flt
。 So far I have implemented 1 and 2, but not 3. Even if 3 may be not very efficient, I still want to have such an option. 到目前为止,我已经实现了1和2,但没有实现3。即使3效率不是很高,我仍然希望有这样的选择。 My query looks like: 我的查询看起来像:
{
"query" : {
'match' : {
'text' : {
'query' : <some text>,
'fuzziness' : 'AUTO',
'operator' : 'and'
}
}
},
"aggs": {
"by_src": {
"terms": {
"field": "src",
"size" : 10,
"order" : {"top_score" : "desc"}
},
"aggs": {
"top_hits" : {
"top_hits" : {
"sort": { "_score": { "order": "desc" } },
"size" : 1
}
},
"top_score": {
"max" : {
"script" : "_score",
}
}
}
}
}
}
I believe it's failing because you don't need to use _source
field to apply the sort to each bucket, just apply the sort by the field name: 我相信它失败了,因为您不需要使用_source
字段将排序应用于每个存储桶,只需按字段名称应用排序:
{
"query" : {
'match' : {
'text' : {
'query' : <some text>,
'fuzziness' : 'AUTO',
'operator' : 'and'
}
}
},
"aggs": {
"by_src": {
"terms": {
"field": "src",
"size" : 10,
"order" : {"top_score" : "desc"}
},
"aggs": {
"top_hits" : {
"top_hits" : {
"sort":[{
"flt": {"order": "desc"}
}],
"size" : 1
}
},
"top_score": {
"max" : {
"script" : "_score",
}
}
}
}
}
}
I am assuming your document has a field called
flt
that you want to use to sort. 我假设您的文档中有一个要用于排序的名为flt
的字段。 Naturally you can also change the sorting toasc
if it's what you need. 当然,如果需要,您也可以将排序更改为asc
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.