Recipe: Reindexing Elasticsearch Documents with Logstash

If you’re working with Elasticsearch, it’s very likely that you’ll need to reindex data at some point. The most popular reason is because you need a mapping change that is incompatible with your current mapping. New fields can be added by default, but many changes are not allowed, for example:

  • Want to switch to doc values because field data is taking too much heap? Reindex!
  • Want to change the analyzer of a given field? Reindex!
  • Want to break one great big index into time-based indices? Reindex!

Enter Logstash

A while ago I was using stream2es for reindexing, but if you look at the GitHub page it recommends using Logstash instead. Why? In general, Logstash can do more stuff, here are my top three reasons:

  1. On the input side, you can filter only a subset of documents to reindex
  2. You can add filters to transform documents on their way to the new index (or indices)
  3. It should perform better, as you can add more filter threads (using the -w parameter) and multiple output worker threads (using the workers configuration option)

Show Me the Configuration!

In short, you’ll use the elasticsearch input to read existing data and the elasticsearch output to write it. In between, you can use various filters to change how documents look like.

Input

To read documents, you’ll use the elasticsearch input. You’ll probably want to specify the host(s) to connect to and the index (check the documentation for more options like query):

input {
  elasticsearch {
   hosts => ["localhost"]
   index => "old-index"
  }
}

By default, this will run a match_all query that does a scan through all the documents of the index, fetch pages of 1000, and times out in a minute (i.e. after a minute it won’t know where it left off). All this is configurable, but the defaults are sensible. Scan is good for deep paging (as normally when you fetch a page from 1000000 to 1000020, Elasticsearch fetches 1000020, sorts them, and gives back the last 20) and also works with a “snapshot” of the index (updates after the scan started won’t be taken into account).

Filter

Next, you might want to change documents in their way to the new index. For example, if the data you’re reindexing wasn’t originally indexed with Logstash, you probably want to remove the @version and/or @timestamp fields that are automatically added. To do that, you’ll use the mutate filter:

filter {
 mutate {
  remove_field => [ "@version" ]
 }
}

Output

Finally, you’ll use the elasticsearch output to send data to a new index. The defaults are once again geared towards the logging use-case. If this is not your setup, you might want to disable the default Logstash template (manage_template=false) and use yours:

output {
 elasticsearch {
   host => "localhost"
   protocol => "http"
   manage_template => false
   index => "new-index"
   index_type => "new-type"
   workers => 5
 }
}

Final Remarks

If you want to use time-based indices, you can change index to something like “logstash-%{+YYYY.MM.dd}” (this is the default), and the date would be taken from the @timestamp field. This is by default populated with the time Logstash processes the document, but you can use the date filter to replace it with a timestamp from the document itself:

filter {
 date {
   "match" => [ "custom_timestamp", "MM/dd/YYYY HH:mm:ss" ]
   target => "@timestamp"
 }
}

If your Logstash configuration contains only these snippets, it will nicely shut down when it’s done reindexing.

That’s it! We are happy answer questions or receive feedback – please drop us a line or get us @sematext. And, yes, we’re hiring!

15 thoughts on “Recipe: Reindexing Elasticsearch Documents with Logstash

  1. Somebody help with this error:

    {:timestamp=>”2016-09-15T09:33:12.980000+0100″, :message=>”Failed parsing date from field”, :field=>”timestamp”, :value=>”2016-09-15 08:33:05.813000″, :exception=>”Invalid format: \”2016-09-15 08:33:05.813000\””, :config_parsers=>”ISO8601,yyyy-MM-dd’T’HH:mm:ss.SSSSSSZZ,yyyy-MM-dd HH:mm:ss,SSSSSS,MMM dd YYYY HH:mm:ss”, :config_locale=>”default=en_US”, :level=>:warn}

    I noticed that when i changed the comma “yyyy-MM-dd HH:mm:ss,SSSSSS” to dot “yyyy-MM-dd HH:mm:ss.SSSSSS”. Logstash started and does not returned the error but kibana stopped visualizing. Once i returned the comma kibana started working again and logstash started giving the error again.

    my filter logstasg config”

    grok {
    add_tag => [ “valid” ]
    match => { “message” => “%{TIMESTAMP_ISO8601:log_timestamp} %{DATA} Processed (?:inbound|outbound) message for ([^\s]+): %{GREEDYDATA:json_data}” }
    }

    json {
    source => json_data
    }

    date {
    match => [ “timestamp”,”ISO8601″,”yyyy-MM-dd’T’HH:mm:ss.SSSZZ”,”yyyy-MM-dd HH:mm:ss,SSS”,”MMM dd YYYY HH:mm:ss” ]
    remove_field => [“timestamp”]
    target => “@timestamp”

    Could this issue attach to elasticsearch mapping?
    Elasticsearch mapping:
    “@timestamp”: {
    “format”: “yyyy-MM-dd’T’HH:mm:ss.SSSZ”,
    “index”: “not_analyzed”,
    “type”: “date”
    },
    Thanks

    1. I think there are two problems here: one is date parsing withing Logstash – if your field contains a dot and your date filter doesn’t (because it has a comma), it throws an error.

      The other is when your Logstash filter contains a dot (so it parses correctly – no error) but you don’t see the logs in Elasticsearch/Kibana. I believe that would be a parsing error on the Elasticsearch side, because Logstash likely produces microsecond-level timestamps (like your original log) and Elasticsearch is configured to only accept millisecond-level ones.

      Elasticsearch logs can confirm/deny my hypothesis, but either way, I think if you remove the “format” line from the mapping then Elasticsearch will accept whatever Logstash throws at it by default.

      1. Hello RADU,

        Thanks for response. I have removed format from elasticsearch mapping but still at same issue.
        What else can i do?

        Thanks

        1. Hello Temitope,

          Anything interesting in the Elasticsearch logs? I think they will give us a better indication on what’s going on than trial and error.

          1. Hi Radu,

            Unfortunately there is no error coming from elasticsearch.
            Except you have what i should look for in elasticsearch.

            Thanks

        2. Hi Temitope,

          How about adding a stdout output next to your Elasticsearch one. You can use the JSON codec to see what it sends to Elasticsearch. If there’s no error, you can try indexing one of those documents manually and see what you get.

          1. Hello Radio,

            I really appreciate your supports so far. I got it fixed by adding Get as timezone and adding the milliseconds.

            Thanks

    1. With Logstash you can’t segment the reading part yet like your reindexer does (though that will likely become possible with 5.x and Sliced Scroll), though this is rarely a bottleneck. If it is, I guess the not-so-nice workaround is to start multiple inputs, one for each slice. Either way, you can tune the number of pipeline threads and output threads, to make use of more CPU while transforming and sending data.

Leave a Reply