Skip to content

Commit 1f35595

Browse files
ioanatiapmpailis
andauthored
Update test_reranking_service to try and parse provided inputs as scores (elastic#122328) (elastic#129731)
Co-authored-by: Panagiotis Bailis <[email protected]>
1 parent 32c19a0 commit 1f35595

File tree

4 files changed

+156
-55
lines changed

4 files changed

+156
-55
lines changed

x-pack/plugin/inference/qa/test-service-plugin/src/main/java/org/elasticsearch/xpack/inference/mock/TestRerankingServiceExtension.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import java.io.IOException;
3838
import java.util.ArrayList;
39+
import java.util.Comparator;
3940
import java.util.EnumSet;
4041
import java.util.HashMap;
4142
import java.util.List;
@@ -148,16 +149,28 @@ public void chunkedInfer(
148149
}
149150

150151
private RankedDocsResults makeResults(List<String> input) {
151-
List<RankedDocsResults.RankedDoc> results = new ArrayList<>();
152152
int totalResults = input.size();
153-
float minScore = random.nextFloat(-1f, 1f);
154-
float resultDiff = 0.2f;
155-
for (int i = 0; i < input.size(); i++) {
156-
results.add(
157-
new RankedDocsResults.RankedDoc(totalResults - 1 - i, minScore + resultDiff * (totalResults - i), input.get(i))
158-
);
153+
try {
154+
List<RankedDocsResults.RankedDoc> results = new ArrayList<>();
155+
for (int i = 0; i < totalResults; i++) {
156+
results.add(new RankedDocsResults.RankedDoc(i, Float.parseFloat(input.get(i)), input.get(i)));
157+
}
158+
return new RankedDocsResults(results.stream().sorted(Comparator.reverseOrder()).toList());
159+
} catch (NumberFormatException ex) {
160+
List<RankedDocsResults.RankedDoc> results = new ArrayList<>();
161+
float minScore = random.nextFloat(-1f, 1f);
162+
float resultDiff = 0.2f;
163+
for (int i = 0; i < input.size(); i++) {
164+
results.add(
165+
new RankedDocsResults.RankedDoc(
166+
totalResults - 1 - i,
167+
minScore + resultDiff * (totalResults - i),
168+
input.get(totalResults - 1 - i)
169+
)
170+
);
171+
}
172+
return new RankedDocsResults(results);
159173
}
160-
return new RankedDocsResults(results);
161174
}
162175

163176
protected ServiceSettings getServiceSettingsFromMap(Map<String, Object> serviceSettingsMap) {

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/InferenceFeatures.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public Set<NodeFeature> getFeatures() {
4040
private static final NodeFeature SEMANTIC_TEXT_HIGHLIGHTER = new NodeFeature("semantic_text.highlighter");
4141
private static final NodeFeature SEMANTIC_TEXT_HIGHLIGHTER_DEFAULT = new NodeFeature("semantic_text.highlighter.default");
4242
private static final NodeFeature SEMANTIC_TEXT_MATCH_ALL_HIGHLIGHTER = new NodeFeature("semantic_text.match_all_highlighter");
43+
private static final NodeFeature TEST_RERANKING_SERVICE_PARSE_TEXT_AS_SCORE = new NodeFeature(
44+
"test_reranking_service.parse_text_as_score"
45+
);
4346

4447
@Override
4548
public Set<NodeFeature> getTestFeatures() {
@@ -59,7 +62,8 @@ public Set<NodeFeature> getTestFeatures() {
5962
SemanticInferenceMetadataFieldsMapper.INFERENCE_METADATA_FIELDS_ENABLED_BY_DEFAULT,
6063
SEMANTIC_TEXT_HIGHLIGHTER_DEFAULT,
6164
SEMANTIC_KNN_FILTER_FIX,
62-
SEMANTIC_TEXT_MATCH_ALL_HIGHLIGHTER
65+
SEMANTIC_TEXT_MATCH_ALL_HIGHLIGHTER,
66+
TEST_RERANKING_SERVICE_PARSE_TEXT_AS_SCORE
6367
);
6468
}
6569
}

x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/70_text_similarity_rank_retriever.yml

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ setup:
22
- skip:
33
features:
44
- close_to
5-
- contains
65
- requires:
76
cluster_features: "text_similarity_reranker_retriever_supported"
87
reason: semantic reranking introduced in 8.15.0
@@ -35,16 +34,8 @@ setup:
3534
type: keyword
3635
subtopic:
3736
type: keyword
38-
39-
- do:
40-
index:
41-
index: test-index
42-
id: doc_1
43-
body:
44-
text: "As seen from Earth, a solar eclipse happens when the Moon is directly between the Earth and the Sun."
45-
topic: [ "science" ]
46-
subtopic: [ "technology" ]
47-
refresh: true
37+
inference_text_field:
38+
type: text
4839

4940
- do:
5041
index:
@@ -54,6 +45,7 @@ setup:
5445
text: "The phases of the Moon come from the position of the Moon relative to the Earth and Sun."
5546
topic: [ "science" ]
5647
subtopic: [ "astronomy" ]
48+
inference_text_field: "0"
5749
refresh: true
5850

5951
- do:
@@ -63,11 +55,27 @@ setup:
6355
body:
6456
text: "Sun Moon Lake is a lake in Nantou County, Taiwan. It is the largest lake in Taiwan."
6557
topic: [ "geography" ]
58+
inference_text_field: "1"
59+
refresh: true
60+
61+
- do:
62+
index:
63+
index: test-index
64+
id: doc_1
65+
body:
66+
text: "As seen from Earth, a solar eclipse happens when the Moon is directly between the Earth and the Sun."
67+
topic: [ "science" ]
68+
subtopic: [ "technology" ]
69+
inference_text_field: "-1"
6670
refresh: true
6771

6872
---
6973
"Simple text similarity rank retriever":
7074

75+
- requires:
76+
cluster_features: "test_reranking_service.parse_text_as_score"
77+
reason: test_reranking_service can now parse provided input as score to provide deterministic ranks
78+
7179
- do:
7280
search:
7381
index: test-index
@@ -77,14 +85,37 @@ setup:
7785
retriever:
7886
text_similarity_reranker:
7987
retriever:
88+
# this one returns docs 1 and 2
8089
standard:
8190
query:
82-
term:
83-
topic: "science"
91+
bool: {
92+
should: [
93+
{
94+
constant_score: {
95+
filter: {
96+
term: {
97+
subtopic: "technology"
98+
}
99+
},
100+
boost: 10
101+
}
102+
},
103+
{
104+
constant_score: {
105+
filter: {
106+
term: {
107+
subtopic: "astronomy"
108+
}
109+
},
110+
boost: 1
111+
}
112+
}
113+
]
114+
}
84115
rank_window_size: 10
85116
inference_id: my-rerank-model
86117
inference_text: "How often does the moon hide the sun?"
87-
field: text
118+
field: inference_text_field
88119
size: 10
89120

90121
- match: { hits.total.value: 2 }
@@ -96,6 +127,10 @@ setup:
96127
---
97128
"Simple text similarity rank retriever and filtering":
98129

130+
- requires:
131+
cluster_features: "test_reranking_service.parse_text_as_score"
132+
reason: test_reranking_service can now parse provided input as score to provide deterministic ranks
133+
99134
- do:
100135
search:
101136
index: test-index
@@ -105,6 +140,7 @@ setup:
105140
retriever:
106141
text_similarity_reranker:
107142
retriever:
143+
# this one returns doc 1
108144
standard:
109145
query:
110146
term:
@@ -115,7 +151,7 @@ setup:
115151
rank_window_size: 10
116152
inference_id: my-rerank-model
117153
inference_text: "How often does the moon hide the sun?"
118-
field: text
154+
field: inference_text_field
119155
size: 10
120156

121157
- match: { hits.total.value: 1 }
@@ -145,7 +181,7 @@ setup:
145181
rank_window_size: 10
146182
inference_id: i-dont-exist
147183
inference_text: "How often does the moon hide the sun?"
148-
field: text
184+
field: inference_text_field
149185
size: 10
150186

151187
---
@@ -171,13 +207,17 @@ setup:
171207
rank_window_size: 10
172208
inference_id: i-dont-exist
173209
inference_text: "asdfasdf"
174-
field: text
210+
field: inference_text_field
175211
size: 10
176212

177213

178214
---
179215
"text similarity reranking with explain":
180216

217+
- requires:
218+
cluster_features: "test_reranking_service.parse_text_as_score"
219+
reason: test_reranking_service can now parse provided input as score to provide deterministic ranks
220+
181221
- do:
182222
search:
183223
index: test-index
@@ -188,28 +228,50 @@ setup:
188228
text_similarity_reranker: {
189229
retriever:
190230
{
231+
# this one returns doc 1 and 2
191232
standard: {
192233
query: {
193-
term: {
194-
topic: "science"
234+
bool: {
235+
should: [
236+
{
237+
constant_score: {
238+
filter: {
239+
term: {
240+
subtopic: "technology"
241+
}
242+
},
243+
boost: 10
244+
}
245+
},
246+
{
247+
constant_score: {
248+
filter: {
249+
term: {
250+
subtopic: "astronomy"
251+
}
252+
},
253+
boost: 1
254+
}
255+
}
256+
]
195257
}
196258
}
197259
}
198260
},
199261
rank_window_size: 10,
200262
inference_id: my-rerank-model,
201263
inference_text: "How often does the moon hide the sun?",
202-
field: text
264+
field: inference_text_field
203265
}
204266
}
205267
size: 10
206268
explain: true
207269

208-
- contains: { hits.hits: { _id: "doc_2" } }
209-
- contains: { hits.hits: { _id: "doc_1" } }
270+
- match: { hits.hits.0._id: "doc_2" }
271+
- match: { hits.hits.1._id: "doc_1" }
210272

211-
- match: {hits.hits.0._explanation.description: "/text_similarity_reranker.match.using.inference.endpoint:.\\[my-rerank-model\\].on.document.field:.\\[text\\].*/" }
212-
- match: {hits.hits.0._explanation.details.0.description: "/weight.*science.*/" }
273+
- match: {hits.hits.0._explanation.description: "/text_similarity_reranker.match.using.inference.endpoint:.\\[my-rerank-model\\].on.document.field:.\\[inference_text_field\\].*/" }
274+
- match: {hits.hits.0._explanation.details.0.details.0.description: "/subtopic.*astronomy.*/" }
213275

214276
---
215277
"text similarity reranker properly handles aliases":
@@ -283,7 +345,7 @@ setup:
283345
rank_window_size: 10
284346
inference_id: my-rerank-model
285347
inference_text: "How often does the moon hide the sun?"
286-
field: text
348+
field: inference_text_field
287349
size: 10
288350

289351
- match: { hits.total.value: 1 }

0 commit comments

Comments
 (0)