# Static variables in Kibana discovery script

**URL:** <https://discuss.elastic.co/t/static-variables-in-kibana-discovery-script/327125>\
**Category:** Kibana\
**Created:** [March 6, 2023, 9:50pm UTC](https://discuss.elastic.co/t/static-variables-in-kibana-discovery-script/327125 "2023-03-06T21:50:03Z")\
**Posts on this page:** 8\
**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 6, 2023, 9:50pm UTC](https://discuss.elastic.co/t/static-variables-in-kibana-discovery-script/327125/1 "2023-03-06T21:50:03Z")

</div>

What should be something very simple.  
I want to create a new field for a Kibana discovery and dashboard that simply counts the received document. So each document will have an index 0, 1, 2, ..., n. However, I do not see anyway to initialize a static field so I can do something simple like

```auto
int index = static_index; // I need this initialized at the beginning of the search and not after
index++
static_index = index;
return index;

```

Is it possible to make two scripts and access my static field using

```auto
int index = doc["my_static_field"].value()
index++
doc['my_static_field'].value() = index

```

where 'my\_created\_field' is my static value

---

<div class="post-metadata">

**Author:** ![nickpeihl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickpeihl/32/112622_2.png) [@nickpeihl](https://discuss.elastic.co/u/nickpeihl)\
**Post date:** [March 7, 2023, 3:17pm UTC](https://discuss.elastic.co/t/static-variables-in-kibana-discovery-script/327125/2 "2023-03-07T15:17:10Z")

</div>

Hi @gyannea. I don't believe it is possible to increment a variable in a field. Variables defined in a runtime field are limited to the scope of the returned document.

Can you explain why you want to define an index field? Maybe we can figure out another solution.

---

<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 7, 2023, 3:56pm UTC](https://discuss.elastic.co/t/static-variables-in-kibana-discovery-script/327125/3 "2023-03-07T15:56:07Z")

</div>

I am trying to get as close as possible to the user-entered timepicker values so I can compute things like the 'percent of time cellular connectivity was down' over the user-specified interval. There is no way to get that range from a script. It was asked for back in 2017 and the responder said to submit an enhancement request but as far as I know it has not been done.

If I could create a 'count' for each document received from 1 to N in time order, I could use the time stamp of the document with count 1 as the start time and document N as the end time. Not exactly the same, but the best I can do.

---

<div class="post-metadata">

**Author:** ![nickpeihl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickpeihl/32/112622_2.png) [@nickpeihl](https://discuss.elastic.co/u/nickpeihl)\
**Post date:** [March 8, 2023, 2:09pm UTC](https://discuss.elastic.co/t/static-variables-in-kibana-discovery-script/327125/4 "2023-03-08T14:09:12Z")

</div>

Thanks for the additional information. Does each document have date fields that indicate when connectivity was lost and then gained? Or do you record connectivity connection/losses in separate documents?

Either way, I don't think it's possible to accomplish this by adding a field in Discover. But it might be possible to use a [Lens visualization with formulas](https://www.elastic.co/guide/en/kibana/current/lens.html#lens-formulas).

---

<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, 3:06pm UTC](https://discuss.elastic.co/t/static-variables-in-kibana-discovery-script/327125/5 "2023-03-08T15:06:14Z")

</div>

They do, but if there is only one such event in the middle of June you won't be able to report what percentage of the time that there was connectivity during the month of June. Of course we would like NO events so we could report 100% but that is an unlikely scenario unless the time period is pretty short.

The month of June is just an example of a user-selected time period. It can be anything (a year, a day, a week, etc.)

---

<div class="post-metadata">

**Author:** ![nickpeihl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickpeihl/32/112622_2.png) [@nickpeihl](https://discuss.elastic.co/u/nickpeihl)\
**Post date:** [March 9, 2023, 3:27pm UTC](https://discuss.elastic.co/t/static-variables-in-kibana-discovery-script/327125/6 "2023-03-09T15:27:18Z")

</div>

I understand. Sorry, I don't have any other ideas at this time. Would you be willing to provide any example documents and index mapping? Maybe that would trigger some ideas. But we could also be running into a feature gap that we just can't solve at this time.

---

<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 9, 2023, 3:41pm UTC](https://discuss.elastic.co/t/static-variables-in-kibana-discovery-script/327125/7 "2023-03-09T15:41:23Z")

</div>

I'm not exactly sure how to do that sharing ... but in the end I am pretty sure you cannot do this relying just on the archived documents. Access to that user input is essential. (I see that the Kibana searches access that range of course!)

---

<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 6, 2023, 3:42pm UTC](https://discuss.elastic.co/t/static-variables-in-kibana-discovery-script/327125/8 "2023-04-06T15:42:08Z")

</div>

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