Search in sources :

Example 1 with DoubleComparator

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;
                        }
                    };
                }
            };
        }
    };
}
Also used : DocValueFormat(org.opensearch.search.DocValueFormat) BitSet(org.apache.lucene.util.BitSet) SortField(org.apache.lucene.search.SortField) DoubleComparator(org.apache.lucene.search.comparators.DoubleComparator) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) GeoPoint(org.opensearch.common.geo.GeoPoint) BigArrays(org.opensearch.common.util.BigArrays) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) MultiGeoPointValues(org.opensearch.index.fielddata.MultiGeoPointValues) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Aggregations

LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 SortField (org.apache.lucene.search.SortField)1 DoubleComparator (org.apache.lucene.search.comparators.DoubleComparator)1 BitSet (org.apache.lucene.util.BitSet)1 GeoPoint (org.opensearch.common.geo.GeoPoint)1 BigArrays (org.opensearch.common.util.BigArrays)1 MultiGeoPointValues (org.opensearch.index.fielddata.MultiGeoPointValues)1 NumericDoubleValues (org.opensearch.index.fielddata.NumericDoubleValues)1 SortedNumericDoubleValues (org.opensearch.index.fielddata.SortedNumericDoubleValues)1 DocValueFormat (org.opensearch.search.DocValueFormat)1