# Can't access keySet of document on scripted query

**URL:** <https://discuss.elastic.co/t/cant-access-keyset-of-document-on-scripted-query/378097>\
**Category:** Elastic Search\
**Tags:** painless\
**Created:** [May 13, 2025, 6:29pm UTC](https://discuss.elastic.co/t/cant-access-keyset-of-document-on-scripted-query/378097 "2025-05-13T18:29:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![laxopix](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@laxopix](https://discuss.elastic.co/u/laxopix)\
**Post date:** [May 13, 2025, 6:29pm UTC](https://discuss.elastic.co/t/cant-access-keyset-of-document-on-scripted-query/378097/1 "2025-05-13T18:29:11Z")

</div>

I was trying to get all the document keys through the keySet() method in a scripted painless query

```auto
{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  "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.

```auto
{"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](https://discuss.elastic.co/t/in-a-painless-script-how-do-i-iterate-through-document-keys-based-on-a-pattern/238552/3), 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.
