# Reindex text field script manipulation

**URL:** <https://discuss.elastic.co/t/reindex-text-field-script-manipulation/304376>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [May 10, 2022, 4:47pm UTC](https://discuss.elastic.co/t/reindex-text-field-script-manipulation/304376 "2022-05-10T16:47:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wesley\_Acheson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wesley_acheson/32/46771_2.png) [@Wesley\_Acheson](https://discuss.elastic.co/u/Wesley_Acheson)\
**Post date:** [May 10, 2022, 4:47pm UTC](https://discuss.elastic.co/t/reindex-text-field-script-manipulation/304376/1 "2022-05-10T16:47:30Z")

</div>

Hi,

We have been indexing some documents for some time now. Unfortunately one of the text fields is incorrect. However the correct value of the field does appear as a substring within the document.

On a basic level ignoring other fields the document looks like this.

```auto
{
  "field_id": <number>,
  "compositeField" : "<number><string value to extract>"
}

```

What I want to do is for every document reindex the composite field to a new field removing the initial part of the string which is always the same as "field\_id"

I haven't tried using the reindex but I have tried some basic expressions using the dev tools in kibana.

```auto
GET index-pattern-*/_search
{
  "query": {
    "match_all": {}
  },
    "script_fields": {
    "new_field": {
      "script": {
        "source": "field('compositeField').get(null)"
      }
    }
  }
}

```

However even without doing any manipulation to the source field. it returns an error response.

```auto
          "script_stack" : [
            "org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:875)",
            "org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:112)",
            "org.elasticsearch.index.query.SearchExecutionContext.lambda$lookup$2(SearchExecutionContext.java:512)",
            "org.elasticsearch.search.lookup.SearchLookup.getForField(SearchLookup.java:109)",
            "org.elasticsearch.search.lookup.LeafDocLookup$2.run(LeafDocLookup.java:107)",
            "org.elasticsearch.search.lookup.LeafDocLookup$2.run(LeafDocLookup.java:104)",
            "java.base/java.security.AccessController.doPrivileged(AccessController.java:318)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:104)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:28)",
            "doc['compositeField']",
            " ^---- HERE"
          ],
          "script" : "doc['compositeField']",
          "lang" : "painless",
          "position" : {
            "offset" : 4,
            "start" : 0,
            "end" : 22
          },
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [customer_EPG_id] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
          }
        }

```

Is there no way to read a string field in a script? I don't need to sort by the value I don't need to do aggregation. Just manipulate the value and store as a new field.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [May 13, 2022, 7:37am UTC](https://discuss.elastic.co/t/reindex-text-field-script-manipulation/304376/2 "2022-05-13T07:37:01Z")

</div>

[This](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html#modules-scripting-source) will help you. The way to access fields are bit different between reindexing and runtime field.

---

<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:** [June 10, 2022, 7:37am UTC](https://discuss.elastic.co/t/reindex-text-field-script-manipulation/304376/3 "2022-06-10T07:37:15Z")

</div>

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