I was trying to get all the document keys through the keySet() method in a scripted painless query
{
"query": {
"bool": {
"filter": {
"script": {
"script": """
double total = 0.0;
for (key in doc.keySet()) {
if (key.startsWith("total")) {
total = total + doc[key].value;
}
}
return total < 10;
"""
}
}
}
}
}
But I'm having a runtime unsupported_operation_exception error related to the keySet() method and not really sure why.
{"error":{"root_cause":[{"type":"script_exception","reason":"runtime error","script_stack":["org.elasticsearch.server@8.17.0/org.elasticsearch.search.lookup.LeafDocLookup.keySet(LeafDocLookup.java:236)","for (key in doc.keySet()) {\n "," ^---- HERE"],"script":" ...","lang":"painless","position":{"offset":60,"start":45,"end":87}}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"model_development","node":"UeH77ZZMRzq4Mb_oGYHcmg","reason":{"type":"script_exception","reason":"runtime error","script_stack":["org.elasticsearch.server@8.17.0/org.elasticsearch.search.lookup.LeafDocLookup.keySet(LeafDocLookup.java:236)","for (key in doc.keySet()) {\n "," ^---- HERE"],"script":" ...","lang":"painless","position":{"offset":60,"start":45,"end":87},"caused_by":{"type":"unsupported_operation_exception","reason":null}}}]},"status":400}
Basically is something similar to what is done here In a painless script how do I iterate through document keys based on a pattern? - #3 by gthang, but I was using doc.keySet() instead of _source.keySet() as i think _source is not available in a scripted query.
I'm new on elasticsearch so it seems i'm missing something pretty obvious, but can't figure out what. Any help will be greatly appreciated.