Skip to content

Commit 63f1cd4

Browse files
committed
sketchy implementation
Signed-off-by: bowenlan-amzn <[email protected]>
1 parent 839d7c9 commit 63f1cd4

File tree

3 files changed

+48
-43
lines changed

3 files changed

+48
-43
lines changed

server/src/main/java/org/opensearch/index/mapper/NumberFieldMapper.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.opensearch.common.settings.Setting;
5959
import org.opensearch.common.settings.Setting.Property;
6060
import org.opensearch.common.settings.Settings;
61+
import org.opensearch.core.common.bytes.BytesArray;
6162
import org.opensearch.core.xcontent.XContentParser;
6263
import org.opensearch.core.xcontent.XContentParser.Token;
6364
import org.opensearch.index.document.SortedUnsignedLongDocValuesRangeQuery;
@@ -69,10 +70,10 @@
6970
import org.opensearch.search.DocValueFormat;
7071
import org.opensearch.search.lookup.SearchLookup;
7172
import org.opensearch.search.query.BitMapFilterQuery;
72-
import org.roaringbitmap.RoaringBitmap;
7373

7474
import java.io.IOException;
7575
import java.math.BigInteger;
76+
import java.nio.ByteBuffer;
7677
import java.time.ZoneId;
7778
import java.util.ArrayList;
7879
import java.util.Arrays;
@@ -84,6 +85,8 @@
8485
import java.util.function.Function;
8586
import java.util.function.Supplier;
8687

88+
import org.roaringbitmap.RoaringBitmap;
89+
8790
/**
8891
* A {@link FieldMapper} for numeric types: byte, short, int, long, float and double.
8992
*
@@ -790,9 +793,14 @@ public Query termQuery(String field, Object value, boolean hasDocValues, boolean
790793

791794
@Override
792795
public Query termsQuery(String field, List<Object> values, boolean hasDocValues, boolean isSearchable) {
793-
if (values.get(0) instanceof RoaringBitmap) {
794-
RoaringBitmap bm = (RoaringBitmap) values.get(0);
795-
return new BitMapFilterQuery(field, bm);
796+
if (values.get(0) instanceof BytesArray) {
797+
RoaringBitmap bitmap = new RoaringBitmap();
798+
try {
799+
bitmap.deserialize(ByteBuffer.wrap(((BytesArray) values.get(0)).array()));
800+
return new BitMapFilterQuery(field, bitmap);
801+
} catch (IOException e) {
802+
throw new RuntimeException(e);
803+
}
796804
}
797805

798806
int[] v = new int[values.size()];

server/src/main/java/org/opensearch/index/query/TermsQueryBuilder.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import org.opensearch.index.mapper.ConstantFieldType;
5555
import org.opensearch.index.mapper.MappedFieldType;
5656
import org.opensearch.indices.TermsLookup;
57-
import org.roaringbitmap.RoaringBitmap;
5857

5958
import java.io.IOException;
6059
import java.nio.CharBuffer;
@@ -474,20 +473,16 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
474473
if (fieldType == null) {
475474
throw new IllegalStateException("Rewrite first");
476475
}
477-
478-
RoaringBitmap bm = new RoaringBitmap();
479-
for (Object value : values) {
480-
bm.add(((Long) value).intValue());
481-
}
482-
483-
return fieldType.termsQuery(List.of(bm), context);
476+
return fieldType.termsQuery(values, context);
484477
}
485478

486479
private void fetch(TermsLookup termsLookup, Client client, ActionListener<List<Object>> actionListener) {
487480
GetRequest getRequest = new GetRequest(termsLookup.index(), termsLookup.id());
488481
getRequest.preference("_local").routing(termsLookup.routing());
482+
getRequest.storedFields(termsLookup.path());
489483
client.get(getRequest, ActionListener.delegateFailure(actionListener, (delegatedListener, getResponse) -> {
490484
List<Object> terms = new ArrayList<>();
485+
terms.addAll(getResponse.getField(termsLookup.path()).getValues());
491486
if (getResponse.isSourceEmpty() == false) { // extract terms only if the doc source exists
492487
List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap());
493488
terms.addAll(extractedValues);

server/src/main/java/org/opensearch/search/query/BitMapFilterQuery.java

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
import org.apache.lucene.search.TwoPhaseIterator;
2323
import org.apache.lucene.search.Weight;
2424
import org.apache.lucene.util.Accountable;
25-
import org.roaringbitmap.RoaringBitmap;
2625

2726
import java.io.IOException;
2827

28+
import org.roaringbitmap.RoaringBitmap;
29+
30+
/**
31+
* Filter with bitmap
32+
*/
2933
public class BitMapFilterQuery extends Query implements Accountable {
3034

3135
final String field;
@@ -45,39 +49,37 @@ public Scorer scorer(LeafReaderContext context) throws IOException {
4549
final NumericDocValues singleton = DocValues.unwrapSingleton(values);
4650
final TwoPhaseIterator iterator;
4751
if (singleton != null) {
48-
iterator =
49-
new TwoPhaseIterator(singleton) {
50-
@Override
51-
public boolean matches() throws IOException {
52-
long value = singleton.longValue();
53-
return bitmap.contains((int) value);
54-
}
55-
56-
@Override
57-
public float matchCost() {
58-
return 5; // 2 comparisions, possible lookup in the set
59-
}
60-
};
52+
iterator = new TwoPhaseIterator(singleton) {
53+
@Override
54+
public boolean matches() throws IOException {
55+
long value = singleton.longValue();
56+
return bitmap.contains((int) value);
57+
}
58+
59+
@Override
60+
public float matchCost() {
61+
return 5; // 2 comparisions, possible lookup in the set
62+
}
63+
};
6164
} else {
62-
iterator =
63-
new TwoPhaseIterator(values) {
64-
@Override
65-
public boolean matches() throws IOException {
66-
int count = values.docValueCount();
67-
for (int i = 0; i < count; i++) {
68-
final long value = values.nextValue();
69-
if (bitmap.contains((int) value)) {
70-
return true;
71-
}
65+
iterator = new TwoPhaseIterator(values) {
66+
@Override
67+
public boolean matches() throws IOException {
68+
int count = values.docValueCount();
69+
for (int i = 0; i < count; i++) {
70+
final long value = values.nextValue();
71+
if (bitmap.contains((int) value)) {
72+
return true;
7273
}
73-
return false;
74-
}
75-
76-
@Override
77-
public float matchCost() {
78-
return 5; // 2 comparisons, possible lookup in the set
7974
}
80-
};
75+
return false;
76+
}
77+
78+
@Override
79+
public float matchCost() {
80+
return 5; // 2 comparisons, possible lookup in the set
81+
}
82+
};
8183
}
8284
return new ConstantScoreScorer(this, score(), scoreMode, iterator);
8385
}

0 commit comments

Comments
 (0)