3

I'm using Elasticsearch's reindex API to migrate logs from an old cluster to a new version 7.9.2 cluster. Here is the command I'm using.

curl -X POST "new_host:9200/_reindex?pretty&refresh&wait_for_completion=true" -H 'Content-Type: application/json' -d'
{
  "source": {
    "remote": {
      "host": "old_host:9200"
    },
    "index": "*",
    "size": 10000,    
    "query": {
      "match_all": {}      
     }
  },
  "conflicts": "proceed",
  "dest": {
    "index": "logstash"
  }
}'

This gets only the last 10000 documents or 1 batch and request gets completed after that. However, I need to reindex more than a million documents. Is there a way to make the request run for all the matched documents? Can we set the number of batches in the request or make the request issue batches till all documents are indexed?

One option I can think of is to send request recursively by modifying query on datetime. Is there a better way to do it? Can I get all the matched documents (1 million plus) in one request?

Shiv Rajawat
  • 898
  • 9
  • 21

1 Answers1

2

Remove the query and size params in order to get all the data. If you need to filter only desired documents using a query, just remove the size to fetch all matched logs.

Using wait_for_completion=false as query param will return the task id and you will be able to monitor the reindex progress using GET /_tasks/<task_id>.

If you need or want to break the reindexing into serveral steps/chunks consider using the slice feature.

BTW: Reindex one index after another instead all at one using * and consider using daily/monthly indicies as it becomes easier to resume the process on errors and manage the log retention in comparison to one whole index.

In order to improve the speed, you should reduce the replicas to 0 and set refresh_interval=-1 in the destination index bevore reindexing and reset the values afterwards.

curl -X POST "new_host:9200/_reindex?pretty&wait_for_completion=false" -H 'Content-Type: application/json' -d'
{
  "source": {
    "remote": {
      "host": "old_host:9200"
    },
    "index": "index_name"
  },
  "conflicts": "proceed",
  "dest": {
    "index": "logstash"
  }
}'

UPDATE based on comments:

While reindexing, there is at least one error what causes the reindexing to stop. The error is being caused by at least one document (id=xiB9...) having 'OK' as value in field 'fields.StatusCode'. But the mapping in the destination index has long as data type what is causing the mentioned exception.

The solution is to change the source documents StatusCode to 200 for example, but there will be probably more documents causing the very same error.

Another solution is to change the mapping in the destination index to keyword type - that requires a handmade mapping set before any data has been inserted and maybe reindexing the already present data.

ibexit
  • 3,465
  • 1
  • 11
  • 25
  • Removing query and size doesn't seem to work. I think somehow it is hitting the limit after updating 1000 documents. For 14 documents I received exceptions. The task status is changing to completed once it hits 1000 records.{"total":1134776,"updated":986,"created":0,"deleted":0,"batches":1,........} – Shiv Rajawat Sep 28 '20 at 02:09
  • So, the request is getting completed after just 1 batch. Is there a way to set the number of batches? – Shiv Rajawat Sep 28 '20 at 02:41
  • It seems that the problem is in the destination cluster. Is there any error? What is the cluster state? – ibexit Sep 28 '20 at 05:33
  • The destination cluster is healthy. And yes there are exceptions in the task status - "type":"illegal_argument_exception". This exception is coming for some documents. Is it possible that the API doesn't send next batch if there are exceptions in previous batch? As far as I can tell, in my case the API is just sending 1 batch. – Shiv Rajawat Sep 28 '20 at 07:18
  • 1
    Yes, reindexing stops on errors. See the task for more details. What is the error message stating in detail? – ibexit Sep 28 '20 at 13:01
  • Yes, it is because of errors. Whenever the error is coming in a batch, the request is not issuing next batches and the task is getting completed. Pasting the error below, please help if you can. – Shiv Rajawat Sep 28 '20 at 16:13
  • {"took":2159,"timed_out":false,"total":1356017,"updated":749,"created":0,"deleted":0,"batches":75,"version_conflicts":0,"noops":0,"retries":{"bulk":0,"search":0},"throttled_millis":0,"requests_per_second":-1.0,"throttled_until_millis":0,"failures":[{"index":"logstash","type":"_doc","id":"xiB9u3QB0iD7wLmy4C0O","cause":{"type":"mapper_parsing_exception","reason":"failed to parse field [fields.StatusCode] of type [long] in document with id 'xiB9u3QB0iD7wLmy4C0O'. Preview of field's value: 'OK'","caused_by":{"type":"illegal_argument_exception","reason":"For input string: \"OK\""}},"status":400}]} – Shiv Rajawat Sep 28 '20 at 16:15