Elasticsearch 1.x add field copy of timestamp

2019-03-01 14:16发布

问题:

I am working in ES 1.5.2. I have an index with documents, with stored timestamp values. I want to add a regular field to it, which will assume the value of the _timestamp field for that document. How can I do this? I could do

PUT twitter/_mapping/new_timestamp
{
  "properties": {
    "name": {
      "type": "float"
    }
  }
}

to create a regular field, but how can I copy over all the _timestamp values to it?

回答1:

In ES 1.5.2, you can use the update by query plugin in order to reindex your documents and copy the _timestamp field to a regular field.

After installing the plugin with the following command:

bin/plugin -url http://oss.sonatype.org/content/repositories/releases/com/yakaz/elasticsearch/plugins/elasticsearch-action-updatebyquery/1.0.0/elasticsearch-action-updatebyquery-1.0.0.zip install elasticsearch-action-updatebyquery

And making sure that dynamic scripting is enabled in your elasticsearch.yml configuration file, you'll be able to run the following command

POST /twitter/_update_by_query
{
  "script": {
    "inline": "ctx._source.new_timestamp = ctx._timestamp”
  },
  "query": {
    "match_all": {}
  }
}