Skip to content

Commit

Permalink
Semantic reranking should fail whenever inference ID does not exist (e…
Browse files Browse the repository at this point in the history
…lastic#112038) (elastic#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
  • Loading branch information
kderusso authored Aug 23, 2024
1 parent d626197 commit dcd2462
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 23 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/112038.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 112038
summary: Semantic reranking should fail whenever inference ID does not exist
area: Relevance
type: bug
issues:
- 111934
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,12 @@ public void computeRankScoresForGlobalResults(
RankFeatureDoc[] featureDocs = extractFeatureDocs(rankSearchResults);

// generate the final `topResults` results, and pass them to fetch phase through the `rankListener`
if (featureDocs.length == 0) {
rankListener.onResponse(new RankFeatureDoc[0]);
} else {
computeScores(featureDocs, rankListener.delegateFailureAndWrap((listener, scores) -> {
for (int i = 0; i < featureDocs.length; i++) {
featureDocs[i].score = scores[i];
}
listener.onResponse(featureDocs);
}));
}
computeScores(featureDocs, rankListener.delegateFailureAndWrap((listener, scores) -> {
for (int i = 0; i < featureDocs.length; i++) {
featureDocs[i].score = scores[i];
}
listener.onResponse(featureDocs);
}));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ protected void computeScores(RankFeatureDoc[] featureDocs, ActionListener<float[

// Ensure we get exactly as many scores as the number of docs we passed, otherwise we may return incorrect results
List<RankedDocsResults.RankedDoc> rankedDocs = ((RankedDocsResults) results).getRankedDocs();

if (rankedDocs.size() != featureDocs.length) {
l.onFailure(
new IllegalStateException(
Expand Down Expand Up @@ -104,12 +105,18 @@ protected void computeScores(RankFeatureDoc[] featureDocs, ActionListener<float[
);
return;
}
List<String> featureData = Arrays.stream(featureDocs).map(x -> x.featureData).toList();
InferenceAction.Request inferenceRequest = generateRequest(featureData);
try {
client.execute(InferenceAction.INSTANCE, inferenceRequest, inferenceListener);
} finally {
inferenceRequest.decRef();

// Short circuit on empty results after request validation
if (featureDocs.length == 0) {
inferenceListener.onResponse(new InferenceAction.Response(new RankedDocsResults(List.of())));
} else {
List<String> featureData = Arrays.stream(featureDocs).map(x -> x.featureData).toList();
InferenceAction.Request inferenceRequest = generateRequest(featureData);
try {
client.execute(InferenceAction.INSTANCE, inferenceRequest, inferenceListener);
} finally {
inferenceRequest.decRef();
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ public void onFailure(Exception e) {
);
}

public void testComputeScoresForEmpty() {
subject.computeScores(new RankFeatureDoc[0], new ActionListener<>() {
@Override
public void onResponse(float[] floats) {
assertArrayEquals(new float[0], floats, 0.0f);
}

@Override
public void onFailure(Exception e) {
fail();
}
});
verify(mockClient).execute(
eq(GetInferenceModelAction.INSTANCE),
argThat(actionRequest -> ((GetInferenceModelAction.Request) actionRequest).getTaskType().equals(TaskType.RERANK)),
any()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ setup:
id: doc_1
body:
text: "As seen from Earth, a solar eclipse happens when the Moon is directly between the Earth and the Sun."
topic: ["science"]
subtopic: ["technology"]
topic: [ "science" ]
subtopic: [ "technology" ]
refresh: true

- do:
Expand All @@ -48,8 +48,8 @@ setup:
id: doc_2
body:
text: "The phases of the Moon come from the position of the Moon relative to the Earth and Sun."
topic: ["science"]
subtopic: ["astronomy"]
topic: [ "science" ]
subtopic: [ "astronomy" ]
refresh: true

- do:
Expand All @@ -58,7 +58,7 @@ setup:
id: doc_3
body:
text: "Sun Moon Lake is a lake in Nantou County, Taiwan. It is the largest lake in Taiwan."
topic: ["geography"]
topic: [ "geography" ]
refresh: true
---
"Simple text similarity rank retriever":
Expand All @@ -82,7 +82,7 @@ setup:
field: text
size: 10

- match: { hits.total.value : 2 }
- match: { hits.total.value: 2 }
- length: { hits.hits: 2 }

- match: { hits.hits.0._id: "doc_2" }
Expand Down Expand Up @@ -118,9 +118,62 @@ setup:
field: text
size: 10

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

- match: { hits.hits.0._id: "doc_1" }
- match: { hits.hits.0._rank: 1 }
- close_to: { hits.hits.0._score: { value: 0.2, error: 0.001 } }


---
"Text similarity reranking fails if the inference ID does not exist":
- do:
catch: /Inference endpoint not found/
search:
index: test-index
body:
track_total_hits: true
fields: [ "text", "topic" ]
retriever:
text_similarity_reranker:
retriever:
standard:
query:
term:
topic: "science"
filter:
term:
subtopic: "technology"
rank_window_size: 10
inference_id: i-dont-exist
inference_text: "How often does the moon hide the sun?"
field: text
size: 10

---
"Text similarity reranking fails if the inference ID does not exist and result set is empty":
- requires:
cluster_features: "gte_v8.15.1"
reason: bug fixed in 8.15.1

- do:
catch: /Inference endpoint not found/
search:
index: test-index
body:
track_total_hits: true
fields: [ "text", "topic" ]
retriever:
text_similarity_reranker:
retriever:
standard:
query:
term:
topic: "asdfasdf"
rank_window_size: 10
inference_id: i-dont-exist
inference_text: "asdfasdf"
field: text
size: 10

0 comments on commit dcd2462

Please sign in to comment.