弹性搜索:_score字段上的聚合? [英] ElasticSearch: aggregation on _score field?

查看:74
本文介绍了弹性搜索:_score字段上的聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用统计信息 extended_stats _score 字段上的聚合,但找不到任何这样做的示例(即,您似乎只能使用具有实际文档字段的聚合)

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 (i.e., seems like you can only use aggregations with actual document fields).

可以在ElasticSearch查询响应中的每个匹配项的计算元数据字段上请求聚合(例如, _score _type _shard 等)?

Is it possible to request aggregations on calculated "metadata" fields for each hit in an ElasticSearch query response (e.g., _score, _type, _shard, etc.)?

我假设答案是不,因为像 _score 这样的字段没有编入索引...

I'm assuming the answer is 'no' since fields like _score aren't indexed...

推荐答案

注意:原来的答案现在已经过时了, Elasticsearch的离子。使用Groovy脚本的等效脚本将是:

Note: The original answer is now outdated in terms of the latest version of Elasticsearch. The equivalent script using Groovy scripting would be:

{
    ...,
    "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)!

您可以使用脚本,并使用doc.score参考分数。更多详细信息,请参阅ElasticSearch的脚本文档

You can use a script and refer to the score using doc.score. More details are available in ElasticSearch's scripting documentation.

样本统计聚合可以是:

{
    ...,
    "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 直方图也可能是一个有用的聚合:

A histogram may also be a useful aggregation:

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

直方图结果:

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

这篇关于弹性搜索:_score字段上的聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆