-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Reciprocal Rank Fusion (RRF) in TopDocs #13470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c6da395
add function rrf signature
harenlin 9a7b57b
update TopDocs
harenlin 57327aa
fix TopDocs error
harenlin ce55148
Float -> float in TopDocs.java RRF
harenlin 4a6e10f
fix bug in rrf
harenlin ccb940f
Merge branch 'apache:main' into TopDocs-RRF
harenlin 8e21bbc
style: gradlew tidy
hack4chang c018e50
feat: add javadocs on rrf method
hack4chang 9579816
fix: gradlew format
hack4chang 8d8f760
Merge branch 'main' into TopDocs-RRF
jpountz 68b041c
Fixes + tests.
jpountz 99c616c
Merge branch 'main' into TopDocs-RRF
jpountz 3164006
Remove usage of break + label
jpountz 67cc61f
CHANGES
jpountz 2af86e3
Merge branch 'main' into TopDocs-RRF
jpountz 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
lucene/core/src/test/org/apache/lucene/search/TestTopDocsRRF.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.lucene.search; | ||
|
|
||
| import org.apache.lucene.tests.util.LuceneTestCase; | ||
|
|
||
| public class TestTopDocsRRF extends LuceneTestCase { | ||
|
|
||
| public void testBasics() { | ||
| TopDocs td1 = | ||
| new TopDocs( | ||
| new TotalHits(100, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), | ||
| new ScoreDoc[] {new ScoreDoc(42, 10f), new ScoreDoc(10, 5f), new ScoreDoc(20, 3f)}); | ||
| TopDocs td2 = | ||
| new TopDocs( | ||
| new TotalHits(80, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), | ||
| new ScoreDoc[] {new ScoreDoc(10, 10f), new ScoreDoc(20, 5f)}); | ||
|
|
||
| TopDocs rrfTd = TopDocs.rrf(3, 20, new TopDocs[] {td1, td2}); | ||
| assertEquals(new TotalHits(100, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), rrfTd.totalHits); | ||
|
|
||
| ScoreDoc[] rrfScoreDocs = rrfTd.scoreDocs; | ||
| assertEquals(3, rrfScoreDocs.length); | ||
|
|
||
| assertEquals(10, rrfScoreDocs[0].doc); | ||
| assertEquals(-1, rrfScoreDocs[0].shardIndex); | ||
| assertEquals((float) (1d / (20 + 2) + 1d / (20 + 1)), rrfScoreDocs[0].score, 0f); | ||
|
|
||
| assertEquals(20, rrfScoreDocs[1].doc); | ||
| assertEquals(-1, rrfScoreDocs[1].shardIndex); | ||
| assertEquals((float) (1d / (20 + 3) + 1d / (20 + 2)), rrfScoreDocs[1].score, 0f); | ||
|
|
||
| assertEquals(42, rrfScoreDocs[2].doc); | ||
| assertEquals(-1, rrfScoreDocs[2].shardIndex); | ||
| assertEquals((float) (1d / (20 + 1)), rrfScoreDocs[2].score, 0f); | ||
| } | ||
|
|
||
| public void testShardIndex() { | ||
| TopDocs td1 = | ||
| new TopDocs( | ||
| new TotalHits(100, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), | ||
| new ScoreDoc[] { | ||
| new ScoreDoc(42, 10f, 0), new ScoreDoc(10, 5f, 1), new ScoreDoc(20, 3f, 0) | ||
| }); | ||
| TopDocs td2 = | ||
| new TopDocs( | ||
| new TotalHits(80, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), | ||
| new ScoreDoc[] {new ScoreDoc(10, 10f, 1), new ScoreDoc(20, 5f, 1)}); | ||
|
|
||
| TopDocs rrfTd = TopDocs.rrf(3, 20, new TopDocs[] {td1, td2}); | ||
| assertEquals(new TotalHits(100, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), rrfTd.totalHits); | ||
|
|
||
| ScoreDoc[] rrfScoreDocs = rrfTd.scoreDocs; | ||
| assertEquals(3, rrfScoreDocs.length); | ||
|
|
||
| assertEquals(10, rrfScoreDocs[0].doc); | ||
| assertEquals(1, rrfScoreDocs[0].shardIndex); | ||
| assertEquals((float) (1d / (20 + 2) + 1d / (20 + 1)), rrfScoreDocs[0].score, 0f); | ||
|
|
||
| assertEquals(42, rrfScoreDocs[1].doc); | ||
| assertEquals(0, rrfScoreDocs[1].shardIndex); | ||
| assertEquals((float) (1d / (20 + 1)), rrfScoreDocs[1].score, 0f); | ||
|
|
||
| assertEquals(20, rrfScoreDocs[2].doc); | ||
| assertEquals(1, rrfScoreDocs[2].shardIndex); | ||
| assertEquals((float) (1d / (20 + 2)), rrfScoreDocs[2].score, 0f); | ||
| } | ||
|
|
||
| public void testInconsistentShardIndex() { | ||
| TopDocs td1 = | ||
| new TopDocs( | ||
| new TotalHits(100, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), | ||
| new ScoreDoc[] { | ||
| new ScoreDoc(42, 10f, 0), new ScoreDoc(10, 5f, 1), new ScoreDoc(20, 3f, 0) | ||
| }); | ||
| TopDocs td2 = | ||
| new TopDocs( | ||
| new TotalHits(80, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), | ||
| new ScoreDoc[] {new ScoreDoc(10, 10f, -1), new ScoreDoc(20, 5f, -1)}); | ||
|
|
||
| IllegalArgumentException e = | ||
| expectThrows( | ||
| IllegalArgumentException.class, () -> TopDocs.rrf(3, 20, new TopDocs[] {td1, td2})); | ||
| assertTrue(e.getMessage().contains("shardIndex")); | ||
| } | ||
|
|
||
| public void testInvalidTopN() { | ||
| TopDocs td1 = | ||
| new TopDocs( | ||
| new TotalHits(100, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), new ScoreDoc[0]); | ||
| TopDocs td2 = | ||
| new TopDocs( | ||
| new TotalHits(80, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), new ScoreDoc[0]); | ||
|
|
||
| IllegalArgumentException e = | ||
| expectThrows( | ||
| IllegalArgumentException.class, () -> TopDocs.rrf(0, 20, new TopDocs[] {td1, td2})); | ||
| assertTrue(e.getMessage().contains("topN")); | ||
| } | ||
|
|
||
| public void testInvalidK() { | ||
| TopDocs td1 = | ||
| new TopDocs( | ||
| new TotalHits(100, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), new ScoreDoc[0]); | ||
| TopDocs td2 = | ||
| new TopDocs( | ||
| new TotalHits(80, TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO), new ScoreDoc[0]); | ||
|
|
||
| IllegalArgumentException e = | ||
| expectThrows( | ||
| IllegalArgumentException.class, () -> TopDocs.rrf(3, 0, new TopDocs[] {td1, td2})); | ||
| assertTrue(e.getMessage().contains("k")); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the purpose here to only check the first scoreDoc of every TopDocs instance provided in the array? Should we try and rewrite this to be more readable and not use goto ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it look better now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, thank you!