According to the documentation of inner_hits it should be possible to use a script to sort the nested inner_hits of a document.
But I can't get it to work - the _score turns NULL as soon as I use it in an script.
An example:
PUT /books
{
"mappings": {
"_doc": {
"properties": {
"editions": {
"type": "nested"
}
}
}
}
}
PUT books/_doc/1?refresh
{
"bookratings": 25,
"editions": [
{
"title": "harry potter I",
"ratings": 10
},
{
"title": "harry potter 1",
"ratings": 15
}
]
}
PUT books/_doc/2?refresh
{
"bookratings": 19,
"editions": [
{
"title": "harry potter II",
"ratings": 7
},
{
"title": "harry potter 2",
"ratings": 12
}
]
}
POST /books/_search
{
"_source": [
"bookratings"
],
"query": {
"function_score": {
"query": {
"nested": {
"path": "editions",
"score_mode": "max",
"query": {
"bool": {
"must": [
{
"match": {
"editions.title": {
"query": "harry",
"operator": "and"
}
}
}
]
}
},
"inner_hits": {
"sort": {
"_script": {
"type": "number",
"script": "return _score * doc['editions.ratings'].value;"
}
},
"docvalue_fields": [
"editions.ratings.keyword"
]
}
}
},
"script_score": {
"script": "return _score * doc['bookratings'].value;"
}
}
}
}
So I get
"_score" : null,
"sort" : [
0.0
]
Instead when I remove the script from inside "inner_hits" a score is shown.
"_score" : 0.18232156,
Is that a bug? Am I the bug?