[英]ElasticSearch: aggregation on _score field?
我想在_score
字段上使用stats或extended_stats聚合,但找不到任何這樣做的例子(即,似乎你只能使用與實際文檔字段的聚合)。
是否可以針對ElasticSearch查詢響應中的每個匹配請求計算“元數據”字段的聚合(例如, _score
, _type
, _shard
等)?
我假設答案是'不',因為像_score
這樣的字段沒有編入索引......
注意:根據最新版本的Elasticsearch,原始答案現已過時。 使用Groovy腳本的等效腳本將是:
{
...,
"aggregations" : {
"grades_stats" : {
"stats" : {
"script" : "_score"
}
}
}
}
為了實現這一目標,您需要啟用動態腳本,或者甚至更好地存儲基於文件的腳本並按名稱執行(為了通過不啟用動態腳本來增加安全性)!
您可以使用腳本並使用doc.score引用分數。 ElasticSearch的腳本文檔中提供了更多詳細信息。
樣本統計聚合可以是:
{
...,
"aggregations" : {
"grades_stats" : {
"stats" : {
"script" : "doc.score"
}
}
}
}
結果如下:
"aggregations": {
"grades_stats": {
"count": 165,
"min": 0.46667441725730896,
"max": 3.1525731086730957,
"avg": 0.8296855776598959,
"sum": 136.89812031388283
}
}
直方圖也可能是有用的聚合:
"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
}
]
}
}
doc.score
似乎不再起作用了。 使用_score
似乎完美無缺。
例:
{
...,
"aggregations" : {
"grades_stats" : {
"stats" : {
"script" : "_score"
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.