简体   繁体   English

ElasticSearch:_score字段上的聚合?

[英]ElasticSearch: aggregation on _score field?

I would like to use the stats or extended_stats aggregation on the _score field but can't find any examples of this being done (ie, seems like you can only use aggregations with actual document fields). 我想在_score字段上使用statsextended_stats聚合,但找不到任何这样做的例子(即,似乎你只能使用与实际文档字段的聚合)。

Is it possible to request aggregations on calculated "metadata" fields for each hit in an ElasticSearch query response (eg, _score , _type , _shard , etc.)? 是否可以针对ElasticSearch查询响应中的每个匹配请求计算“元数据”字段的聚合(例如, _score_type_shard等)?

I'm assuming the answer is 'no' since fields like _score aren't indexed... 我假设答案是'不',因为像_score这样的字段没有编入索引......

Note: The original answer is now outdated in terms of the latest version of Elasticsearch. 注意:根据最新版本的Elasticsearch,原始答案现已过时。 The equivalent script using Groovy scripting would be: 使用Groovy脚本的等效脚本将是:

{
    ...,
    "aggregations" : {
        "grades_stats" : { 
            "stats" : { 
                "script" : "_score" 
            } 
        }
    }
}

In order to make this work, you will need to enable dynamic scripting or, even better, store a file-based script and execute it by name (for added security by not enabling dynamic scripting)! 为了实现这一目标,您需要启用动态脚本,或者甚至更好地存储基于文件的脚本并按名称执行(为了通过不启用动态脚本来增加安全性)!


You can use a script and refer to the score using doc.score. 您可以使用脚本并使用doc.score引用分数。 More details are available in ElasticSearch's scripting documentation . ElasticSearch的脚本文档中提供了更多详细信息。

A sample stats aggregation could be: 样本统计聚合可以是:

{
    ...,
    "aggregations" : {
        "grades_stats" : { 
            "stats" : { 
                "script" : "doc.score" 
            } 
        }
    }
}

And the results would look like: 结果如下:

"aggregations": {
    "grades_stats": {
        "count": 165,
        "min": 0.46667441725730896,
        "max": 3.1525731086730957,
        "avg": 0.8296855776598959,
        "sum": 136.89812031388283
    }
}

A histogram may also be a useful aggregation: 直方图也可能是有用的聚合:

"aggs": {
    "grades_histogram": {
        "histogram": {
            "script": "doc.score * 10",
            "interval": 3
        }
    }
}

Histogram results: 直方图结果:

"aggregations": {
    "grades_histogram": {
        "buckets": [
            {
               "key": 3,
               "doc_count": 15
            },
            {
               "key": 6,
               "doc_count": 103
            },
            {
               "key": 9,
               "doc_count": 46
            },
            {
               "key": 30,
               "doc_count": 1
            }
        ]
    }
}

doc.score doesn't seem to work anymore. doc.score似乎不再起作用了。 Using _score seems to work perfectly. 使用_score似乎完美无缺。

Example: 例:

{
    ...,
    "aggregations" : {
        "grades_stats" : { 
            "stats" : { 
                "script" : "_score" 
            } 
        }
    }
}

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

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