Skip to content

Commit f4f13c9

Browse files
committed
Set scores where there are none
1 parent bfbcca3 commit f4f13c9

File tree

2 files changed

+93
-7
lines changed

2 files changed

+93
-7
lines changed

server/src/main/java/org/elasticsearch/action/search/RankFeaturePhase.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.search.builder.SearchSourceBuilder;
2121
import org.elasticsearch.search.dfs.AggregatedDfs;
2222
import org.elasticsearch.search.internal.ShardSearchContextId;
23+
import org.elasticsearch.search.rank.RankDoc;
2324
import org.elasticsearch.search.rank.context.RankFeaturePhaseRankCoordinatorContext;
2425
import org.elasticsearch.search.rank.feature.RankFeatureDoc;
2526
import org.elasticsearch.search.rank.feature.RankFeatureResult;
@@ -187,7 +188,7 @@ private void onPhaseDone(
187188
new ActionListener<>() {
188189
@Override
189190
public void onResponse(RankFeatureDoc[] docsWithUpdatedScores) {
190-
RankFeatureDoc[] topResults = rankFeaturePhaseRankCoordinatorContext.rankAndPaginate(docsWithUpdatedScores, true);
191+
RankDoc[] topResults = rankFeaturePhaseRankCoordinatorContext.rankAndPaginate(docsWithUpdatedScores, true);
191192
SearchPhaseController.ReducedQueryPhase reducedRankFeaturePhase = newReducedQueryPhaseResults(
192193
reducedQueryPhase,
193194
topResults
@@ -200,13 +201,18 @@ public void onFailure(Exception e) {
200201
if (rankFeaturePhaseRankCoordinatorContext.failuresAllowed()) {
201202
// TODO: handle the exception somewhere
202203
// don't want to log the entire stack trace, it's not helpful here
203-
logger.warn("Exception computing updated ranks: {}. Continuing with existing ranks.", e.toString());
204+
logger.warn("Exception computing updated ranks, continuing with existing ranks: {}", e.toString());
204205
// use the existing score docs as-is
205-
RankFeatureDoc[] existingScores = Arrays.stream(reducedQueryPhase.sortedTopDocs().scoreDocs())
206-
.map(sd -> new RankFeatureDoc(sd.doc, sd.score, sd.shardIndex))
207-
.toArray(RankFeatureDoc[]::new);
208-
209-
RankFeatureDoc[] topResults = rankFeaturePhaseRankCoordinatorContext.rankAndPaginate(existingScores, false);
206+
// downstream things expect every doc to have a score, so we need to infer a score here
207+
// if the doc doesn't otherwise have a score. We can use the rank.
208+
ScoreDoc[] inputDocs = reducedQueryPhase.sortedTopDocs().scoreDocs();
209+
// use RankDoc to indicate there was a problem using the specified features
210+
RankFeatureDoc[] rankDocs = new RankFeatureDoc[inputDocs.length];
211+
for (int i = 0; i < inputDocs.length; i++) {
212+
ScoreDoc doc = inputDocs[i];
213+
rankDocs[i] = new RankFeatureDoc(doc.doc, Float.isNaN(doc.score) ? 1f / (i+1) : doc.score, doc.shardIndex);
214+
}
215+
RankDoc[] topResults = rankFeaturePhaseRankCoordinatorContext.rankAndPaginate(rankDocs, false);
210216
SearchPhaseController.ReducedQueryPhase reducedRankFeaturePhase = newReducedQueryPhaseResults(
211217
reducedQueryPhase,
212218
topResults

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,83 @@ setup:
334334
- match: {hits.hits.0._explanation.details.1.description: "/rrf.score:.\\[0.5\\].*/" }
335335
- match: {hits.hits.0._explanation.details.1.details.0.description: "/text_similarity_reranker.match.using.inference.endpoint:.\\[my-rerank-model\\].on.document.field:.\\[text\\].*/" }
336336
- match: {hits.hits.0._explanation.details.1.details.0.details.0.description: "/weight.*astronomy.*/" }
337+
338+
---
339+
"rrf retriever with failed text similarity reranker":
340+
341+
- do:
342+
search:
343+
index: test-index
344+
body:
345+
track_total_hits: true
346+
fields: [ "text", "topic" ]
347+
retriever:
348+
rrf: {
349+
retrievers:
350+
[
351+
{
352+
standard: {
353+
query: {
354+
bool: {
355+
should:
356+
[
357+
{
358+
constant_score: {
359+
filter: {
360+
term: {
361+
integer: 1
362+
}
363+
},
364+
boost: 10
365+
}
366+
},
367+
{
368+
constant_score:
369+
{
370+
filter:
371+
{
372+
term:
373+
{
374+
integer: 2
375+
}
376+
},
377+
boost: 1
378+
}
379+
}
380+
]
381+
}
382+
}
383+
}
384+
},
385+
{
386+
text_similarity_reranker: {
387+
retriever:
388+
{
389+
standard: {
390+
query: {
391+
match_all: {}
392+
},
393+
sort: {
394+
integer: "asc"
395+
}
396+
}
397+
},
398+
rank_window_size: 10,
399+
inference_id: failure-rerank-model,
400+
inference_text: "How often does the moon hide the sun?",
401+
field: text,
402+
allow_rerank_failures: true
403+
}
404+
}
405+
],
406+
rank_window_size: 10,
407+
rank_constant: 1
408+
}
409+
size: 10
410+
411+
- match: { hits.total.value: 3 }
412+
- length: { hits.hits: 3 }
413+
414+
- match: { hits.hits.0._id: "doc_1" }
415+
- match: { hits.hits.1._id: "doc_2" }
416+
- match: { hits.hits.2._id: "doc_3" }

0 commit comments

Comments
 (0)