diff --git a/src/main/java/org/opensearch/neuralsearch/query/HybridQueryBuilder.java b/src/main/java/org/opensearch/neuralsearch/query/HybridQueryBuilder.java index 0b52b90e6..c6123c8bf 100644 --- a/src/main/java/org/opensearch/neuralsearch/query/HybridQueryBuilder.java +++ b/src/main/java/org/opensearch/neuralsearch/query/HybridQueryBuilder.java @@ -59,7 +59,6 @@ public final class HybridQueryBuilder extends AbstractQueryBuilder toQueries(Collection queryBuilders, Quer return queries; } - private static void validatePaginationDepth(Integer paginationDepth) { - if (paginationDepth != null - && (paginationDepth < LOWER_BOUND_OF_PAGINATION_DEPTH || paginationDepth > UPPER_BOUND_OF_PAGINATION_DEPTH)) { + private static void validatePaginationDepth(Integer paginationDepth, QueryShardContext queryShardContext) { + if (Objects.isNull(paginationDepth)) { + return; + } + // compare pagination depth with OpenSearch setting index.max_result_window + // see https://opensearch.org/docs/latest/install-and-configure/configuring-opensearch/index-settings/ + int maxResultWindowIndexSetting = queryShardContext.getIndexSettings().getMaxResultWindow(); + if (paginationDepth < LOWER_BOUND_OF_PAGINATION_DEPTH || paginationDepth > maxResultWindowIndexSetting) { throw new IllegalArgumentException( String.format(Locale.ROOT, "Pagination depth should lie in the range of 1-1000. Received: %s", paginationDepth) );