简体   繁体   English

ElasticSearch 5.0:_score字段上的聚合?

[英]ElasticSearch 5.0: aggregation on _score field?

I'm migrating my Elasticsearch queries from the 1.7 version to 5.0 (the latest at this moment) and I'm having some trouble with aggregations. 我正在将我的Elasticsearch查询从1.7版本迁移到5.0(目前是最新版本),并且在聚合方面遇到了一些麻烦。

I want to do aggregation on a given field and show the top 5 documents ordered by score. 我想在给定的字段上进行汇总,并显示按得分排序的前5个文档。 From what I've read this used to be done like this: 从我阅读的内容来看,以前是这样完成的:

"aggs" : {
    "max_price" : { "max" : { "script" : "_score" } }
}

Using Sense, the answer I get is "Unexpected token VALUE_STRING [script] in [top_score]." 使用Sense,我得到的答案是“ [top_score]中的意外令牌VALUE_STRING [script]”。

Anyone went through this? 有人经历过这个吗?

Note: For an older version an answer was posted here: ElasticSearch: aggregation on _score field? 注意:对于较旧的版本,此处发布了答案: ElasticSearch:_score字段上的聚合?

The default scripting language for Elasticsearch is now Painless instead of groovy. Elasticsearch的默认脚本语言现在是无痛而不是普通的。 According to How to use scripts , you could try: 根据如何使用脚本 ,您可以尝试:

"aggs" : {
    "max_price": {
        "max": {
            "script": {
                "lang": "groovy", 
                "inline": "_score"
            } 
        } 
    }
}

Updated in Elasticsearch 5.x, there will be deprecated logs like: 在Elasticsearch 5.x中进行了更新,将不再推荐使用以下日志:

[WARN ][oedsgGroovyScriptEngineService] [groovy] scripts are deprecated, use [painless] scripts instead [WARN] [oedsgGroovyScriptEngineService] [groovy]脚本已弃用,请改用[painless]脚本

The more correct way is: 更正确的方法是:

"aggs" : {
    "max_price": {
        "max": {
            "script": {
                "lang": "painless", 
                "inline": "_score"
            } 
        } 
    }
}

If you just need the top 5 documents by score, overall, for your search terms, you can just set the size of your query to 5 and that should do the trick (eg below) 如果您只需要按分数作为总分最高的5个文档作为搜索词,则只需将查询的大小设置为5,就可以解决问题(例如,下面的方法)

{ "size": 5, "query": { "bool": { "must": [ { "match": { "my_field": "whatever" } }, { "match": { "my_other_field": "whatever else" } } ], "should": [], "must_not": [], "filter": [] } }, "aggs": { "something": { "terms": { "field": "my_term" }, "aggs": { "field_stats": { "stats": { "field": "price" } } } } } }

On the other hand, if you want the top 5 scoring documents for each bucket, have you considered using a top hits aggregation? 另一方面,如果您想为每个存储区分配得分最高的5个文档,是否考虑过使用热门匹配? https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html https://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html

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

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