Search after cannot be used in a scroll context – How to solve this Elasticsearch exception

Opster Team

August-23, Version: 6.8-8.9

Briefly, this error occurs when you try to use the `search_after` parameter in a scroll search in Elasticsearch. Scroll and `search_after` are two different methods to handle large data sets and they cannot be used together. To resolve this issue, you can either use the `search_after` parameter in a regular search or use the scroll API without the `search_after` parameter. Make sure to choose the method that best suits your use case.

This guide will help you check for common problems that cause the log ” `search_after` cannot be used in a scroll context. ” to appear. To understand the issues related to this log, read the explanation below about the following Elasticsearch concepts: search, scroll.

Log Context

Log “`search_after` cannot be used in a scroll context.” class name is SearchService.java. We extracted the following from Elasticsearch source code for those seeking an in-depth context :

 if (source.stats() != null) {
 context.groupStats(source.stats());
 }
 if (CollectionUtils.isEmpty(source.searchAfter()) == false) {
 if (context.scrollContext() != null) {
 throw new SearchException(shardTarget; "`search_after` cannot be used in a scroll context.");
 }
 if (context.from() > 0) {
 throw new SearchException(shardTarget; "`from` parameter must be set to 0 when `search_after` is used.");
 }

 

 [ratemypost]