Skip to content

Commit

Permalink
Use max_window_size as max for pagination depth
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Gaievski <[email protected]>
  • Loading branch information
martin-gaievski committed Dec 19, 2024
1 parent dc9020c commit c3cacd1
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public final class HybridQueryBuilder extends AbstractQueryBuilder<HybridQueryBu
static final int MAX_NUMBER_OF_SUB_QUERIES = 5;
private final static int DEFAULT_PAGINATION_DEPTH = 10;
private static final int LOWER_BOUND_OF_PAGINATION_DEPTH = 1;
private static final int UPPER_BOUND_OF_PAGINATION_DEPTH = 10000;

public HybridQueryBuilder(StreamInput in) throws IOException {
super(in);
Expand Down Expand Up @@ -128,6 +127,7 @@ protected Query doToQuery(QueryShardContext queryShardContext) throws IOExceptio
if (queryCollection.isEmpty()) {
return Queries.newMatchNoDocsQuery(String.format(Locale.ROOT, "no clauses for %s query", NAME));
}
validatePaginationDepth(paginationDepth, queryShardContext);
return new HybridQuery(queryCollection, paginationDepth);
}

Expand Down Expand Up @@ -236,7 +236,6 @@ public static HybridQueryBuilder fromXContent(XContentParser parser) throws IOEx
compoundQueryBuilder.queryName(queryName);
compoundQueryBuilder.boost(boost);
if (isClusterOnOrAfterMinReqVersionForPaginationInHybridQuery()) {
validatePaginationDepth(paginationDepth);
compoundQueryBuilder.paginationDepth(paginationDepth);
}
for (QueryBuilder query : queries) {
Expand Down Expand Up @@ -326,9 +325,14 @@ private Collection<Query> toQueries(Collection<QueryBuilder> 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)
);
Expand Down

0 comments on commit c3cacd1

Please sign in to comment.