# Elasticsearch SearchContextMissingException during 'scan & scroll' query with Spring Data Elasticsearch

**URL:** https://discuss.elastic.co/t/elasticsearch-searchcontextmissingexception-during-scan-scroll-query-with-spring-data-elasticsearch/54402
**Category:** Elasticsearch
**Created:** [June 30, 2016, 12:05pm UTC](https://discuss.elastic.co/t/elasticsearch-searchcontextmissingexception-during-scan-scroll-query-with-spring-data-elasticsearch/54402 "2016-06-30T12:05:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![esnosek](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@esnosek](https://discuss.elastic.co/u/esnosek)
#### Post date: [June 30, 2016, 12:05pm UTC](https://discuss.elastic.co/t/elasticsearch-searchcontextmissingexception-during-scan-scroll-query-with-spring-data-elasticsearch/54402/1 "2016-06-30T12:05:57Z")

</div>

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:

```
[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)

```

My 'scan & scroll' code:

```
public List<T> getAllElements(SearchQuery searchQuery) {
    searchQuery.setPageable(new PageRequest(0, PAGE_SIZE));
    String scrollId = elasticsearchTemplate.scan(searchQuery, 1000, false);
    List<T> allElements = new LinkedList<>();
    boolean hasRecords = true;
    while (hasRecords) {
        Page<T> page = elasticsearchTemplate.scroll(scrollId, 5000, resultMapper);
        if (page.hasContent()) {
            allElements.addAll(page.getContent());
        } else {
            hasRecords = false;
        }
    }
    elasticsearchTemplate.clearScroll(scrollId);
    return allElements;
}

```

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:

`Page<T> page = elasticsearchTemplate.scroll(scrollId, 5000, resultMapper);`

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.

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?

Thank you for help,

Simon.

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [June 30, 2016, 12:09pm UTC](https://discuss.elastic.co/t/elasticsearch-searchcontextmissingexception-during-scan-scroll-query-with-spring-data-elasticsearch/54402/2 "2016-06-30T12:09:36Z")

</div>

This usually happens if your search context is not alive anymore.

In your case, you're starting your scan with a timeout of 1 second and then during each scan the search context is kept alive during 5 seconds. It's probably too low. The [default duration](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html#scroll-search-context) to keep the search context alive is 1 minute, so you should probably increase it to 60 seconds like this:

```
String scrollId = elasticsearchTemplate.scan(searchQuery, 60000, false);
...
Page<T> page = elasticsearchTemplate.scroll(scrollId, 60000, resultMapper);
```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 10:39pm UTC](https://discuss.elastic.co/t/elasticsearch-searchcontextmissingexception-during-scan-scroll-query-with-spring-data-elasticsearch/54402/3 "2017-07-05T22:39:13Z")

</div>


