# REPLACE in scripted field

**URL:** <https://discuss.elastic.co/t/replace-in-scripted-field/215703>\
**Category:** Elasticsearch\
**Created:** [January 20, 2020, 9:50am UTC](https://discuss.elastic.co/t/replace-in-scripted-field/215703 "2020-01-20T09:50:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anne\_Kim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anne_kim/32/47196_2.png) [@Anne\_Kim](https://discuss.elastic.co/u/Anne_Kim)\
**Post date:** [January 20, 2020, 9:50am UTC](https://discuss.elastic.co/t/replace-in-scripted-field/215703/1 "2020-01-20T09:50:00Z")

</div>

Hello,  
I'm trying to convert a string into a number by extracting dots with the help of regexp.  
Here is the scripted field for this:

```auto
if (doc['build.keyword'].size() != 0) {
    def m = /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/.matcher(doc['build.keyword'].value);
    if ( m.matches() ) {
        return m.replace(/./, '')
    } else {
   return null
    }
} else {
    return null
}

```

However it raises a following error:

 ![](https://us1.discourse-cdn.com/elastic/original/3X/3/e/3e98e21bb5249f26cdd4a1ed1c521073ac731dd9.png)  
The sub-string cannot be replaced in a scripted field? How can it be fixed?  
I suppose that I can find all groups consisting of digits and return them all, right?

---

<div class="post-metadata">

**Author:** ![grumo35](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grumo35/32/59451_2.png) [@grumo35](https://discuss.elastic.co/u/grumo35)\
**Post date:** [January 20, 2020, 2:12pm UTC](https://discuss.elastic.co/t/replace-in-scripted-field/215703/2 "2020-01-20T14:12:57Z")

</div>

Hello mate,

Can you provide your full request please ? and use cases ?

I have to try this myself.

---

<div class="post-metadata">

**Author:** ![Anne\_Kim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anne_kim/32/47196_2.png) [@Anne\_Kim](https://discuss.elastic.co/u/Anne_Kim)\
**Post date:** [January 20, 2020, 2:36pm UTC](https://discuss.elastic.co/t/replace-in-scripted-field/215703/3 "2020-01-20T14:36:35Z")

</div>

Hello! The script above fully represents my scripted field.  
The field "_build.keyword_" looks like " **x**. **x**. **x**. **xxxx**" string where **x** is a number. Here is the mapping of the index:

```auto
{
  "fps_data" : {
    "mappings" : {
      "properties" : {
        "build" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "created_at" : {
          "type" : "date"
        },
        "device" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "from" : {
          "type" : "long"
        },
        "gameplay_statistics_id" : {
          "type" : "long"
        },
        "is_tester" : {
          "type" : "boolean"
        },
        "max" : {
          "type" : "long"
        },
        "median" : {
          "type" : "long"
        },
        "middle" : {
          "type" : "long"
        },
        "min" : {
          "type" : "long"
        },
        "player_id" : {
          "type" : "long"
        },
        "room_id" : {
          "type" : "long"
        },
        "session_id" : {
          "type" : "long"
        },
        "to" : {
          "type" : "long"
        }
      }
    }
  }
}

```

A single document looks like:

```auto
{
  "_index": "fps_data",
  "_type": "data",
  "_id": "EZ7Sv28BJcrEI-8qDIPc",
  "_version": 1,
  "_score": null,
  "_source": {
    "gameplay_statistics_id": 27218,
    "min": 28,
    "max": 30,
    "middle": 30,
    "median": 29,
    "from": 0,
    "to": 10,
    "player_id": 520,
    "room_id": 38588,
    "created_at": "2020-01-19T19:45:45.000+00:00",
    "session_id": 47813,
    "build": "0.3.1.1992",
    "device": "meizu meizu note9",
    "is_tester": false
  },
  "fields": {
    "created_at": [
      "2020-01-19T19:45:45.000Z"
    ],
  },
  "sort": [
    1579463145000
  ]
}

```

The error (a screenshot above) appears when I open Discover of the _fps\_data_ index.  
Do you need any additional information?

---

<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:** [February 17, 2020, 2:36pm UTC](https://discuss.elastic.co/t/replace-in-scripted-field/215703/4 "2020-02-17T14:36:41Z")

</div>

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