# KQL and scripted fields

**URL:** https://discuss.elastic.co/t/kql-and-scripted-fields/272260
**Category:** Kibana
**Tags:** kql-kibana-query-language
**Created:** [May 6, 2021, 9:03am UTC](https://discuss.elastic.co/t/kql-and-scripted-fields/272260 "2021-05-06T09:03:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![thomasv95](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@thomasv95](https://discuss.elastic.co/u/thomasv95)
#### Post date: [May 6, 2021, 9:03am UTC](https://discuss.elastic.co/t/kql-and-scripted-fields/272260/1 "2021-05-06T09:03:23Z")

</div>

Hello, I've been having issues with KQL queries using scripted fields. Specifically, queries on numerical fields using operators such as "\<=" or "\>" throw the following error when using a Lens : `[lens_merge_tables] > [esaggs] > EsError`

It looks like I get a more detailed error using an Aggregation (see attached screenshot).  
 ![Capture d’écran 2021-05-06 à 09.32.42](https://us1.discourse-cdn.com/elastic/original/3X/0/2/02fdcd6a50f7b60dae632fe1d9236b2ab3ab7a9f.png)

Interestingly enough, the KQL operator ":" works just fine.

All I am doing in the scripted field is multiplying a numerical field by 2, and there are no missing values.

I am at a complete loss since the Kibana documentation clearly states that all KQL operators are compatible with scripted fields.

I absolutely need to use KQL filtering since I'm using a Filter Ratio in the TSVB tool.

Your support would be much appreciated. Thank you

---

<div class="post-metadata">

### Author: ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)
#### Post date: [May 6, 2021, 12:15pm UTC](https://discuss.elastic.co/t/kql-and-scripted-fields/272260/2 "2021-05-06T12:15:40Z")

</div>

hello,

This seems like a bug indeed. If you're using ES \> 7.11 you can solve this issue by changing from scripted fields to runtime fields.  
For example, if you had a scripted field like this:  
`doc['FlightDelayMin'].value * 2`

you can change it to a runtime field from the Dev Tools

```auto
PUT kibana_sample_data_flights/_mapping
{
    "runtime": {
      "minz": {
        "type": "long",
        "script": {
          "source": "emit(doc['FlightDelayMin'].value * 2)"
        }
      }
    }
}

```

where `kibana_sample_data_flights` is to be replaced with your index pattern name and `minz` with your field name.

---

<div class="post-metadata">

### Author: ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)
#### Post date: [May 6, 2021, 12:19pm UTC](https://discuss.elastic.co/t/kql-and-scripted-fields/272260/3 "2021-05-06T12:19:14Z")

</div>

And this is the issue for the scripted fields error: [[kql] Creating filters with numeric scripted fields sends incorrect type · Issue #98761 · elastic/kibana · GitHub](https://github.com/elastic/kibana/issues/98761)

---

<div class="post-metadata">

### Author: ![thomasv95](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@thomasv95](https://discuss.elastic.co/u/thomasv95)
#### Post date: [May 7, 2021, 7:46am UTC](https://discuss.elastic.co/t/kql-and-scripted-fields/272260/4 "2021-05-07T07:46:49Z")

</div>

Hi Marius,

Thank you for your detailed response, using runtime fields did indeed provide a quick fix to my problem. I just saw this morning that a PR was made to solve this bug in Kibana (if I'm not mistaken). My team and I hope that it can be resolved ASAP as we are scheduled to deliver our prototype in the upcoming weeks to be used by non-technical end users who would prefer using scripted fields rather than runtime fields.

Thank you again for your time and support.

---

<div class="post-metadata">

### Author: ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)
#### Post date: [May 8, 2021, 7:08pm UTC](https://discuss.elastic.co/t/kql-and-scripted-fields/272260/5 "2021-05-08T19:08:12Z")

</div>

Just to manage the expectations, that PR is set to go in 7.14.0 which is still some time away. Right now 7.13.0 is close to being released in a couple weeks and that fix won't get in it due to code freeze. I wouldn't expect 7.14.0 until mid summer ☹

---

<div class="post-metadata">

### Author: ![thomasv95](https://avatars.discourse-cdn.com/v4/letter/t/9dc877/32.png) [@thomasv95](https://discuss.elastic.co/u/thomasv95)
#### Post date: [May 11, 2021, 8:15am UTC](https://discuss.elastic.co/t/kql-and-scripted-fields/272260/6 "2021-05-11T08:15:41Z")

</div>

Ok, thanks for letting me know, I will pass the message on to my team.

---

<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: [June 8, 2021, 8:15am UTC](https://discuss.elastic.co/t/kql-and-scripted-fields/272260/7 "2021-06-08T08:15:56Z")

</div>

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