Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
73cb2cf
Added TODO
Mikep86 Feb 4, 2026
a61fe7b
Added top_n task setting to test reranking service
Mikep86 Feb 4, 2026
4422cab
Added YAML test
Mikep86 Feb 4, 2026
e3fef43
Updated ranked doc score extraction
Mikep86 Feb 4, 2026
e75f55f
Don't reverse sort order
Mikep86 Feb 5, 2026
fabafe5
Added TopNProvider interface
Mikep86 Feb 5, 2026
422caf5
Updated TextSimilarityRankFeaturePhaseRankCoordinatorContext to use T…
Mikep86 Feb 5, 2026
8a97f42
Spotless
Mikep86 Feb 5, 2026
ab9023c
Updated TestTaskSettings to implement TopNProvider. Also added a sett…
Mikep86 Feb 5, 2026
b01d8e4
Updated YAML tests
Mikep86 Feb 5, 2026
e62a551
Update extractScoresFromRankedDocs to detect when not all feature doc…
Mikep86 Feb 5, 2026
e3a7a09
Spotless
Mikep86 Feb 5, 2026
5e2ca36
Remove unnecessary validation check
Mikep86 Feb 5, 2026
3944ed1
Don't resolve chunking settings
Mikep86 Feb 6, 2026
8bcdf0e
Remove ChunkScorerConfig from TextSimilarityRankFeaturePhaseRankCoord…
Mikep86 Feb 6, 2026
47bdb69
Spotless
Mikep86 Feb 6, 2026
1a5e013
Fix unit tests
Mikep86 Feb 6, 2026
202cffb
Added empty ranked docs test
Mikep86 Feb 6, 2026
ad28cab
Assert on null feature data
Mikep86 Feb 6, 2026
49e1b67
Fix test
Mikep86 Feb 6, 2026
d9be5aa
Added cluster feature
Mikep86 Feb 6, 2026
de90b23
Improved ranked doc size error check
Mikep86 Feb 6, 2026
978062b
Merge branch 'main' into text-similarity-reranker_aioob-error
Mikep86 Feb 6, 2026
e724bc7
Update docs/changelog/142039.yaml
Mikep86 Feb 6, 2026
206959a
Add allow_rerank_failures test case
Mikep86 Feb 9, 2026
5116dd9
Merge branch 'main' into text-similarity-reranker_aioob-error
elasticmachine Feb 9, 2026
e62445f
Gracefully handle a feature doc with no features
Mikep86 Feb 10, 2026
1f661b7
Merge branch 'main' into text-similarity-reranker_aioob-error
Mikep86 Feb 10, 2026
25b4367
Merge branch 'text-similarity-reranker_aioob-error' of github.com:Mik…
Mikep86 Feb 10, 2026
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/142039.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
area: Ranking
issues: []
pr: 142039
summary: Implement comprehensive top N parameter handling for text similarity reranker
type: bug
14 changes: 14 additions & 0 deletions server/src/main/java/org/elasticsearch/inference/TopNProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.inference;

public interface TopNProvider {
Integer getTopN();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.inference.SettingsConfiguration;
import org.elasticsearch.inference.TaskSettings;
import org.elasticsearch.inference.TaskType;
import org.elasticsearch.inference.TopNProvider;
import org.elasticsearch.inference.UnifiedCompletionRequest;
import org.elasticsearch.inference.configuration.SettingsConfigurationFieldType;
import org.elasticsearch.rest.RestStatus;
Expand All @@ -40,7 +41,6 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -194,7 +194,14 @@ private RankedDocsResults makeResults(List<String> input, TestRerankingServiceEx
for (int i = 0; i < totalResults; i++) {
results.add(new RankedDocsResults.RankedDoc(i, Float.parseFloat(input.get(i)), input.get(i)));
}
return new RankedDocsResults(results.stream().sorted(Comparator.reverseOrder()).toList());

// RankedDoc's compareTo implementation already sorts by score descending, so we don't need to reverse the sort order
var sortedResultsStream = results.stream().sorted();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it turns out that our test reranker service has been sorting docs in reverse relevance this whole time 🫠 . We didn't catch it as an issue because ES sorts the results again (correctly) here. This all happens to work, as long as reranker reranks every doc sent to it. However, if the reranker truncates results (like if a top_n parameter is applied 😉 ), then the incorrect sort order becomes an issue.

if (taskSettings.topN != null) {
sortedResultsStream = sortedResultsStream.limit(taskSettings.topN);
}

return new RankedDocsResults(sortedResultsStream.toList());
} catch (NumberFormatException ex) {
return makeResultFromTextInput(input, taskSettings);
}
Expand All @@ -216,6 +223,10 @@ private RankedDocsResults makeResultFromTextInput(List<String> input, TestRerank
}
// Ensure result are sorted by descending score
results.sort((a, b) -> -Float.compare(a.relevanceScore(), b.relevanceScore()));
if (taskSettings.topN != null && taskSettings.topN < results.size()) {
results = results.subList(0, taskSettings.topN);
}

return new RankedDocsResults(results);
}

Expand Down Expand Up @@ -257,9 +268,14 @@ public static InferenceServiceConfiguration get() {
}
}

public record TestTaskSettings(boolean shouldFailValidation, boolean useTextLength, float minScore, float resultDiff)
implements
TaskSettings {
public record TestTaskSettings(
boolean shouldFailValidation,
boolean useTextLength,
float minScore,
float resultDiff,
Integer topN,
boolean hideTopN
) implements TaskSettings, TopNProvider {

static final String NAME = "test_reranking_task_settings";

Expand All @@ -268,6 +284,8 @@ public static TestTaskSettings fromMap(Map<String, Object> map) {
boolean useTextLength = false;
float minScore = random.nextFloat(-1f, 1f);
float resultDiff = 0.2f;
Integer topN = null;
boolean hideTopN = false;

if (map.containsKey("should_fail_validation")) {
shouldFailValidation = Boolean.parseBoolean(map.remove("should_fail_validation").toString());
Expand All @@ -285,11 +303,19 @@ public static TestTaskSettings fromMap(Map<String, Object> map) {
resultDiff = Float.parseFloat(map.remove("result_diff").toString());
}

return new TestTaskSettings(shouldFailValidation, useTextLength, minScore, resultDiff);
if (map.containsKey("top_n")) {
topN = Integer.parseInt(map.remove("top_n").toString());
}

if (map.containsKey("hide_top_n")) {
hideTopN = Boolean.parseBoolean(map.remove("hide_top_n").toString());
}

return new TestTaskSettings(shouldFailValidation, useTextLength, minScore, resultDiff, topN, hideTopN);
}

public TestTaskSettings(StreamInput in) throws IOException {
this(in.readBoolean(), in.readBoolean(), in.readFloat(), in.readFloat());
this(in.readBoolean(), in.readBoolean(), in.readFloat(), in.readFloat(), in.readOptionalInt(), in.readBoolean());
}

@Override
Expand All @@ -303,18 +329,30 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(useTextLength);
out.writeFloat(minScore);
out.writeFloat(resultDiff);
out.writeOptionalInt(topN);
out.writeBoolean(hideTopN);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("should_fail_validation", shouldFailValidation);
builder.field("use_text_length", useTextLength);
builder.field("min_score", minScore);
builder.field("result_diff", resultDiff);
if (topN != null) {
builder.field("top_n", topN);
}
builder.field("hide_top_n", hideTopN);
builder.endObject();
return builder;
}

@Override
public Integer getTopN() {
return hideTopN ? null : topN;
}

@Override
public String getWriteableName() {
return NAME;
Expand All @@ -332,7 +370,9 @@ public TaskSettings updatedTaskSettings(Map<String, Object> newSettingsMap) {
newSettingsMap.containsKey("should_fail_validation") ? newSettingsObject.shouldFailValidation() : shouldFailValidation,
newSettingsMap.containsKey("use_text_length") ? newSettingsObject.useTextLength() : useTextLength,
newSettingsMap.containsKey("min_score") ? newSettingsObject.minScore() : minScore,
newSettingsMap.containsKey("result_diff") ? newSettingsObject.resultDiff() : resultDiff
newSettingsMap.containsKey("result_diff") ? newSettingsObject.resultDiff() : resultDiff,
newSettingsMap.containsKey("top_n") ? newSettingsObject.topN() : topN,
newSettingsMap.containsKey("hide_top_n") ? newSettingsObject.hideTopN() : hideTopN
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public class InferenceFeatures implements FeatureSpecification {
public static final NodeFeature TEXT_SIMILARITY_RERANKER_INFERENCE_ID_CHUNKING = new NodeFeature(
"text_similarity_reranker_inference_id_chunking"
);
public static final NodeFeature TEXT_SIMILARITY_RERANKER_COMPREHENSIVE_TOP_N_HANDLING = new NodeFeature(
"text_similarity_reranker.comprehensive_top_n_handling"
);
public static final NodeFeature INFERENCE_AUTH_POLLER_PERSISTENT_TASK = new NodeFeature("inference.auth_poller.persistent_task");
public static final NodeFeature INFERENCE_CCM_ENABLEMENT_SERVICE = new NodeFeature("inference.ccm.enablement_service");

Expand Down Expand Up @@ -133,7 +136,8 @@ public Set<NodeFeature> getTestFeatures() {
SEARCH_USAGE_EXTENDED_DATA,
TEXT_SIMILARITY_RANK_DOC_EXPLAIN_CHUNKS,
RETRIEVER_RESULT_DIVERSIFICATION_USES_QUERY_VECTOR_BUILDER,
TEXT_SIMILARITY_RERANKER_INFERENCE_ID_CHUNKING
TEXT_SIMILARITY_RERANKER_INFERENCE_ID_CHUNKING,
TEXT_SIMILARITY_RERANKER_COMPREHENSIVE_TOP_N_HANDLING
)
);
testFeatures.addAll(getFeatures());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,7 @@ public RankFeaturePhaseRankCoordinatorContext buildRankFeaturePhaseCoordinatorCo
inferenceId,
inferenceText,
minScore,
failuresAllowed,
chunkScorerConfig != null
? new ChunkScorerConfig(chunkScorerConfig.size, inferenceText, chunkScorerConfig.chunkingSettings())
: null
failuresAllowed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup here!

);
}

Expand Down
Loading