Skip to content

Commit 0ab34c2

Browse files
committed
iter
1 parent 30eb8f6 commit 0ab34c2

File tree

8 files changed

+46
-14
lines changed

8 files changed

+46
-14
lines changed

docs/reference/search/retriever.asciidoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,23 @@ You have the following options:
523523
** Then set up an <<inference-example-eland,{es} service inference endpoint>> with the `rerank` task type.
524524
** Refer to the <<text-similarity-reranker-retriever-example-eland,example>> on this page for a step-by-step guide.
525525

526+
[IMPORTANT]
527+
====
528+
Scores from the re-ranking process are normalized using the following formula before returned to the user,
529+
to avoid having negative scores.
530+
[source,text]
531+
----
532+
score = max(score, 0) + min(exp(score), 1)
533+
----
534+
Using the above, any initially negative scores are projected to (0, 1) and positive scores to [1, infinity).
535+
To revert back if needed, one can use:
536+
[source, text]
537+
----
538+
score = score - 1, if score >= 0
539+
score = ln(score), if score < 0
540+
----
541+
====
542+
526543
===== Parameters
527544

528545
`retriever`::

server/src/main/java/org/elasticsearch/search/retriever/CompoundRetrieverBuilder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
import java.io.IOException;
3838
import java.util.ArrayList;
39-
import java.util.Arrays;
4039
import java.util.List;
4140
import java.util.Locale;
4241
import java.util.Objects;

server/src/main/java/org/elasticsearch/search/retriever/rankdoc/RankDocsQuery.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.apache.lucene.search.Scorer;
2525
import org.apache.lucene.search.ScorerSupplier;
2626
import org.apache.lucene.search.Weight;
27-
import org.elasticsearch.index.query.RankDocsQueryBuilder;
2827
import org.elasticsearch.search.rank.RankDoc;
2928

3029
import java.io.IOException;

test/framework/src/main/java/org/elasticsearch/search/rank/rerank/AbstractRerankerIT.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public enum ThrowingRankBuilderType {
5656

5757
protected abstract Collection<Class<? extends Plugin>> pluginsNeeded();
5858

59+
protected boolean shouldCheckScores() {
60+
return true;
61+
}
62+
5963
@Override
6064
protected Collection<Class<? extends Plugin>> nodePlugins() {
6165
return pluginsNeeded();
@@ -95,9 +99,11 @@ public void testRerankerNoExceptions() throws Exception {
9599
int rank = 1;
96100
for (SearchHit searchHit : response.getHits().getHits()) {
97101
assertThat(searchHit, hasId(String.valueOf(5 - (rank - 1))));
98-
assertEquals(0.5f - ((rank - 1) * 0.1f), searchHit.getScore(), 1e-5f);
99102
assertThat(searchHit, hasRank(rank));
100103
assertNotNull(searchHit.getFields().get(searchField));
104+
if (shouldCheckScores()) {
105+
assertEquals(0.5f - ((rank - 1) * 0.1f), searchHit.getScore(), 1e-5f);
106+
}
101107
rank++;
102108
}
103109
}
@@ -140,9 +146,11 @@ public void testRerankerPagination() throws Exception {
140146
int rank = 3;
141147
for (SearchHit searchHit : response.getHits().getHits()) {
142148
assertThat(searchHit, hasId(String.valueOf(5 - (rank - 1))));
143-
assertEquals(0.5f - ((rank - 1) * 0.1f), searchHit.getScore(), 1e-5f);
144149
assertThat(searchHit, hasRank(rank));
145150
assertNotNull(searchHit.getFields().get(searchField));
151+
if (shouldCheckScores()) {
152+
assertEquals(0.5f - ((rank - 1) * 0.1f), searchHit.getScore(), 1e-5f);
153+
}
146154
rank++;
147155
}
148156
}
@@ -222,9 +230,11 @@ public void testNotAllShardsArePresentInFetchPhase() throws Exception {
222230
int rank = 1;
223231
for (SearchHit searchHit : response.getHits().getHits()) {
224232
assertThat(searchHit, hasId(String.valueOf(5 - (rank - 1))));
225-
assertEquals(0.5f - ((rank - 1) * 0.1f), searchHit.getScore(), 1e-5f);
226233
assertThat(searchHit, hasRank(rank));
227234
assertNotNull(searchHit.getFields().get(searchField));
235+
if (shouldCheckScores()) {
236+
assertEquals(0.5f - ((rank - 1) * 0.1f), searchHit.getScore(), 1e-5f);
237+
}
228238
rank++;
229239
}
230240
}

x-pack/plugin/inference/qa/test-service-plugin/src/main/java/org/elasticsearch/xpack/inference/mock/AbstractTestInferenceService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@
2626
import java.io.IOException;
2727
import java.util.HashMap;
2828
import java.util.Map;
29+
import java.util.Random;
2930

3031
public abstract class AbstractTestInferenceService implements InferenceService {
3132

33+
protected final Random random = new Random(
34+
System.getProperty("tests.seed") == null
35+
? System.currentTimeMillis()
36+
: Long.parseUnsignedLong(System.getProperty("tests.seed").split(":")[0], 16)
37+
);
38+
3239
protected static int stringWeight(String input, int position) {
3340
int hashCode = input.hashCode();
3441
if (hashCode < 0) {

x-pack/plugin/inference/qa/test-service-plugin/src/main/java/org/elasticsearch/xpack/inference/mock/TestRerankingServiceExtension.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.util.HashMap;
4141
import java.util.List;
4242
import java.util.Map;
43-
import java.util.Random;
4443

4544
public class TestRerankingServiceExtension implements InferenceServiceExtension {
4645

@@ -151,10 +150,12 @@ public void chunkedInfer(
151150
private RankedDocsResults makeResults(List<String> input) {
152151
List<RankedDocsResults.RankedDoc> results = new ArrayList<>();
153152
int totalResults = input.size();
154-
float minScore = new Random().nextFloat(-1f, 1f);
153+
float minScore = random.nextFloat(-1f, 1f);
155154
float resultDiff = 0.2f;
156155
for (int i = 0; i < input.size(); i++) {
157-
results.add(new RankedDocsResults.RankedDoc(totalResults - 1 - i, minScore + resultDiff * (totalResults - i), input.get(i)));
156+
results.add(
157+
new RankedDocsResults.RankedDoc(totalResults - 1 - i, minScore + resultDiff * (totalResults - i), input.get(i))
158+
);
158159
}
159160
return new RankedDocsResults(results);
160161
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public void testQueryPhaseShardThrowingAllShardsFail() throws Exception {
5050
public void testQueryPhaseCoordinatorThrowingAllShardsFail() throws Exception {
5151
// no-op
5252
}
53+
54+
@Override
55+
protected boolean shouldCheckScores() {
56+
return false;
57+
}
5358
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ setup:
8989
- length: { hits.hits: 2 }
9090

9191
- match: { hits.hits.0._id: "doc_2" }
92-
- close_to: { hits.hits.0._score: { value: 0.4, error: 0.001 } }
93-
9492
- match: { hits.hits.1._id: "doc_1" }
95-
- close_to: { hits.hits.1._score: { value: 0.2, error: 0.001 } }
9693

9794
---
9895
"Simple text similarity rank retriever and filtering":
@@ -123,8 +120,6 @@ setup:
123120
- length: { hits.hits: 1 }
124121

125122
- match: { hits.hits.0._id: "doc_1" }
126-
- close_to: { hits.hits.0._score: { value: 0.2, error: 0.001 } }
127-
128123

129124
---
130125
"Text similarity reranking fails if the inference ID does not exist":
@@ -211,7 +206,6 @@ setup:
211206
- contains: { hits.hits: { _id: "doc_2" } }
212207
- contains: { hits.hits: { _id: "doc_1" } }
213208

214-
- close_to: { hits.hits.0._explanation.value: { value: 0.4, error: 0.000001 } }
215209
- match: {hits.hits.0._explanation.description: "/text_similarity_reranker.match.using.inference.endpoint:.\\[my-rerank-model\\].on.document.field:.\\[text\\].*/" }
216210
- match: {hits.hits.0._explanation.details.0.description: "/weight.*science.*/" }
217211

0 commit comments

Comments
 (0)