From parameter must be set to 0 when search after is used – How to solve this Elasticsearch exception

Opster Team

August-23, Version: 6.8-8.9

Briefly, this error occurs when the `from` parameter in an Elasticsearch query is not set to 0 while using the `search_after` parameter. The `search_after` parameter is used for pagination of results, and the `from` parameter must be set to 0 to ensure correct pagination. To resolve this issue, you should set the `from` parameter to 0 in your query when using `search_after`. Alternatively, if you need to use the `from` parameter with a value other than 0, you should not use `search_after` in the same query.

This guide will help you check for common problems that cause the log ” `from` parameter must be set to 0 when `search_after` is used. ” to appear. To understand the issues related to this log, read the explanation below about the following Elasticsearch concepts: search.

Log Context

Log “`from` parameter must be set to 0 when `search_after` is used.” class name is SearchService.java. We extracted the following from Elasticsearch source code for those seeking an in-depth context :

 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.");
 }  String collapseField = source.collapse() != null ? source.collapse().getField() : null;
 FieldDoc fieldDoc = SearchAfterBuilder.buildFieldDoc(context.sort(); source.searchAfter(); collapseField);
 context.searchAfter(fieldDoc);

 

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?