Skip to content

[ML] fix random sampling background query consistency #83676

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 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
public final class RandomSamplingQuery extends Query {

private final double p;
private final SplittableRandom splittableRandom;
private final int seed;
private final int hash;

Expand All @@ -49,7 +48,6 @@ public RandomSamplingQuery(double p, int seed, int hash) {
this.p = p;
this.seed = seed;
this.hash = hash;
this.splittableRandom = new SplittableRandom(BitMixer.mix(hash, seed));
}

@Override
Expand Down Expand Up @@ -78,7 +76,7 @@ public Explanation explain(LeafReaderContext context, int doc) throws IOExceptio

@Override
public Scorer scorer(LeafReaderContext context) {
final SplittableRandom random = splittableRandom.split();
final SplittableRandom random = new SplittableRandom(BitMixer.mix(hash, seed));
int maxDoc = context.reader().maxDoc();
return new ConstantScoreScorer(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
import org.apache.lucene.search.DocIdSetIterator;
import org.elasticsearch.test.ESTestCase;

import java.util.ArrayList;
import java.util.List;
import java.util.SplittableRandom;

import static org.hamcrest.Matchers.equalTo;

public class RandomDocIDSetIteratorTests extends ESTestCase {

public void testRandomSampler() {
Expand Down Expand Up @@ -43,4 +47,26 @@ public void testRandomSampler() {
}
}

public void testRandomSamplerConsistency() {
int maxDoc = 10000;
int seed = randomInt();

for (int i = 1; i < 100; i++) {
double p = i / 100.0;
SplittableRandom random = new SplittableRandom(seed);
List<Integer> iterationOne = new ArrayList<>();
RandomSamplingQuery.RandomSamplingIterator iter = new RandomSamplingQuery.RandomSamplingIterator(maxDoc, p, random::nextInt);
while (iter.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
iterationOne.add(iter.docID());
}
random = new SplittableRandom(seed);
List<Integer> iterationTwo = new ArrayList<>();
iter = new RandomSamplingQuery.RandomSamplingIterator(maxDoc, p, random::nextInt);
while (iter.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
iterationTwo.add(iter.docID());
}
assertThat(iterationOne, equalTo(iterationTwo));
}
}

}