Skip to content
Open
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 @@ -51,7 +51,7 @@ public BitmapDocValuesQuery(String field, RoaringBitmap bitmap) {
this.bitmap = bitmap;
if (!bitmap.isEmpty()) {
min = bitmap.first();
max = bitmap.last();
max = Integer.toUnsignedLong(bitmap.last());
} else {
min = 0; // final field
max = 0;
Expand All @@ -71,6 +71,11 @@ public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOExcepti
@Override
public boolean matches() throws IOException {
long value = singleton.longValue();
// Check if negative
if (value < 0) {
// add 2^32 to make it unsigned
value = (1L << 32) + value;
}
return value >= min && value <= max && bitmap.contains((int) value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.lucene.util.RamUsageEstimator;

import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;

import org.roaringbitmap.PeekableIntIterator;
Expand Down Expand Up @@ -71,7 +72,7 @@ interface BitmapIterator extends BytesRefIterator {

private static BitmapIterator bitmapEncodedIterator(RoaringBitmap bitmap) {
return new BitmapIterator() {
private final PeekableIntIterator iterator = bitmap.getIntIterator();
private final PeekableIntIterator iterator = bitmap.getSignedIntIterator();
private final BytesRef encoded = new BytesRef(new byte[Integer.BYTES]);

public BytesRef next() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,48 @@ public void testScoreMutilValues() throws IOException {
Set<Integer> expected = Set.of(2, 3);
assertEquals(expected, actual);
}

public void testScoreWithNegatives() throws IOException {
Document d = new Document();
d.add(new IntField("product_id", 1, Field.Store.NO));
w.addDocument(d);

d = new Document();
d.add(new IntField("product_id", 2, Field.Store.NO));
w.addDocument(d);

d = new Document();
d.add(new IntField("product_id", 3, Field.Store.NO));
w.addDocument(d);

d = new Document();
d.add(new IntField("product_id", 4, Field.Store.NO));
w.addDocument(d);

d = new Document();
d.add(new IntField("product_id", -2, Field.Store.NO));
w.addDocument(d);

d = new Document();
d.add(new IntField("product_id", -3, Field.Store.NO));
w.addDocument(d);

w.commit();
reader = DirectoryReader.open(w);
searcher = newSearcher(reader);

RoaringBitmap bitmap = new RoaringBitmap();
bitmap.add(1);
bitmap.add(-2);
bitmap.add(4);
bitmap.add(-3);
bitmap.add(2);
BitmapDocValuesQuery query = new BitmapDocValuesQuery("product_id", bitmap);

Weight weight = searcher.createWeight(searcher.rewrite(query), ScoreMode.COMPLETE_NO_SCORES, 1f);

List<Integer> actual = getMatchingValues(weight, searcher.getIndexReader());
List<Integer> expected = List.of(1, 2, 4, -2, -3);
assertEquals(expected, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,53 @@ public void testPointVisitor() throws IOException {
Set<Integer> expected = Set.of(1, 2, 3);
assertEquals(expected, actual);
}

public void testScoreWithNegatives() throws IOException {
Document d = new Document();
d.add(new IntField("product_id", 1, Field.Store.NO));
w.addDocument(d);

d = new Document();
d.add(new IntField("product_id", 2, Field.Store.NO));
w.addDocument(d);

d = new Document();
d.add(new IntField("product_id", 3, Field.Store.NO));
w.addDocument(d);

d = new Document();
d.add(new IntField("product_id", 4, Field.Store.NO));
w.addDocument(d);

// Add negative product_ids as they are technically valid integer values
d = new Document();
d.add(new IntField("product_id", -2, Field.Store.NO));
w.addDocument(d);

d = new Document();
d.add(new IntField("product_id", -3, Field.Store.NO));
w.addDocument(d);

w.commit();
reader = DirectoryReader.open(w);
searcher = newSearcher(reader);

RoaringBitmap bitmap = new RoaringBitmap();
bitmap.add(1);
bitmap.add(4);
bitmap.add(-2);
bitmap.add(2);
bitmap.add(-3);
bitmap.add(3);

BitmapIndexQuery query = new BitmapIndexQuery("product_id", bitmap);

Weight weight = searcher.createWeight(searcher.rewrite(query), ScoreMode.COMPLETE_NO_SCORES, 1f);

List<Integer> actual = getMatchingValues(weight, searcher.getIndexReader());
// Has to be sorted. Negative values are greater than Positive as 2^32 is added to negative numbers when
// converting signed int to unsigned int
List<Integer> expected = List.of(1, 2, 3, 4, -2, -3);
assertEquals(expected, actual);
}
}
Loading