There are lot of questions and answers about this but still didn\'t get a satisfied answers. Elasticsearch version: 6.5
Index mapping
\"_doc\": {
Despite the fact that this would've been slow and against the guidance of not accessing _source
in search queries, the query would have worked before v6.4.
After v6.4, thanks to an unintentional side effect of refactoring, accessing _source
in the script query context is not possible.
With that being said, we can "hijack" a function_score
query whose Painless context still has access to the _source
:
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"functions": [
{
"script_score": {
"script": {
"source": "params._source.containsKey('nested_field') && params._source['nested_field'] != null && params._source.nested_field.size() > 1 ? 1 : 0"
}
}
}
],
"min_score": 1
}
}
}
You could use a pipeline
to calculate the field size and then save it on the doc's top level.
Alternatively, you could use copy_to
as outlined in my related answer.