title: Elasticsearch: Waiting for Long Running requests to complete
body: <p>What is the best approach for knowing when a <strong>long running</strong> Elasticsearch request is complete?</p>
  
  <p>Today I have a process that periodically purges ~100K documents from an AWS hosted ES that contains a total of ~60M documents.</p>
  
  <pre><code>var settings = new ConnectionSettings(new Uri("https://mycompany.es.aws.com"));
  settings.RequestTimeout(TimeSpan.FromMinutes(3)); // not sure this helps
  
  var client = new ElasticClient(settings);
  
  var request = new DeleteByQueryRequest("MyIndex") { ... };
  
  // this call will return an IsValid = true, httpstatus = 504 after ~60s,
  var response = await client.DeleteByQueryAsync(request);
  </code></pre>
  
  <p>Even with timeout set to 3 minutes, the call always returns in ~60s with an empty response and a 504 status code.  Though through Kibana, I can see that the delete action continues (and properly completes) over the next several minutes.</p>
  
  <p>Is there a better way to request and monitor (wait for completion) a long running ES request?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Based on Simon Lang's response I updated my code to make use of ES Tasks.  The final solution looks something like this...</p>
  
  <pre><code>var settings = new ConnectionSettings(new Uri("https://mycompany.es.aws.com"));
  settings.RequestTimeout(TimeSpan.FromMinutes(3)); // not sure this helps
  
  var client = new ElasticClient(settings);
  
  var request = new DeleteByQueryRequest("MyIndex") 
  {
    Query = ...,
    WaitForCompletion = false
  };
  
  var response = await client.DeleteByQueryAsync(request);
  
  if (response.IsValid)
  {
    var taskCompleted = false;
    while (!taskCompleted)
    {
      var taskResponse = await client.GetTaskAsync(response.Task);
      taskCompleted = taskResponse.Completed;
  
      if (!taskCompleted)
      {
        await Task.Delay(5000);
      }
    }
  }
  </code></pre>