简体   繁体   English

使用top_hits字段和脚本顺序进行Elasticsearch聚合

[英]Elasticsearch aggregation using top_hits field with script ordering

I have a set of documents with src , txt and flt fields. 我有一组带有srctxtflt字段的文档。 I want to query by txt field in the following way: 我想通过以下方式通过txt字段进行查询:

  1. Group (bucketize) by src ; 通过src分组(存储桶);
  2. In each bucket calculate top 1 most relevant document; 在每个存储桶中计算前1个最相关的文件;
  3. Order each bucket by the _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 to asc if it's what you need. 当然,如果需要,您也可以将排序更改为asc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM