leeway
leeway

Reputation: 11

reindex while converting a string value of a specific field (present in old index) into a number field value (in the new index)

Could I ask, how could I reindex while converting a 'string' field e.g. "field2": "123.2" (in old index documents) into a float/double number e.g. "field2": 123.2 (intended to be in the new index) ? This post is the closest I could get, but I do not know which function to use for the cast/conversion of a string to a number. I am using ElasticSearch version 2.3.3. Thank you very much for any advice !!!

Upvotes: 1

Views: 3213

Answers (2)

troymass
troymass

Reputation: 1062

Use Elasticsearch templates to specify the mapping for the new index and specify the field as a double type.

The easiest way to build a template is to use the existing mapping.

GET oldindex/_mapping
POST _template/templatename
{
  "template" : "newindex", // this can be a wildcard pattern to match indexes
  "mappings": { // this is copied from the response of the previous call
    "mytype": {
      "properties": {
        "field2": {
          "type": "double" // change the type
        }
      }
    }
  }
}
POST newindex
GET newindex/_mapping

Then use the elasticsearch _reindex API to move the data from the old index to the new index and parse the field as a double using an inline scripting (you may need to enable inline scripting)

POST _reindex
{
  "source": {
    "index": "oldindex"
  },
  "dest": {
    "index": "newindex"
  },
  "script": {
    "inline": "ctx._source.field2 = ctx._source.field2.toDouble()"
  }
}

Edit: Updated to use _reindex endpoint

Upvotes: 0

Christian Häckh
Christian Häckh

Reputation: 522

You could use Logstash to reindex your data and convert the field. Something like the following:

input {
  elasticsearch {
    hosts => "es.server.url"
    index => "old_index"
    query => "*"
    size => 500
    scroll => "5m"
    docinfo => true
  }
}

filter {
  mutate {
    convert => { "fieldname" => "long" }
  }
}

output {
  elasticsearch {
    host => "es.server.url"
    index => "new_index"
    index_type => "%{[@metadata][_type]}"
    document_id => "%{[@metadata][_id]}"
  }
}

Upvotes: 0

Related Questions