Skip to content

Commit dcd2462

Browse files
authored
Semantic reranking should fail whenever inference ID does not exist (#112038) (#112153)
* Semantic reranking should fail whenever inference ID does not exist * Short circuit text similarity reranking on empty result set * Update tests * Remove test - it doesn't do anything useful * Update docs/changelog/112038.yaml
1 parent d626197 commit dcd2462

File tree

5 files changed

+104
-23
lines changed

5 files changed

+104
-23
lines changed

docs/changelog/112038.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 112038
2+
summary: Semantic reranking should fail whenever inference ID does not exist
3+
area: Relevance
4+
type: bug
5+
issues:
6+
- 111934

server/src/main/java/org/elasticsearch/search/rank/context/RankFeaturePhaseRankCoordinatorContext.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,12 @@ public void computeRankScoresForGlobalResults(
7474
RankFeatureDoc[] featureDocs = extractFeatureDocs(rankSearchResults);
7575

7676
// generate the final `topResults` results, and pass them to fetch phase through the `rankListener`
77-
if (featureDocs.length == 0) {
78-
rankListener.onResponse(new RankFeatureDoc[0]);
79-
} else {
80-
computeScores(featureDocs, rankListener.delegateFailureAndWrap((listener, scores) -> {
81-
for (int i = 0; i < featureDocs.length; i++) {
82-
featureDocs[i].score = scores[i];
83-
}
84-
listener.onResponse(featureDocs);
85-
}));
86-
}
77+
computeScores(featureDocs, rankListener.delegateFailureAndWrap((listener, scores) -> {
78+
for (int i = 0; i < featureDocs.length; i++) {
79+
featureDocs[i].score = scores[i];
80+
}
81+
listener.onResponse(featureDocs);
82+
}));
8783
}
8884

8985
/**

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/rank/textsimilarity/TextSimilarityRankFeaturePhaseRankCoordinatorContext.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ protected void computeScores(RankFeatureDoc[] featureDocs, ActionListener<float[
6262

6363
// Ensure we get exactly as many scores as the number of docs we passed, otherwise we may return incorrect results
6464
List<RankedDocsResults.RankedDoc> rankedDocs = ((RankedDocsResults) results).getRankedDocs();
65+
6566
if (rankedDocs.size() != featureDocs.length) {
6667
l.onFailure(
6768
new IllegalStateException(
@@ -104,12 +105,18 @@ protected void computeScores(RankFeatureDoc[] featureDocs, ActionListener<float[
104105
);
105106
return;
106107
}
107-
List<String> featureData = Arrays.stream(featureDocs).map(x -> x.featureData).toList();
108-
InferenceAction.Request inferenceRequest = generateRequest(featureData);
109-
try {
110-
client.execute(InferenceAction.INSTANCE, inferenceRequest, inferenceListener);
111-
} finally {
112-
inferenceRequest.decRef();
108+
109+
// Short circuit on empty results after request validation
110+
if (featureDocs.length == 0) {
111+
inferenceListener.onResponse(new InferenceAction.Response(new RankedDocsResults(List.of())));
112+
} else {
113+
List<String> featureData = Arrays.stream(featureDocs).map(x -> x.featureData).toList();
114+
InferenceAction.Request inferenceRequest = generateRequest(featureData);
115+
try {
116+
client.execute(InferenceAction.INSTANCE, inferenceRequest, inferenceListener);
117+
} finally {
118+
inferenceRequest.decRef();
119+
}
113120
}
114121
});
115122

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/rank/textsimilarity/TextSimilarityRankFeaturePhaseRankCoordinatorContextTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,23 @@ public void onFailure(Exception e) {
6161
);
6262
}
6363

64+
public void testComputeScoresForEmpty() {
65+
subject.computeScores(new RankFeatureDoc[0], new ActionListener<>() {
66+
@Override
67+
public void onResponse(float[] floats) {
68+
assertArrayEquals(new float[0], floats, 0.0f);
69+
}
70+
71+
@Override
72+
public void onFailure(Exception e) {
73+
fail();
74+
}
75+
});
76+
verify(mockClient).execute(
77+
eq(GetInferenceModelAction.INSTANCE),
78+
argThat(actionRequest -> ((GetInferenceModelAction.Request) actionRequest).getTaskType().equals(TaskType.RERANK)),
79+
any()
80+
);
81+
}
82+
6483
}

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

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ setup:
3838
id: doc_1
3939
body:
4040
text: "As seen from Earth, a solar eclipse happens when the Moon is directly between the Earth and the Sun."
41-
topic: ["science"]
42-
subtopic: ["technology"]
41+
topic: [ "science" ]
42+
subtopic: [ "technology" ]
4343
refresh: true
4444

4545
- do:
@@ -48,8 +48,8 @@ setup:
4848
id: doc_2
4949
body:
5050
text: "The phases of the Moon come from the position of the Moon relative to the Earth and Sun."
51-
topic: ["science"]
52-
subtopic: ["astronomy"]
51+
topic: [ "science" ]
52+
subtopic: [ "astronomy" ]
5353
refresh: true
5454

5555
- do:
@@ -58,7 +58,7 @@ setup:
5858
id: doc_3
5959
body:
6060
text: "Sun Moon Lake is a lake in Nantou County, Taiwan. It is the largest lake in Taiwan."
61-
topic: ["geography"]
61+
topic: [ "geography" ]
6262
refresh: true
6363
---
6464
"Simple text similarity rank retriever":
@@ -82,7 +82,7 @@ setup:
8282
field: text
8383
size: 10
8484

85-
- match: { hits.total.value : 2 }
85+
- match: { hits.total.value: 2 }
8686
- length: { hits.hits: 2 }
8787

8888
- match: { hits.hits.0._id: "doc_2" }
@@ -118,9 +118,62 @@ setup:
118118
field: text
119119
size: 10
120120

121-
- match: { hits.total.value : 1 }
121+
- match: { hits.total.value: 1 }
122122
- length: { hits.hits: 1 }
123123

124124
- match: { hits.hits.0._id: "doc_1" }
125125
- match: { hits.hits.0._rank: 1 }
126126
- close_to: { hits.hits.0._score: { value: 0.2, error: 0.001 } }
127+
128+
129+
---
130+
"Text similarity reranking fails if the inference ID does not exist":
131+
- do:
132+
catch: /Inference endpoint not found/
133+
search:
134+
index: test-index
135+
body:
136+
track_total_hits: true
137+
fields: [ "text", "topic" ]
138+
retriever:
139+
text_similarity_reranker:
140+
retriever:
141+
standard:
142+
query:
143+
term:
144+
topic: "science"
145+
filter:
146+
term:
147+
subtopic: "technology"
148+
rank_window_size: 10
149+
inference_id: i-dont-exist
150+
inference_text: "How often does the moon hide the sun?"
151+
field: text
152+
size: 10
153+
154+
---
155+
"Text similarity reranking fails if the inference ID does not exist and result set is empty":
156+
- requires:
157+
cluster_features: "gte_v8.15.1"
158+
reason: bug fixed in 8.15.1
159+
160+
- do:
161+
catch: /Inference endpoint not found/
162+
search:
163+
index: test-index
164+
body:
165+
track_total_hits: true
166+
fields: [ "text", "topic" ]
167+
retriever:
168+
text_similarity_reranker:
169+
retriever:
170+
standard:
171+
query:
172+
term:
173+
topic: "asdfasdf"
174+
rank_window_size: 10
175+
inference_id: i-dont-exist
176+
inference_text: "asdfasdf"
177+
field: text
178+
size: 10
179+

0 commit comments

Comments
 (0)