Search code examples
javascriptelasticsearchlogstash

Elasticsearch: how can i convert long data-type into keyword or text


i want to convert field 'districId' that has long data-type to keyword/text for wildcard search. please guid me how can convert data-type from long to keyword/text data-type in elasticsearch

PUT geoxingsite/_mapping
{
    "properties": {
      "districtId": {
        "type": "keyword"
      }
    }
}

i am getting error below...

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "mapper [districtId] cannot be changed from type [long] to [keyword]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "mapper [districtId] cannot be changed from type [long] to [keyword]"
  },
  "status" : 400
}

Solution

  • You cannot change the type of a field once it's created. However, you can add a sub-field like this:

    PUT geoxingsite/_mapping
    {
        "properties": {
          "districtId": {
            "type": "long",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
    }
    

    When done you need to update your index in place using

    POST geoxingsite/_update_by_query?wait_for_completion=false
    

    When the task has run, you'll have a new field called districtId.keyword on which you can run your wildcard queries