Skip to content

Commit

Permalink
improve error message related to window size
Browse files Browse the repository at this point in the history
  • Loading branch information
jimczi committed Dec 18, 2024
1 parent 3f421ef commit 6e2a44c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ setup:
---
"Rescorer retriever and invalid window size":
- do:
catch: "/\\[rescorer\\] requires \\[rank_window_size: 5\\] be greater than or equal to \\[size: 10\\]/"
catch: "/\\[rescorer\\] requires \\[window_size: 5\\] be greater than or equal to \\[size: 10\\]/"
search:
index: test
body:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class RescorerBuilder<RB extends RescorerBuilder<RB>>

protected Integer windowSize;

private static final ParseField WINDOW_SIZE_FIELD = new ParseField("window_size");
public static final ParseField WINDOW_SIZE_FIELD = new ParseField("window_size");

/**
* Construct an empty RescoreBuilder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.ShardDocSortField;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.xcontent.ParseField;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -50,6 +51,8 @@ public abstract class CompoundRetrieverBuilder<T extends CompoundRetrieverBuilde

public static final NodeFeature INNER_RETRIEVERS_FILTER_SUPPORT = new NodeFeature("inner_retrievers_filter_support");

public static final ParseField RANK_WINDOW_SIZE_FIELD = new ParseField("rank_window_size");

public record RetrieverSource(RetrieverBuilder retriever, SearchSourceBuilder source) {}

protected final int rankWindowSize;
Expand Down Expand Up @@ -82,6 +85,14 @@ public final boolean isCompound() {
return true;
}

/**
* Retrieves the {@link ParseField} used to configure the {@link CompoundRetrieverBuilder#rankWindowSize}
* at the REST layer.
*/
public ParseField getRankWindowSizeField() {
return RANK_WINDOW_SIZE_FIELD;
}

@Override
public final RetrieverBuilder rewrite(QueryRewriteContext ctx) throws IOException {
if (ctx.getPointInTimeBuilder() == null) {
Expand Down Expand Up @@ -210,14 +221,14 @@ public ActionRequestValidationException validate(
validationException = super.validate(source, validationException, isScroll, allowPartialSearchResults);
if (source.size() > rankWindowSize) {
validationException = addValidationError(
"["
+ this.getName()
+ "] requires [rank_window_size: "
+ rankWindowSize
+ "]"
+ " be greater than or equal to [size: "
+ source.size()
+ "]",
String.format(
Locale.ROOT,
"%s requires [%s: %d] to be greater than or equal to [size: %d]",
getName(),
getRankWindowSizeField().getPreferredName(),
rankWindowSize,
source.size()
),
validationException
);
}
Expand All @@ -236,10 +247,12 @@ public ActionRequestValidationException validate(
if (rankWindowSize > compoundChild.rankWindowSize) {
String errorMessage = String.format(
Locale.ROOT,
"[%s] requires [rank_window_size: %d] to be smaller than or equal to its sub retriever's %s [rank_window_size: %d]",
"[%s] requires [%s: %d] to be smaller than or equal to its sub retriever's %s [%s: %d]",
this.getName(),
getRankWindowSizeField().getPreferredName(),
rankWindowSize,
compoundChild.getName(),
compoundChild.getRankWindowSizeField(),
compoundChild.rankWindowSize
);
validationException = addValidationError(errorMessage, validationException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public String getName() {
return NAME;
}

@Override
public ParseField getRankWindowSizeField() {
return RescorerBuilder.WINDOW_SIZE_FIELD;
}

@Override
protected SearchSourceBuilder finalizeSourceBuilder(SearchSourceBuilder source) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public final class QueryRuleRetrieverBuilder extends CompoundRetrieverBuilder<Qu
public static final ParseField RULESET_IDS_FIELD = new ParseField("ruleset_ids");
public static final ParseField MATCH_CRITERIA_FIELD = new ParseField("match_criteria");
public static final ParseField RETRIEVER_FIELD = new ParseField("retriever");
public static final ParseField RANK_WINDOW_SIZE_FIELD = new ParseField("rank_window_size");

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<QueryRuleRetrieverBuilder, RetrieverParserContext> PARSER = new ConstructingObjectParser<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class TextSimilarityRankRetrieverBuilder extends CompoundRetrieverBuilder
public static final ParseField INFERENCE_ID_FIELD = new ParseField("inference_id");
public static final ParseField INFERENCE_TEXT_FIELD = new ParseField("inference_text");
public static final ParseField FIELD_FIELD = new ParseField("field");
public static final ParseField RANK_WINDOW_SIZE_FIELD = new ParseField("rank_window_size");

public static final ConstructingObjectParser<TextSimilarityRankRetrieverBuilder, RetrieverParserContext> PARSER =
new ConstructingObjectParser<>(TextSimilarityRankBuilder.NAME, args -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public final class RRFRetrieverBuilder extends CompoundRetrieverBuilder<RRFRetri
public static final NodeFeature RRF_RETRIEVER_COMPOSITION_SUPPORTED = new NodeFeature("rrf_retriever_composition_supported");

public static final ParseField RETRIEVERS_FIELD = new ParseField("retrievers");
public static final ParseField RANK_WINDOW_SIZE_FIELD = new ParseField("rank_window_size");
public static final ParseField RANK_CONSTANT_FIELD = new ParseField("rank_constant");

public static final int DEFAULT_RANK_CONSTANT = 60;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ setup:
---
"RRF with rescorer retriever and invalid window size":
- do:
catch: "/\\[rescorer\\] requires \\[rank_window_size: 5\\] be greater than or equal to \\[size: 10\\]/"
catch: "/\\[rescorer\\] requires \\[window_size: 5\\] be greater than or equal to \\[size: 10\\]/"
search:
index: test
body:
Expand Down Expand Up @@ -367,7 +367,7 @@ setup:
size: 10

- do:
catch: "/\\[rescorer\\] requires \\[rank_window_size: 10\\] to be smaller than or equal to its sub retriever's rrf \\[rank_window_size: 5\\]/"
catch: "/\\[rescorer\\] requires \\[window_size: 10\\] to be smaller than or equal to its sub retriever's rrf \\[rank_window_size: 5\\]/"
search:
index: test
body:
Expand Down

0 comments on commit 6e2a44c

Please sign in to comment.