During re-index, add a keyword field using painless?

530 views Asked by At

My painless script for a re-index is adding a field. How can I index that new field as a keyword vice text field using painless?

   POST _reindex
   {
     8<
    "script" : {
       "source" : "ctx._source.newfield = 'test'"
       "lang" : "painless"
    }

I want newfield to be newfield.keyword

1

There are 1 answers

2
lkatiforis On

By default, Elasticsearch will map it to a text field, with a keyword sub-field, unless you have specified explicit mapping. Your mapping would look like this:

 "mappings": {
        "properties": {
            "newfield": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    }

As a consequence, it will both be possible to perform full-text search on newfield, and keyword search and aggregations using the newfield.keyword field.

If you want it to be mapped as a keyword you will have to specify your own explicit mappings.