eze
eze

Reputation: 2422

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

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

Upvotes: 0

Views: 580

Answers (2)

yulinxp
yulinxp

Reputation: 19

Here is what I did. I have only text field for Gender.

"Gender":{
    "type":"text"
}

I want to add a keyword sub field to it. I update mapping as

"Gender": {
  "type": "text",
  "fields": {
    "keyword": {
      "type": "keyword"
    }
  }
}

I use this script to set keyword sub field. Updating the main field automatically updates all its sub-fields.

ctx._source.Gender = ctx._source.Gender.text;

Upvotes: 0

lkatiforis
lkatiforis

Reputation: 6255

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.

Upvotes: 0

Related Questions