Skip to content
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

Improve performance of the bitmap filtering #16936

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Support object fields in star-tree index([#16728](https://github.com/opensearch-project/OpenSearch/pull/16728/))
- Support searching from doc_value using termQueryCaseInsensitive/termQuery in flat_object/keyword field([#16974](https://github.com/opensearch-project/OpenSearch/pull/16974/))
- Added a new `time` field to replace the deprecated `getTime` field in `GetStats`. ([#17009](https://github.com/opensearch-project/OpenSearch/pull/17009))
- Improve performance of the bitmap filtering([#16936](https://github.com/opensearch-project/OpenSearch/pull/16936/))

### Dependencies
- Bump `com.google.cloud:google-cloud-core-http` from 2.23.0 to 2.47.0 ([#16504](https://github.com/opensearch-project/OpenSearch/pull/16504))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@
import org.apache.lucene.sandbox.document.HalfFloatPoint;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PointInSetQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils;
Expand All @@ -73,6 +71,7 @@
import org.opensearch.search.DocValueFormat;
import org.opensearch.search.lookup.SearchLookup;
import org.opensearch.search.query.BitmapDocValuesQuery;
import org.opensearch.search.query.BitmapIndexQuery;

import java.io.IOException;
import java.math.BigInteger;
Expand All @@ -81,7 +80,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -888,10 +886,10 @@ public Query bitmapQuery(String field, BytesArray bitmapArray, boolean isSearcha
}

if (isSearchable && hasDocValues) {
return new IndexOrDocValuesQuery(bitmapIndexQuery(field, bitmap), new BitmapDocValuesQuery(field, bitmap));
return new IndexOrDocValuesQuery(new BitmapIndexQuery(field, bitmap), new BitmapDocValuesQuery(field, bitmap));
}
if (isSearchable) {
return bitmapIndexQuery(field, bitmap);
return new BitmapIndexQuery(field, bitmap);
}
return new BitmapDocValuesQuery(field, bitmap);
}
Expand Down Expand Up @@ -1507,40 +1505,6 @@ public static Query unsignedLongRangeQuery(
}
return builder.apply(l, u);
}

static PointInSetQuery bitmapIndexQuery(String field, RoaringBitmap bitmap) {
final BytesRef encoded = new BytesRef(new byte[Integer.BYTES]);
return new PointInSetQuery(field, 1, Integer.BYTES, new PointInSetQuery.Stream() {

final Iterator<Integer> iterator = bitmap.iterator();

@Override
public BytesRef next() {
int value;
if (iterator.hasNext()) {
value = iterator.next();
} else {
return null;
}
IntPoint.encodeDimension(value, encoded.bytes, 0);
return encoded;
}
}) {
@Override
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
if (bitmap.isEmpty()) {
return new MatchNoDocsQuery();
}
return super.rewrite(indexSearcher);
}

@Override
protected String toString(byte[] value) {
assert value.length == Integer.BYTES;
return Integer.toString(IntPoint.decodeDimension(value, 0));
}
};
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

import org.roaringbitmap.RoaringBitmap;

import static org.opensearch.search.query.BitmapIndexQuery.checkArgs;

/**
* Filter with bitmap
* <p>
Expand All @@ -43,6 +45,7 @@
final long max;

public BitmapDocValuesQuery(String field, RoaringBitmap bitmap) {
checkArgs(field, bitmap);
this.field = field;
this.bitmap = bitmap;
if (!bitmap.isEmpty()) {
Expand Down Expand Up @@ -111,8 +114,7 @@

@Override
public String toString(String field) {
// bitmap may contain high cardinality, so choose to not show the actual values in it
return field + " cardinality: " + bitmap.getLongCardinality();
return "BitmapDocValuesQuery(field=" + this.field + ")";
}

@Override
Expand All @@ -139,8 +141,8 @@

@Override
public long ramBytesUsed() {
return RamUsageEstimator.shallowSizeOfInstance(BitmapDocValuesQuery.class) + RamUsageEstimator.sizeOfObject(field)
+ RamUsageEstimator.sizeOfObject(bitmap);
return RamUsageEstimator.shallowSizeOfInstance(BitmapIndexQuery.class) + RamUsageEstimator.sizeOf(field) + bitmap
.getLongSizeInBytes();

Check warning on line 145 in server/src/main/java/org/opensearch/search/query/BitmapDocValuesQuery.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/search/query/BitmapDocValuesQuery.java#L144-L145

Added lines #L144 - L145 were not covered by tests
}

@Override
Expand Down
Loading
Loading