REPLACE in scripted field

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:

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:


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?

Hello mate,

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

I have to try this myself.

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:

{
  "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:

{
  "_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?

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