Hi!
I've come across something that looks like a bug. When _score
is used in a cardinality aggregation, it always returns 0, no matter what the actual document scores are.
Here's an example that illustrates it. A function_score
query is created and it assigns custom scores based on filters. The results are then used in two aggregations, cardinality
and max
.
POST score-test/_doc/1
{
"letter": "A",
"number": "1"
}
POST score-test/_doc/2
{
"letter": "B",
"number": "1"
}
POST score-test/_doc/3
{
"letter": "A",
"number": "2"
}
POST score-test/_doc/4
{
"letter": "B",
"number": "2"
}
GET score-test/_search
{
"query": {
"function_score": {
"query": { "match_all": {} },
"functions": [
{
"filter": { "match": { "letter": "A" } },
"weight": 5
},
{
"filter": { "match": { "number": "1" } },
"weight": 10
}
],
"score_mode": "sum",
"boost_mode": "replace"
}
},
"aggs": {
"test_card": {
"cardinality": {
"script": {
"source": "_score"
}
}
},
"test_max": {
"max": {
"script": {
"source": "_score"
}
}
}
}
}
Here's what it spits out when run on ES 7.4.1:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 15.0,
"hits" : [
{
"_index" : "score-test",
"_type" : "_doc",
"_id" : "1",
"_score" : 15.0,
"_source" : {
"letter" : "A",
"number" : "1"
}
},
{
"_index" : "score-test",
"_type" : "_doc",
"_id" : "2",
"_score" : 10.0,
"_source" : {
"letter" : "B",
"number" : "1"
}
},
{
"_index" : "score-test",
"_type" : "_doc",
"_id" : "3",
"_score" : 5.0,
"_source" : {
"letter" : "A",
"number" : "2"
}
},
{
"_index" : "score-test",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"letter" : "B",
"number" : "2"
}
}
]
},
"aggregations" : {
"test_max" : {
"value" : 15.0
},
"test_card" : {
"value" : 1
}
}
}
The document scores are fine. The max
aggregation works as expected as well (I've tried some other metric aggs and they work, too).
However, the cardinality agg gives "1" instead of "4" (since we have 4 distinct scores). By trying more complex scripts, I've concluded that _score
(as well as score.doubleValue()
and the like) always return zero. This explains the "1" above (there is only one distinct value and it is 0).
Is this a bug, and if so, is there a fundamental limitation behind it?