Skip to content

Commit 7225b35

Browse files
authored
Simplified Linear & RRF Retrievers - Return error on empty fields param (#129962) (#129970)
1 parent e1894f8 commit 7225b35

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

docs/changelog/129962.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 129962
2+
summary: Simplified Linear & RRF Retrievers - Return error on empty fields param
3+
area: Search
4+
type: bug
5+
issues: []

x-pack/plugin/rank-rrf/src/main/java/org/elasticsearch/xpack/rank/MultiFieldsInnerRetrieverUtils.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ public record WeightedRetrieverSource(CompoundRetrieverBuilder.RetrieverSource r
5656
*/
5757
public static ActionRequestValidationException validateParams(
5858
List<CompoundRetrieverBuilder.RetrieverSource> innerRetrievers,
59-
List<String> fields,
59+
@Nullable List<String> fields,
6060
@Nullable String query,
6161
String retrieverName,
6262
String retrieversParamName,
6363
String fieldsParamName,
6464
String queryParamName,
6565
ActionRequestValidationException validationException
6666
) {
67-
if (fields.isEmpty() == false || query != null) {
67+
if (fields != null || query != null) {
6868
// Using the multi-fields query format
6969
if (query == null) {
7070
// Return early here because the following validation checks assume a query param value is provided
@@ -87,6 +87,13 @@ public static ActionRequestValidationException validateParams(
8787
);
8888
}
8989

90+
if (fields != null && fields.isEmpty()) {
91+
validationException = addValidationError(
92+
String.format(Locale.ROOT, "[%s] [%s] cannot be empty", retrieverName, fieldsParamName),
93+
validationException
94+
);
95+
}
96+
9097
if (innerRetrievers.isEmpty() == false) {
9198
validationException = addValidationError(
9299
String.format(Locale.ROOT, "[%s] cannot combine [%s] and [%s]", retrieverName, retrieversParamName, queryParamName),
@@ -131,13 +138,15 @@ public static ActionRequestValidationException validateParams(
131138
* @return The inner retriever tree as a {@code RetrieverBuilder} list
132139
*/
133140
public static List<RetrieverBuilder> generateInnerRetrievers(
134-
List<String> fieldsAndWeights,
141+
@Nullable List<String> fieldsAndWeights,
135142
String query,
136143
Collection<IndexMetadata> indicesMetadata,
137144
Function<List<WeightedRetrieverSource>, CompoundRetrieverBuilder<?>> innerNormalizerGenerator,
138145
@Nullable Consumer<Float> weightValidator
139146
) {
140-
Map<String, Float> parsedFieldsAndWeights = QueryParserHelper.parseFieldsAndWeights(fieldsAndWeights);
147+
Map<String, Float> parsedFieldsAndWeights = fieldsAndWeights != null
148+
? QueryParserHelper.parseFieldsAndWeights(fieldsAndWeights)
149+
: Map.of();
141150
if (weightValidator != null) {
142151
parsedFieldsAndWeights.values().forEach(weightValidator);
143152
}

x-pack/plugin/rank-rrf/src/main/java/org/elasticsearch/xpack/rank/linear/LinearRetrieverBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public LinearRetrieverBuilder(
163163
throw new IllegalArgumentException("The number of normalizers must match the number of inner retrievers");
164164
}
165165

166-
this.fields = fields == null ? List.of() : List.copyOf(fields);
166+
this.fields = fields == null ? null : List.copyOf(fields);
167167
this.query = query;
168168
this.normalizer = normalizer;
169169
this.weights = weights;
@@ -400,7 +400,7 @@ public void doToXContent(XContentBuilder builder, Params params) throws IOExcept
400400
builder.endArray();
401401
}
402402

403-
if (fields.isEmpty() == false) {
403+
if (fields != null) {
404404
builder.startArray(FIELDS_FIELD.getPreferredName());
405405
for (String field : fields) {
406406
builder.value(field);

x-pack/plugin/rank-rrf/src/main/java/org/elasticsearch/xpack/rank/rrf/RRFRetrieverBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public RRFRetrieverBuilder(
124124
) {
125125
// Use a mutable list for childRetrievers so that we can use addChild
126126
super(childRetrievers == null ? new ArrayList<>() : new ArrayList<>(childRetrievers), rankWindowSize);
127-
this.fields = fields == null ? List.of() : List.copyOf(fields);
127+
this.fields = fields == null ? null : List.copyOf(fields);
128128
this.query = query;
129129
this.rankConstant = rankConstant;
130130
}
@@ -302,7 +302,7 @@ public void doToXContent(XContentBuilder builder, Params params) throws IOExcept
302302
builder.endArray();
303303
}
304304

305-
if (fields.isEmpty() == false) {
305+
if (fields != null) {
306306
builder.startArray(FIELDS_FIELD.getPreferredName());
307307
for (String field : fields) {
308308
builder.value(field);

x-pack/plugin/rank-rrf/src/yamlRestTest/resources/rest-api-spec/test/linear/20_linear_retriever_simplified.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,18 @@ setup:
465465

466466
- contains: { error.root_cause.0.reason: "[linear] [query] cannot be empty" }
467467

468+
- do:
469+
catch: bad_request
470+
search:
471+
index: test-index
472+
body:
473+
retriever:
474+
linear:
475+
fields: [ ]
476+
query: "foo"
477+
478+
- contains: { error.root_cause.0.reason: "[linear] [fields] cannot be empty" }
479+
468480
- do:
469481
catch: bad_request
470482
search:

x-pack/plugin/rank-rrf/src/yamlRestTest/resources/rest-api-spec/test/rrf/310_rrf_retriever_simplified.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,18 @@ setup:
359359

360360
- contains: { error.root_cause.0.reason: "[rrf] [query] cannot be empty" }
361361

362+
- do:
363+
catch: bad_request
364+
search:
365+
index: test-index
366+
body:
367+
retriever:
368+
rrf:
369+
fields: [ ]
370+
query: "foo"
371+
372+
- contains: { error.root_cause.0.reason: "[rrf] [fields] cannot be empty" }
373+
362374
- do:
363375
catch: bad_request
364376
search:

0 commit comments

Comments
 (0)