Skip to content

Commit 2eeeb41

Browse files
committed
Modified code
1 parent 0d93fda commit 2eeeb41

File tree

3 files changed

+30
-154
lines changed

3 files changed

+30
-154
lines changed

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

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.search.retriever.RetrieverParserContext;
2626
import org.elasticsearch.search.retriever.StandardRetrieverBuilder;
2727
import org.elasticsearch.xcontent.ConstructingObjectParser;
28-
import org.elasticsearch.xcontent.ObjectParser;
2928
import org.elasticsearch.xcontent.ParseField;
3029
import org.elasticsearch.xcontent.XContentBuilder;
3130
import org.elasticsearch.xcontent.XContentParser;
@@ -89,35 +88,7 @@ public final class RRFRetrieverBuilder extends CompoundRetrieverBuilder<RRFRetri
8988

9089
static {
9190
PARSER.declareObjectArray(ConstructingObjectParser.optionalConstructorArg(), RRFRetrieverComponent::fromXContent, RETRIEVERS_FIELD);
92-
PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> {
93-
List<String> fields = new ArrayList<>();
94-
XContentParser.Token token = p.currentToken();
95-
if (token == XContentParser.Token.START_ARRAY) {
96-
while ((token = p.nextToken()) != XContentParser.Token.END_ARRAY) {
97-
if (token == XContentParser.Token.VALUE_STRING) {
98-
fields.add(p.text());
99-
} else if (token == XContentParser.Token.START_OBJECT) {
100-
String fieldName = null;
101-
float weight = 1.0f;
102-
while (p.nextToken() != XContentParser.Token.END_OBJECT) {
103-
if (p.currentToken() == XContentParser.Token.FIELD_NAME) {
104-
String name = p.currentName();
105-
p.nextToken();
106-
if ("field".equals(name)) {
107-
fieldName = p.text();
108-
} else if ("weight".equals(name)) {
109-
weight = p.floatValue();
110-
}
111-
}
112-
}
113-
if (fieldName != null) {
114-
fields.add(weight == 1.0f ? fieldName : fieldName + "^" + weight);
115-
}
116-
}
117-
}
118-
}
119-
return fields;
120-
}, FIELDS_FIELD, ObjectParser.ValueType.VALUE_ARRAY);
91+
PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), FIELDS_FIELD);
12192
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), QUERY_FIELD);
12293
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), RANK_WINDOW_SIZE_FIELD);
12394
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), RANK_CONSTANT_FIELD);

x-pack/plugin/rank-rrf/src/test/java/org/elasticsearch/xpack/rank/rrf/RRFRetrieverBuilderParsingTests.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -432,46 +432,37 @@ public void testSimplifiedWeightedRRFBasicParsing() throws IOException {
432432
}
433433

434434
public void testSimplifiedFieldSyntaxVariations() throws IOException {
435-
// Test parsing succeeds with various field syntax variations (validation happens during rewrite, not parsing)
435+
// Test parsing succeeds with various field^weight syntax variations (validation happens during rewrite, not parsing)
436436
SearchUsageHolder searchUsageHolder = new UsageService().getSearchUsageHolder();
437437

438-
// Test 1: Pure object-based syntax
438+
// Test all field syntax variations
439439
String restContent1 = """
440440
{
441441
"retriever": {
442442
"rrf": {
443-
"fields": [
444-
{"field": "name", "weight": 2.0},
445-
{"field": "description", "weight": 1.0}
446-
],
443+
"fields": ["name^2", "description^1"],
447444
"query": "test"
448445
}
449446
}
450447
}
451448
""";
452449

453-
// Test 2: Mixed syntax (object-based + plain strings)
454450
String restContent2 = """
455451
{
456452
"retriever": {
457453
"rrf": {
458-
"fields": [
459-
{"field": "name", "weight": 3.0},
460-
"description",
461-
{"field": "category", "weight": 0.5}
462-
],
454+
"fields": ["name^3", "description", "category^0.5"],
463455
"query": "test"
464456
}
465457
}
466458
}
467459
""";
468460

469-
// Test 3: Field^weight syntax
470461
String restContent3 = """
471462
{
472463
"retriever": {
473464
"rrf": {
474-
"fields": ["name^2", "description^0.5"],
465+
"fields": ["name", "description"],
475466
"query": "test"
476467
}
477468
}

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

Lines changed: 24 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -216,33 +216,39 @@ setup:
216216
- lt: { hits.hits.2._score: 1.0 }
217217

218218
---
219-
"Zero weight handling":
219+
"Negative weight validation":
220+
- requires:
221+
cluster_features: ["rrf_retriever.weighted_support"]
222+
reason: "Weighted fields syntax support"
223+
220224
- do:
225+
catch: bad_request
221226
search:
222227
index: test-index
223228
body:
224229
retriever:
225230
rrf:
226-
fields: [ "text_1^0", "text_2^1" ]
231+
fields: ["text_1^-1"]
227232
query: "foo"
228233

229-
# Zero weight on text_1 means only text_2 matches count
230-
- gte: { hits.total.value: 1 }
231-
- length: { hits.hits: 1 }
234+
- match: { error.root_cause.0.reason: "[rrf] per-field weights must be non-negative" }
232235

233236
---
234-
"Negative weight validation":
237+
"Zero weight handling":
238+
- requires:
239+
cluster_features: ["rrf_retriever.weighted_support"]
240+
reason: "Weighted fields syntax support"
241+
235242
- do:
236-
catch: bad_request
237243
search:
238244
index: test-index
239245
body:
240246
retriever:
241247
rrf:
242-
fields: [ "text_1", "text_2^-1" ]
248+
fields: ["text_1^0", "text_2^1"]
243249
query: "foo"
244250

245-
- match: { error.root_cause.0.reason: "[rrf] per-field weights must be non-negative" }
251+
- gte: { hits.total.value: 1 }
246252

247253
---
248254
"Basic per-field boosting using the simplified format":
@@ -576,128 +582,36 @@ setup:
576582
- gte: { hits.total.value: 1 }
577583

578584
---
579-
"Object-based weighted fields syntax":
580-
- requires:
581-
cluster_features: ["rrf_retriever.weighted_support"]
582-
reason: "Object-based weighted fields syntax support"
583-
584-
- do:
585-
search:
586-
index: test-index
587-
body:
588-
retriever:
589-
rrf:
590-
fields:
591-
- field: text_1
592-
weight: 2.0
593-
- field: text_2
594-
weight: 1.0
595-
query: "foo z"
596-
597-
- match: { hits.total.value: 2 }
598-
- length: { hits.hits: 2 }
599-
600-
---
601-
"Mixed object and string field syntax":
602-
- requires:
603-
cluster_features: ["rrf_retriever.weighted_support"]
604-
reason: "Object-based weighted fields syntax support"
605-
606-
- do:
607-
search:
608-
index: test-index
609-
body:
610-
retriever:
611-
rrf:
612-
fields:
613-
- field: text_1
614-
weight: 3.0
615-
- text_2
616-
query: "foo z"
617-
618-
- match: { hits.total.value: 2 }
619-
- length: { hits.hits: 2 }
620-
621-
---
622-
"Object-based negative weight validation":
623-
- requires:
624-
cluster_features: ["rrf_retriever.weighted_support"]
625-
reason: "Object-based weighted fields syntax support"
626-
627-
- do:
628-
catch: bad_request
629-
search:
630-
index: test-index
631-
body:
632-
retriever:
633-
rrf:
634-
fields:
635-
- field: text_1
636-
weight: -1.0
637-
query: "foo"
638-
639-
- match: { error.root_cause.0.reason: "[rrf] per-field weights must be non-negative" }
640-
641-
---
642-
"Object-based zero weight handling":
585+
"Semantic field weighting":
643586
- requires:
644587
cluster_features: ["rrf_retriever.weighted_support"]
645-
reason: "Object-based weighted fields syntax support"
588+
reason: "Weighted fields syntax support"
646589

647590
- do:
648591
search:
649592
index: test-index
650593
body:
651594
retriever:
652595
rrf:
653-
fields:
654-
- field: text_1
655-
weight: 0.0
656-
- field: text_2
657-
weight: 1.0
658-
query: "foo"
596+
fields: ["dense_inference^2", "sparse_inference^1.5"]
597+
query: "elasticsearch"
659598

660-
- gte: { hits.total.value: 1 }
599+
- match: { hits.total.value: 3 }
600+
- length: { hits.hits: 3 }
661601

662602
---
663-
"Object-based large weight values":
603+
"Large weight values":
664604
- requires:
665605
cluster_features: ["rrf_retriever.weighted_support"]
666-
reason: "Object-based weighted fields syntax support"
606+
reason: "Weighted fields syntax support"
667607

668608
- do:
669609
search:
670610
index: test-index
671611
body:
672612
retriever:
673613
rrf:
674-
fields:
675-
- field: text_1
676-
weight: 1000000.0
677-
- field: text_2
678-
weight: 1.0
614+
fields: ["text_1^1000000", "text_2^1"]
679615
query: "foo"
680616

681617
- gte: { hits.total.value: 1 }
682-
683-
---
684-
"Object-based semantic field weighting":
685-
- requires:
686-
cluster_features: ["rrf_retriever.weighted_support"]
687-
reason: "Object-based weighted fields syntax support"
688-
689-
- do:
690-
search:
691-
index: test-index
692-
body:
693-
retriever:
694-
rrf:
695-
fields:
696-
- field: dense_inference
697-
weight: 2.0
698-
- field: sparse_inference
699-
weight: 1.5
700-
query: "elasticsearch"
701-
702-
- match: { hits.total.value: 3 }
703-
- length: { hits.hits: 3 }

0 commit comments

Comments
 (0)