# Scripted terms always returns stringified values

**URL:** <https://discuss.elastic.co/t/scripted-terms-always-returns-stringified-values/256623>\
**Category:** Elasticsearch\
**Created:** [November 25, 2020, 9:17am UTC](https://discuss.elastic.co/t/scripted-terms-always-returns-stringified-values/256623 "2020-11-25T09:17:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![APic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/apic/32/78687_2.png) [@APic](https://discuss.elastic.co/u/APic)\
**Post date:** [November 25, 2020, 9:17am UTC](https://discuss.elastic.co/t/scripted-terms-always-returns-stringified-values/256623/1 "2020-11-25T09:17:01Z")

</div>

Hello,  
we are using script to execute terms on fields that are run-time created by source and we don't know their name in advance.  
Source documents:

```auto
{
                 "@timestamp": "2020-11-24T06:06:14.0058521+01:00",
                        "UnknownFieldName": 1,
                        "VariableName": "UnknownFieldName",
                  }

```

Here VariableName tells name of field that contains value and we want to execute term on field "UnknownFieldName".

We use the following query to retrieve values for each runtime variable:

```auto
       {
       "RunTimeVariableTerms":{
          "aggs":{
             "name":{
                "terms":{
                   "field":"Raw.VariableName.keyword"
                },
                "aggs":{
                   "RunTimeVarValues":{
                      "terms":{
                         "size":99,
                         "script":{
                            "lang":"painless",
                            "source":"String varName = doc['VariableName.keyword'].value; return doc[varName]"
                         }
                      }
                   }
                }
             }
          }
       }
    }

```

We obtain:

```auto
"RunTimeVariableTerms":{
   "doc_count":5,
   "Name":{
      "doc_count_error_upper_bound":0,
      "sum_other_doc_count":0,
      "buckets":[
         {
            "key":"UnknownFieldName",
            "doc_count":5,
            "RunTimeVarValues":{
               "doc_count_error_upper_bound":0,
               "sum_other_doc_count":0,
               "buckets":[
                  {
                     "key":"1",
                     "doc_count":5,
                     "type":{
                        "doc_count_error_upper_bound":0,
                        "sum_other_doc_count":0,
                        "buckets":[
                           {
                              "key":"int",
                              "doc_count":5
                           }
                        ]
                     }
                  }
               ]
            }
         }
      ]
   }
}

```

Values in the key field are always returned stringified, no matter the variable type while if we execute terms directly on field we obtain keys with original type. We need variable with it's own real type. How can we achieve it?

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 23, 2020, 9:17am UTC](https://discuss.elastic.co/t/scripted-terms-always-returns-stringified-values/256623/2 "2020-12-23T09:17:04Z")

</div>

Closed on December 23, 2020, 9:17am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
