Tighten up IndexSortSortedNumericDocValuesRangeQuery index sort validation

This commit is contained in:
Greg Miller 2024-11-01 13:20:09 -07:00
parent cfdd20f5bc
commit da8c9b98a4
1 changed files with 39 additions and 40 deletions

View File

@ -192,23 +192,27 @@ public class IndexSortSortedNumericDocValuesRangeQuery extends Query {
IteratorAndCount itAndCount = null; IteratorAndCount itAndCount = null;
LeafReader reader = context.reader(); LeafReader reader = context.reader();
Sort indexSort = reader.getMetaData().sort();
if (indexSort == null
|| indexSort.getSort().length == 0
|| indexSort.getSort()[0].getField().equals(field) == false) {
// If the index sort is actually using `field` we can't optimize and need to fall back:
return fallbackWeight.count(context);
}
SortField sortField = indexSort.getSort()[0];
// first use bkd optimization if possible // first use bkd optimization if possible
SortedNumericDocValues sortedNumericValues = DocValues.getSortedNumeric(reader, field); SortedNumericDocValues sortedNumericValues = DocValues.getSortedNumeric(reader, field);
NumericDocValues numericValues = DocValues.unwrapSingleton(sortedNumericValues); NumericDocValues numericValues = DocValues.unwrapSingleton(sortedNumericValues);
PointValues pointValues = reader.getPointValues(field); PointValues pointValues = reader.getPointValues(field);
if (pointValues != null && pointValues.getDocCount() == reader.maxDoc()) { if (pointValues != null && pointValues.getDocCount() == reader.maxDoc()) {
itAndCount = getDocIdSetIteratorOrNullFromBkd(context, numericValues); itAndCount = getDocIdSetIteratorOrNullFromBkd(sortField, context, numericValues);
} }
if (itAndCount != null && itAndCount.count != -1) { if (itAndCount != null && itAndCount.count != -1) {
return itAndCount.count; return itAndCount.count;
} }
// use index sort optimization if possible // use index sort optimization if possible
Sort indexSort = reader.getMetaData().sort();
if (indexSort != null
&& indexSort.getSort().length > 0
&& indexSort.getSort()[0].getField().equals(field)) {
final SortField sortField = indexSort.getSort()[0];
final SortField.Type sortFieldType = getSortFieldType(sortField); final SortField.Type sortFieldType = getSortFieldType(sortField);
// The index sort optimization is only supported for Type.INT and Type.LONG // The index sort optimization is only supported for Type.INT and Type.LONG
if (sortFieldType == Type.INT || sortFieldType == Type.LONG) { if (sortFieldType == Type.INT || sortFieldType == Type.LONG) {
@ -224,7 +228,6 @@ public class IndexSortSortedNumericDocValuesRangeQuery extends Query {
} }
} }
} }
}
return fallbackWeight.count(context); return fallbackWeight.count(context);
} }
}; };
@ -431,15 +434,9 @@ public class IndexSortSortedNumericDocValuesRangeQuery extends Query {
} }
private IteratorAndCount getDocIdSetIteratorOrNullFromBkd( private IteratorAndCount getDocIdSetIteratorOrNullFromBkd(
LeafReaderContext context, DocIdSetIterator delegate) throws IOException { SortField sortField, LeafReaderContext context, DocIdSetIterator delegate)
Sort indexSort = context.reader().getMetaData().sort(); throws IOException {
if (indexSort == null final boolean reverse = sortField.getReverse();
|| indexSort.getSort().length == 0
|| indexSort.getSort()[0].getField().equals(field) == false) {
return null;
}
final boolean reverse = indexSort.getSort()[0].getReverse();
PointValues points = context.reader().getPointValues(field); PointValues points = context.reader().getPointValues(field);
if (points == null) { if (points == null) {
@ -524,27 +521,29 @@ public class IndexSortSortedNumericDocValuesRangeQuery extends Query {
return IteratorAndCount.empty(); return IteratorAndCount.empty();
} }
Sort indexSort = context.reader().getMetaData().sort();
if (indexSort == null
|| indexSort.getSort().length == 0
|| indexSort.getSort()[0].getField().equals(field) == false) {
return null;
}
SortField sortField = indexSort.getSort()[0];
SortedNumericDocValues sortedNumericValues = SortedNumericDocValues sortedNumericValues =
DocValues.getSortedNumeric(context.reader(), field); DocValues.getSortedNumeric(context.reader(), field);
NumericDocValues numericValues = DocValues.unwrapSingleton(sortedNumericValues); NumericDocValues numericValues = DocValues.unwrapSingleton(sortedNumericValues);
if (numericValues != null) { if (numericValues != null) {
IteratorAndCount itAndCount = getDocIdSetIteratorOrNullFromBkd(context, numericValues); IteratorAndCount itAndCount =
getDocIdSetIteratorOrNullFromBkd(sortField, context, numericValues);
if (itAndCount != null) { if (itAndCount != null) {
return itAndCount; return itAndCount;
} }
Sort indexSort = context.reader().getMetaData().sort();
if (indexSort != null
&& indexSort.getSort().length > 0
&& indexSort.getSort()[0].getField().equals(field)) {
final SortField sortField = indexSort.getSort()[0];
final SortField.Type sortFieldType = getSortFieldType(sortField); final SortField.Type sortFieldType = getSortFieldType(sortField);
// The index sort optimization is only supported for Type.INT and Type.LONG // The index sort optimization is only supported for Type.INT and Type.LONG
if (sortFieldType == Type.INT || sortFieldType == Type.LONG) { if (sortFieldType == Type.INT || sortFieldType == Type.LONG) {
return getDocIdSetIterator(sortField, sortFieldType, context, numericValues); return getDocIdSetIterator(sortField, sortFieldType, context, numericValues);
} }
} }
}
return null; return null;
} }