Skip to content

Commit cf8e543

Browse files
committed
lazy load the score of point in set query
Signed-off-by: bowenlan-amzn <bowenlan23@gmail.com>
1 parent ab546ae commit cf8e543

File tree

1 file changed

+82
-22
lines changed

1 file changed

+82
-22
lines changed

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

+82-22
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,22 @@
4242
import org.apache.lucene.document.LongPoint;
4343
import org.apache.lucene.document.SortedNumericDocValuesField;
4444
import org.apache.lucene.document.StoredField;
45+
import org.apache.lucene.index.LeafReaderContext;
4546
import org.apache.lucene.sandbox.document.BigIntegerPoint;
4647
import org.apache.lucene.sandbox.document.HalfFloatPoint;
4748
import org.apache.lucene.search.BoostQuery;
49+
import org.apache.lucene.search.ConstantScoreWeight;
4850
import org.apache.lucene.search.IndexOrDocValuesQuery;
4951
import org.apache.lucene.search.IndexSearcher;
5052
import org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery;
5153
import org.apache.lucene.search.MatchNoDocsQuery;
5254
import org.apache.lucene.search.PointInSetQuery;
5355
import org.apache.lucene.search.Query;
56+
import org.apache.lucene.search.QueryVisitor;
57+
import org.apache.lucene.search.ScoreMode;
58+
import org.apache.lucene.search.Scorer;
59+
import org.apache.lucene.search.ScorerSupplier;
60+
import org.apache.lucene.search.Weight;
5461
import org.apache.lucene.util.BytesRef;
5562
import org.apache.lucene.util.NumericUtils;
5663
import org.opensearch.common.Explicit;
@@ -1508,36 +1515,89 @@ public static Query unsignedLongRangeQuery(
15081515
return builder.apply(l, u);
15091516
}
15101517

1511-
static PointInSetQuery bitmapIndexQuery(String field, RoaringBitmap bitmap) {
1512-
final BytesRef encoded = new BytesRef(new byte[Integer.BYTES]);
1513-
return new PointInSetQuery(field, 1, Integer.BYTES, new PointInSetQuery.Stream() {
1518+
static Query bitmapIndexQuery(String field, RoaringBitmap bitmap) {
1519+
return new Query() {
15141520

1515-
final Iterator<Integer> iterator = bitmap.iterator();
1521+
@Override
1522+
public String toString(String field) {
1523+
return "";
1524+
}
15161525

15171526
@Override
1518-
public BytesRef next() {
1519-
int value;
1520-
if (iterator.hasNext()) {
1521-
value = iterator.next();
1522-
} else {
1523-
return null;
1524-
}
1525-
IntPoint.encodeDimension(value, encoded.bytes, 0);
1526-
return encoded;
1527+
public void visit(QueryVisitor visitor) {
1528+
15271529
}
1528-
}) {
1530+
15291531
@Override
1530-
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
1531-
if (bitmap.isEmpty()) {
1532-
return new MatchNoDocsQuery();
1533-
}
1534-
return super.rewrite(indexSearcher);
1532+
public boolean equals(Object obj) {
1533+
return false;
15351534
}
15361535

15371536
@Override
1538-
protected String toString(byte[] value) {
1539-
assert value.length == Integer.BYTES;
1540-
return Integer.toString(IntPoint.decodeDimension(value, 0));
1537+
public int hashCode() {
1538+
return 0;
1539+
}
1540+
1541+
@Override
1542+
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) {
1543+
return new ConstantScoreWeight(this, boost) {
1544+
@Override
1545+
public Scorer scorer(LeafReaderContext context) throws IOException {
1546+
return scorerSupplier(context).get(Long.MAX_VALUE);
1547+
}
1548+
1549+
@Override
1550+
public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException {
1551+
return new ScorerSupplier() {
1552+
@Override
1553+
public Scorer get(long leadCost) throws IOException {
1554+
1555+
final BytesRef encoded = new BytesRef(new byte[Integer.BYTES]);
1556+
Query query = new PointInSetQuery(field, 1, Integer.BYTES, new PointInSetQuery.Stream() {
1557+
1558+
final Iterator<Integer> iterator = bitmap.iterator();
1559+
1560+
@Override
1561+
public BytesRef next() {
1562+
int value;
1563+
if (iterator.hasNext()) {
1564+
value = iterator.next();
1565+
} else {
1566+
return null;
1567+
}
1568+
IntPoint.encodeDimension(value, encoded.bytes, 0);
1569+
return encoded;
1570+
}
1571+
}) {
1572+
@Override
1573+
public Query rewrite(IndexSearcher indexSearcher) throws IOException {
1574+
if (bitmap.isEmpty()) {
1575+
return new MatchNoDocsQuery();
1576+
}
1577+
return super.rewrite(indexSearcher);
1578+
}
1579+
1580+
@Override
1581+
protected String toString(byte[] value) {
1582+
assert value.length == Integer.BYTES;
1583+
return Integer.toString(IntPoint.decodeDimension(value, 0));
1584+
}
1585+
};
1586+
return query.createWeight(searcher, scoreMode, boost).scorer(context);
1587+
}
1588+
1589+
@Override
1590+
public long cost() {
1591+
return bitmap.getLongCardinality();
1592+
}
1593+
};
1594+
}
1595+
1596+
@Override
1597+
public boolean isCacheable(LeafReaderContext ctx) {
1598+
return false;
1599+
}
1600+
};
15411601
}
15421602
};
15431603
}

0 commit comments

Comments
 (0)