# Scripted fields for match(regex)

**URL:** <https://discuss.elastic.co/t/scripted-fields-for-match-regex/129297>\
**Category:** Kibana\
**Created:** [April 24, 2018, 11:30am UTC](https://discuss.elastic.co/t/scripted-fields-for-match-regex/129297 "2018-04-24T11:30:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![1862347ba9566e72a47d](https://avatars.discourse-cdn.com/v4/letter/1/cdc98d/32.png) [@1862347ba9566e72a47d](https://discuss.elastic.co/u/1862347ba9566e72a47d)\
**Post date:** [April 24, 2018, 11:30am UTC](https://discuss.elastic.co/t/scripted-fields-for-match-regex/129297/1 "2018-04-24T11:30:00Z")

</div>

How to use scripted fields to find count of match a number several digits at the beginning of a value in a field with a string type?  
For example, there is  
doc1 with a text field = 12345  
doc2 with text = 12678  
doc3 with text = 12809  
doc4 with text = 13098  
It is necessary to find the number of documents for which the field contains '12'

---

<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:** [April 24, 2018, 12:54pm UTC](https://discuss.elastic.co/t/scripted-fields-for-match-regex/129297/2 "2018-04-24T12:54:20Z")

</div>

This can be done easily without using scripted fields.  
Just create a Metric visualization, with "Count" as the metric and then in the query bar write:  
`<name of the field>:12*`  
Example:  
`number:12*`

---

<div class="post-metadata">

**Author:** ![1862347ba9566e72a47d](https://avatars.discourse-cdn.com/v4/letter/1/cdc98d/32.png) [@1862347ba9566e72a47d](https://discuss.elastic.co/u/1862347ba9566e72a47d)\
**Post date:** [April 24, 2018, 1:40pm UTC](https://discuss.elastic.co/t/scripted-fields-for-match-regex/129297/3 "2018-04-24T13:40:28Z")

</div>

Thanks for the answer. But I need to create a new script field for further calculations.  
Only for this value.

---

<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:** [April 24, 2018, 1:57pm UTC](https://discuss.elastic.co/t/scripted-fields-for-match-regex/129297/4 "2018-04-24T13:57:55Z")

</div>

Unfortunately, scripted fields work only on values within each individual doc. So if a doc had 3 numeric fields you could use a scripted field to add them together, or average them, or find the min, max, etc. But not across all docs or a set of docs.  
But you can also use some scripting within Visualizations in the Advanced JSON field, not sure how much it would help you as I don't know what you need to do with the value after.  
Here's an example:

> [@Display concurrency in data on Kibana](https://discuss.elastic.co/t/display-concurrency-in-data-on-kibana/26006/13):
>
> It worked for me with the following data and config: curl -XPOST 'http://localhost:9200/test3' -d '{ "settings" : { "number\_of\_shards" : 1 }, "mappings" : { "test" : { "properties" : { "start\_date" : { "type" : "date"}, "end\_date" : { "type" : "date"}, "duration" : { "type" : "integer"} } } } }' curl -XPUT 'http://localhost:9200/test3/test/1' -d '{ "start\_date" : "2015-08-23T00:0…

---

<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:** [May 22, 2018, 1:58pm UTC](https://discuss.elastic.co/t/scripted-fields-for-match-regex/129297/5 "2018-05-22T13:58:07Z")

</div>

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