title: Elasticsearch SearchContextMissingException during 'scan & scroll' query with Spring Data Elasticsearch
body: <p>I am using elasticsearch 2.2.0 with default cluster configuration. I encounter a problem with scan and scroll query using spring data elasticsearch. When I execute the query I get error like this:</p>
  
  <pre><code>[2016-06-29 12:45:52,046][DEBUG][action.search.type       ] [Vector] [155597] Failed to execute query phase
  RemoteTransportException[[Vector][10.132.47.95:9300][indices:data/read/search[phase/scan/scroll]]]; nested: SearchContextMissingException[No search context found for id [155597]];
  Caused by: SearchContextMissingException[No search context found for id [155597]]
      at org.elasticsearch.search.SearchService.findContext(SearchService.java:611)
      at org.elasticsearch.search.SearchService.executeScan(SearchService.java:311)
      at org.elasticsearch.search.action.SearchServiceTransportAction$SearchScanScrollTransportHandler.messageReceived(SearchServiceTransportAction.java:433)
      at org.elasticsearch.search.action.SearchServiceTransportAction$SearchScanScrollTransportHandler.messageReceived(SearchServiceTransportAction.java:430)
      at org.elasticsearch.transport.TransportService$4.doRun(TransportService.java:350)
      at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>My 'scan &amp; scroll' code:</p>
  
  <pre><code>public List&lt;T&gt; getAllElements(SearchQuery searchQuery) {
      searchQuery.setPageable(new PageRequest(0, PAGE_SIZE));
      String scrollId = elasticsearchTemplate.scan(searchQuery, 1000, false);
      List&lt;T&gt; allElements = new LinkedList&lt;&gt;();
      boolean hasRecords = true;
      while (hasRecords) {
          Page&lt;T&gt; page = elasticsearchTemplate.scroll(scrollId, 5000, resultMapper);
          if (page.hasContent()) {
              allElements.addAll(page.getContent());
          } else {
              hasRecords = false;
          }
      }
      elasticsearchTemplate.clearScroll(scrollId);
      return allElements;
  }
  </code></pre>
  
  <p>When my query result size is less than PAGE_SIZE parameter, then error like this occurs five times. I guess that it is one per shard. When result size is bigger than PAGE_SIZE, the error occurs few times more. I've tried to refactor my code to not call:</p>
  
  <pre><code>Page&lt;T&gt; page = elasticsearchTemplate.scroll(scrollId, 5000, resultMapper);
  </code></pre>
  
  <p>when I'm sure that the page has no content. But it works only if PAGE_SIZE is bigger than query result, so it is no the solution at all.</p>
  
  <p>I have to add that it is problem only on elasticsearch side. On the client side the errors is hidden and in each case the query result is correct. Has anybody knows what causes this issue?</p>
  
  <p>Thank you for help,</p>
  
  <p>Simon.</p>