Skip to content

Commit 45420dc

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

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
@@ -28,6 +28,9 @@ public class InferenceFeatures implements FeatureSpecification {
2828
private static final NodeFeature SEMANTIC_TEXT_HIGHLIGHTER = new NodeFeature("semantic_text.highlighter");
2929
private static final NodeFeature SEMANTIC_TEXT_HIGHLIGHTER_DEFAULT = new NodeFeature("semantic_text.highlighter.default");
3030
private static final NodeFeature SEMANTIC_TEXT_MATCH_ALL_HIGHLIGHTER = new NodeFeature("semantic_text.match_all_highlighter");
31+
private static final NodeFeature TEST_RERANKING_SERVICE_PARSE_TEXT_AS_SCORE = new NodeFeature(
32+
"test_reranking_service.parse_text_as_score"
33+
);
3134

3235
@Override
3336
public Set<NodeFeature> getTestFeatures() {
@@ -47,7 +50,8 @@ public Set<NodeFeature> getTestFeatures() {
4750
SemanticInferenceMetadataFieldsMapper.INFERENCE_METADATA_FIELDS_ENABLED_BY_DEFAULT,
4851
SEMANTIC_TEXT_HIGHLIGHTER_DEFAULT,
4952
SEMANTIC_KNN_FILTER_FIX,
50-
SEMANTIC_TEXT_MATCH_ALL_HIGHLIGHTER
53+
SEMANTIC_TEXT_MATCH_ALL_HIGHLIGHTER,
54+
TEST_RERANKING_SERVICE_PARSE_TEXT_AS_SCORE
5155
);
5256
}
5357
}

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
test_runner_features: "close_to"
87

@@ -33,16 +32,8 @@ setup:
3332
type: keyword
3433
subtopic:
3534
type: keyword
36-
37-
- do:
38-
index:
39-
index: test-index
40-
id: doc_1
41-
body:
42-
text: "As seen from Earth, a solar eclipse happens when the Moon is directly between the Earth and the Sun."
43-
topic: [ "science" ]
44-
subtopic: [ "technology" ]
45-
refresh: true
35+
inference_text_field:
36+
type: text
4637

4738
- do:
4839
index:
@@ -52,6 +43,7 @@ setup:
5243
text: "The phases of the Moon come from the position of the Moon relative to the Earth and Sun."
5344
topic: [ "science" ]
5445
subtopic: [ "astronomy" ]
46+
inference_text_field: "0"
5547
refresh: true
5648

5749
- do:
@@ -61,11 +53,27 @@ setup:
6153
body:
6254
text: "Sun Moon Lake is a lake in Nantou County, Taiwan. It is the largest lake in Taiwan."
6355
topic: [ "geography" ]
56+
inference_text_field: "1"
57+
refresh: true
58+
59+
- do:
60+
index:
61+
index: test-index
62+
id: doc_1
63+
body:
64+
text: "As seen from Earth, a solar eclipse happens when the Moon is directly between the Earth and the Sun."
65+
topic: [ "science" ]
66+
subtopic: [ "technology" ]
67+
inference_text_field: "-1"
6468
refresh: true
6569

6670
---
6771
"Simple text similarity rank retriever":
6872

73+
- requires:
74+
cluster_features: "test_reranking_service.parse_text_as_score"
75+
reason: test_reranking_service can now parse provided input as score to provide deterministic ranks
76+
6977
- do:
7078
search:
7179
index: test-index
@@ -75,14 +83,37 @@ setup:
7583
retriever:
7684
text_similarity_reranker:
7785
retriever:
86+
# this one returns docs 1 and 2
7887
standard:
7988
query:
80-
term:
81-
topic: "science"
89+
bool: {
90+
should: [
91+
{
92+
constant_score: {
93+
filter: {
94+
term: {
95+
subtopic: "technology"
96+
}
97+
},
98+
boost: 10
99+
}
100+
},
101+
{
102+
constant_score: {
103+
filter: {
104+
term: {
105+
subtopic: "astronomy"
106+
}
107+
},
108+
boost: 1
109+
}
110+
}
111+
]
112+
}
82113
rank_window_size: 10
83114
inference_id: my-rerank-model
84115
inference_text: "How often does the moon hide the sun?"
85-
field: text
116+
field: inference_text_field
86117
size: 10
87118

88119
- match: { hits.total.value: 2 }
@@ -94,6 +125,10 @@ setup:
94125
---
95126
"Simple text similarity rank retriever and filtering":
96127

128+
- requires:
129+
cluster_features: "test_reranking_service.parse_text_as_score"
130+
reason: test_reranking_service can now parse provided input as score to provide deterministic ranks
131+
97132
- do:
98133
search:
99134
index: test-index
@@ -103,6 +138,7 @@ setup:
103138
retriever:
104139
text_similarity_reranker:
105140
retriever:
141+
# this one returns doc 1
106142
standard:
107143
query:
108144
term:
@@ -113,7 +149,7 @@ setup:
113149
rank_window_size: 10
114150
inference_id: my-rerank-model
115151
inference_text: "How often does the moon hide the sun?"
116-
field: text
152+
field: inference_text_field
117153
size: 10
118154

119155
- match: { hits.total.value: 1 }
@@ -143,7 +179,7 @@ setup:
143179
rank_window_size: 10
144180
inference_id: i-dont-exist
145181
inference_text: "How often does the moon hide the sun?"
146-
field: text
182+
field: inference_text_field
147183
size: 10
148184

149185
---
@@ -169,13 +205,17 @@ setup:
169205
rank_window_size: 10
170206
inference_id: i-dont-exist
171207
inference_text: "asdfasdf"
172-
field: text
208+
field: inference_text_field
173209
size: 10
174210

175211

176212
---
177213
"text similarity reranking with explain":
178214

215+
- requires:
216+
cluster_features: "test_reranking_service.parse_text_as_score"
217+
reason: test_reranking_service can now parse provided input as score to provide deterministic ranks
218+
179219
- do:
180220
search:
181221
index: test-index
@@ -186,28 +226,50 @@ setup:
186226
text_similarity_reranker: {
187227
retriever:
188228
{
229+
# this one returns doc 1 and 2
189230
standard: {
190231
query: {
191-
term: {
192-
topic: "science"
232+
bool: {
233+
should: [
234+
{
235+
constant_score: {
236+
filter: {
237+
term: {
238+
subtopic: "technology"
239+
}
240+
},
241+
boost: 10
242+
}
243+
},
244+
{
245+
constant_score: {
246+
filter: {
247+
term: {
248+
subtopic: "astronomy"
249+
}
250+
},
251+
boost: 1
252+
}
253+
}
254+
]
193255
}
194256
}
195257
}
196258
},
197259
rank_window_size: 10,
198260
inference_id: my-rerank-model,
199261
inference_text: "How often does the moon hide the sun?",
200-
field: text
262+
field: inference_text_field
201263
}
202264
}
203265
size: 10
204266
explain: true
205267

206-
- contains: { hits.hits: { _id: "doc_2" } }
207-
- contains: { hits.hits: { _id: "doc_1" } }
268+
- match: { hits.hits.0._id: "doc_2" }
269+
- match: { hits.hits.1._id: "doc_1" }
208270

209-
- match: {hits.hits.0._explanation.description: "/text_similarity_reranker.match.using.inference.endpoint:.\\[my-rerank-model\\].on.document.field:.\\[text\\].*/" }
210-
- match: {hits.hits.0._explanation.details.0.description: "/weight.*science.*/" }
271+
- match: {hits.hits.0._explanation.description: "/text_similarity_reranker.match.using.inference.endpoint:.\\[my-rerank-model\\].on.document.field:.\\[inference_text_field\\].*/" }
272+
- match: {hits.hits.0._explanation.details.0.details.0.description: "/subtopic.*astronomy.*/" }
211273

212274
---
213275
"text similarity reranker properly handles aliases":
@@ -281,7 +343,7 @@ setup:
281343
rank_window_size: 10
282344
inference_id: my-rerank-model
283345
inference_text: "How often does the moon hide the sun?"
284-
field: text
346+
field: inference_text_field
285347
size: 10
286348

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

0 commit comments

Comments
 (0)