# Scripted fields versus scripts in discovery objects

**URL:** https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802
**Category:** Kibana
**Tags:** painless
**Created:** [March 1, 2023, 7:42pm UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802 "2023-03-01T19:42:12Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![gyannea](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@gyannea](https://discuss.elastic.co/u/gyannea)
#### Post date: [March 1, 2023, 7:42pm UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/1 "2023-03-01T19:42:12Z")

</div>

In Kibana I have found two ways to create an additional field based upon what is in the documents.

1. Use scripted fields in the index patterns. Since I want to convert a parameter that is in seconds to hours the script is

```auto
params._source.hubEvent.downtime / 3600.0;

```

with name 'downtimeInHrs'. Or

1. Place a full painless script into a search filter

```auto
{
  "script_fields": {
    "downtimeInHrs": {
      "script": {
        "lang": "painless",
        "source": "params._source.hubEvent.downtime * params.multiplier",
        "params": {
          "multiplier": 0.0002777777778
        }
      }
    }
  },
the filter 
...

```

Both generate the field 'downtimeInHrs' and in both cases they are visible in the discovered document and in the left hand column.

Using #1 I can use the field in visualizations. And the field is prefixed with '#'. I do not know what that '#' prefix means. However, if I do a search that does not contain a hubEvent.downtime, I get a null pointer exception. Doing an

```auto
if (params._source.hubEvent.downtime !=null)

```

does not solve the problem. There is still a null pointer exception.

Using #2 I cannot use the field in visualizations. In the left hand window of the discovery the field is prefixed by a '?'. There is no null pointer issue since the filter only works with docs that have a hubEvent.downtime field.

I am so confused. The docs do not explain how any of this works . I want to use the script in the discovery since my understanding is that it is much more efficient. But it is useless if I cannot use it in visualizations.

---

<div class="post-metadata">

### Author: ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)
#### Post date: [March 1, 2023, 9:54pm UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/3 "2023-03-01T21:54:41Z")

</div>

What version of Kibana are you using?

I'm not sure I understand how (2) is working for you. When you say "search filter", what do you mean? Are you referring to the query bar or the add filter dialog?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/2/42c90667ace90d8ed49839492c319d0cfa83ae31.png)

Regardless, both of the methods you described are deprecated and will be removed at some point. It is recommended that you use [runtime fields](https://www.elastic.co/guide/en/kibana/current/managing-data-views.html#runtime-fields) to accomplish what you're saying. These can be used both in Discover and inside visualizations.

---

<div class="post-metadata">

### Author: ![gyannea](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@gyannea](https://discuss.elastic.co/u/gyannea)
#### Post date: [March 1, 2023, 10:30pm UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/4 "2023-03-01T22:30:05Z")

</div>

Lucas,

I am using version 7.1.1. It does not have the runtime fields options. That sounds like a pretty cool option for what I am trying to do and hopefully it won't be so confusing. That comes in at 7.13 I think. I am not sure if I have control over the version I get to use....

In any case, I create the base search using the 'add filter' dialog using Lucene. I place the painless script is at the start of the search JSON and it converts the document parameter hubEvent.downtime (in seconds) to a new field 'downtimeInHrs'). It looks as follows:

```auto
{
  "script_fields": {
    "downtimeInHrs": {
      "script": {
        "lang": "painless",
        "source": "params._source.hubEvent.downtime * params.multiplier",
        "params": {
          "multiplier": 0.0002777777778
        }
      }
    }
  },
  "highlightAll": true,
  "version": true,
  "query": {
    "language": "lucene",
    "query": ""
  },
  "filter": [
    {
      "aggs": {
        "downtime_stats": {
          "stats": {
            "field": "hubEvent.downtime"
          }
        },
        "total_downtime": {
          "sum": {
            "field": "hubEvent.downtime"
          }
        }
      },
      "from": 0,
      "query": {
        "bool": {
          "filter": [
            {
              "range": {
                "hubEvent.downtime": {
                  "gte": 10
                }
              }
            }
          ],
          "must": [
            {
              "match": {
                "hubEvent.code": "qmiUp"
              }
            },
            {
              "exists": {
                "field": "hubEvent.downtime"
              }
            }
          ]
        }
      },
      "size": 25,
      "sort": [
        {
          "hubEvent.downtime": {
            "order": "desc"
          }
        }
      ],
      "meta": {
        "type": "custom",
        "disabled": false,
        "negate": false,
        "alias": "downtime",
        "key": "aggs",
        "value": "{\"downtime_stats\":{\"stats\":{\"field\":\"hubEvent.downtime\"}},\"total_downtime\":{\"sum\":{\"field\":\"hubEvent.downtime\"}}}",
        "indexRefName": "kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index"
      },
      "$state": {
        "store": "appState"
      }
    }
  ],
  "indexRefName": "kibanaSavedObjectMeta.searchSourceJSON.index"
}

```

So within the search I assure that hubEvent.downtime is present.

This search works as it gets the docs I want and there is a 'downtimeInHrs' field present, both appended to the document and it is listed in the left hand column. I can add the new field as a table and the values are correct. However, clicking on the new field shows values but no visualization option. So something is not right.

---

<div class="post-metadata">

### Author: ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)
#### Post date: [March 1, 2023, 10:57pm UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/5 "2023-03-01T22:57:21Z")

</div>

Ahh, I see. In the meantime, if you alter your script as follows, it should work for you in both Discover and Visualize:

```auto
return doc["hubEvent.downtime"].size() > 0 ? doc["hubEvent.downtime"].value / 3600.0 : null;

```

Let me know if that works!

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [March 2, 2023, 1:21am UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/6 "2023-03-02T01:21:09Z")

</div>

> [@gyannea](#):
>
> I am using version 7.1.1

Please note that version is very much [EOL](https://www.elastic.co/support/eol) and no longer supported, you should be looking to upgrade as a matter of urgency.

(Sorry I am not directly resolving your issue, but you are running a very old version!)

---

<div class="post-metadata">

### Author: ![gyannea](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@gyannea](https://discuss.elastic.co/u/gyannea)
#### Post date: [March 2, 2023, 10:09am UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/7 "2023-03-02T10:09:56Z")

</div>

This looks good. I have not yet got a null pointer exception. Also looking to update.

I am confused by the use of

```auto
doc['hubEvent.downtime']

```

versus

```auto
params._source.hubEvent.downtime

```

I could not get my simple one-line script to work using 'doc' but it did work with the 'params' variant. I do not understand what these two syntaxes are doing.

---

<div class="post-metadata">

### Author: ![gyannea](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@gyannea](https://discuss.elastic.co/u/gyannea)
#### Post date: [March 2, 2023, 10:11am UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/8 "2023-03-02T10:11:49Z")

</div>

I am looking to get the powers that be to upgrade, especially given what they want me to do. Would like to have that runtime option. Hope it is a simple as it sounds. It's certainly sounds like what I want to perform the task.

---

<div class="post-metadata">

### Author: ![gyannea](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@gyannea](https://discuss.elastic.co/u/gyannea)
#### Post date: [March 3, 2023, 1:29pm UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/9 "2023-03-03T13:29:48Z")

</div>

@lukas I have spent days trying to figure out how to capture the time period the user selects through the Kibana interface, for example, a month, year, week to date, etc. I started a new topic and have not gotten any response. It may not be possible.

What I want to display is, for example, the percent of time a device was down in the period picked by the user. This is not an indexed data field. But Kibana uses the range in its search.

```auto
        {
          "range": {
            "date": {
              "format": "strict_date_optional_time",
              "gte": "2023-02-01T05:00:00.000Z",
              "lte": "2023-03-02T15:11:30.784Z"
            }
          }
        }

```

Kibana can get those values but I can't!

---

<div class="post-metadata">

### Author: ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)
#### Post date: [March 8, 2023, 6:26pm UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/10 "2023-03-08T18:26:55Z")

</div>

Unfortunately there isn't a way I'm aware of to get those values in a scripted field.

What is the calculation you're trying to do? Can you give an example?

---

<div class="post-metadata">

### Author: ![gyannea](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@gyannea](https://discuss.elastic.co/u/gyannea)
#### Post date: [March 8, 2023, 7:02pm UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/11 "2023-03-08T19:02:42Z")

</div>

The user enters a time period using the timepicker. I want to calculate the percent of time a given device did not have connectivity during that period.

There was a request for this feature back in 2017 (several added they would also like it). The response was to make an issue requesting the feature. Guess it never got addressed.

---

<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: [April 5, 2023, 7:03pm UTC](https://discuss.elastic.co/t/scripted-fields-versus-scripts-in-discovery-objects/326802/12 "2023-04-05T19:03:11Z")

</div>

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