use of org.apache.lucene.search.comparators.DoubleComparator in project OpenSearch by opensearch-project.
the class GeoDistanceSortBuilder method comparatorSource.
private IndexFieldData.XFieldComparatorSource comparatorSource(GeoPoint[] localPoints, MultiValueMode localSortMode, IndexGeoPointFieldData geoIndexFieldData, Nested nested) {
return new IndexFieldData.XFieldComparatorSource(null, localSortMode, nested) {
@Override
public SortField.Type reducedType() {
return SortField.Type.DOUBLE;
}
private NumericDoubleValues getNumericDoubleValues(LeafReaderContext context) throws IOException {
final MultiGeoPointValues geoPointValues = geoIndexFieldData.load(context).getGeoPointValues();
final SortedNumericDoubleValues distanceValues = GeoUtils.distanceValues(geoDistance, unit, geoPointValues, localPoints);
if (nested == null) {
return FieldData.replaceMissing(sortMode.select(distanceValues), Double.POSITIVE_INFINITY);
} else {
final BitSet rootDocs = nested.rootDocs(context);
final DocIdSetIterator innerDocs = nested.innerDocs(context);
final int maxChildren = nested.getNestedSort() != null ? nested.getNestedSort().getMaxChildren() : Integer.MAX_VALUE;
return localSortMode.select(distanceValues, Double.POSITIVE_INFINITY, rootDocs, innerDocs, context.reader().maxDoc(), maxChildren);
}
}
@Override
public FieldComparator<?> newComparator(String fieldname, int numHits, int sortPos, boolean reversed) {
return new DoubleComparator(numHits, null, null, reversed, sortPos) {
@Override
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
return new DoubleLeafComparator(context) {
@Override
protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
return getNumericDoubleValues(context).getRawDoubleValues();
}
};
}
};
}
@Override
public BucketedSort newBucketedSort(BigArrays bigArrays, SortOrder sortOrder, DocValueFormat format, int bucketSize, BucketedSort.ExtraData extra) {
return new BucketedSort.ForDoubles(bigArrays, sortOrder, format, bucketSize, extra) {
@Override
public Leaf forLeaf(LeafReaderContext ctx) throws IOException {
return new Leaf(ctx) {
private final NumericDoubleValues values = getNumericDoubleValues(ctx);
private double value;
@Override
protected boolean advanceExact(int doc) throws IOException {
if (values.advanceExact(doc)) {
value = values.doubleValue();
return true;
}
return false;
}
@Override
protected double docValue() {
return value;
}
};
}
};
}
};
}
Aggregations