Search in sources :

Example 6 with NumericDoubleValues

use of org.opensearch.index.fielddata.NumericDoubleValues 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, boolean enableSkipping, boolean reversed) {
            return new DoubleComparator(numHits, null, null, reversed, enableSkipping) {

                @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)

Example 7 with NumericDoubleValues

use of org.opensearch.index.fielddata.NumericDoubleValues in project OpenSearch by opensearch-project.

the class MultiValueModeTests method verifySortedNumericDouble.

private void verifySortedNumericDouble(Supplier<SortedNumericDoubleValues> supplier, int maxDoc) throws IOException {
    for (MultiValueMode mode : MultiValueMode.values()) {
        SortedNumericDoubleValues values = supplier.get();
        final NumericDoubleValues selected = mode.select(values);
        for (int i = 0; i < maxDoc; ++i) {
            Double actual = null;
            if (selected.advanceExact(i)) {
                actual = selected.doubleValue();
                verifyDoubleValueCanCalledMoreThanOnce(selected, actual);
            }
            Double expected = null;
            if (values.advanceExact(i)) {
                int numValues = values.docValueCount();
                if (mode == MultiValueMode.MAX) {
                    expected = Double.NEGATIVE_INFINITY;
                } else if (mode == MultiValueMode.MIN) {
                    expected = Double.POSITIVE_INFINITY;
                } else {
                    expected = 0d;
                }
                for (int j = 0; j < numValues; ++j) {
                    if (mode == MultiValueMode.SUM || mode == MultiValueMode.AVG) {
                        expected += values.nextValue();
                    } else if (mode == MultiValueMode.MIN) {
                        expected = Math.min(expected, values.nextValue());
                    } else if (mode == MultiValueMode.MAX) {
                        expected = Math.max(expected, values.nextValue());
                    }
                }
                if (mode == MultiValueMode.AVG) {
                    expected = expected / numValues;
                } else if (mode == MultiValueMode.MEDIAN) {
                    int value = numValues / 2;
                    if (numValues % 2 == 0) {
                        for (int j = 0; j < value - 1; ++j) {
                            values.nextValue();
                        }
                        expected = (values.nextValue() + values.nextValue()) / 2.0;
                    } else {
                        for (int j = 0; j < value; ++j) {
                            values.nextValue();
                        }
                        expected = values.nextValue();
                    }
                }
            }
            assertEquals(mode.toString() + " docId=" + i, expected, actual);
        }
    }
}
Also used : SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues)

Example 8 with NumericDoubleValues

use of org.opensearch.index.fielddata.NumericDoubleValues in project OpenSearch by opensearch-project.

the class MultiValueModeTests method verifySortedNumericDouble.

private void verifySortedNumericDouble(Supplier<SortedNumericDoubleValues> supplier, int maxDoc, FixedBitSet rootDocs, FixedBitSet innerDocs, int maxChildren) throws IOException {
    for (long missingValue : new long[] { 0, randomLong() }) {
        for (MultiValueMode mode : new MultiValueMode[] { MultiValueMode.MIN, MultiValueMode.MAX, MultiValueMode.SUM, MultiValueMode.AVG }) {
            SortedNumericDoubleValues values = supplier.get();
            final NumericDoubleValues selected = mode.select(values, missingValue, rootDocs, new BitSetIterator(innerDocs, 0L), maxDoc, maxChildren);
            int prevRoot = -1;
            for (int root = rootDocs.nextSetBit(0); root != -1; root = root + 1 < maxDoc ? rootDocs.nextSetBit(root + 1) : -1) {
                assertTrue(selected.advanceExact(root));
                final double actual = selected.doubleValue();
                verifyDoubleValueCanCalledMoreThanOnce(selected, actual);
                double expected = 0.0;
                if (mode == MultiValueMode.MAX) {
                    expected = Long.MIN_VALUE;
                } else if (mode == MultiValueMode.MIN) {
                    expected = Long.MAX_VALUE;
                }
                int numValues = 0;
                int count = 0;
                for (int child = innerDocs.nextSetBit(prevRoot + 1); child != -1 && child < root; child = innerDocs.nextSetBit(child + 1)) {
                    if (values.advanceExact(child)) {
                        if (++count > maxChildren) {
                            break;
                        }
                        for (int j = 0; j < values.docValueCount(); ++j) {
                            if (mode == MultiValueMode.SUM || mode == MultiValueMode.AVG) {
                                expected += values.nextValue();
                            } else if (mode == MultiValueMode.MIN) {
                                expected = Math.min(expected, values.nextValue());
                            } else if (mode == MultiValueMode.MAX) {
                                expected = Math.max(expected, values.nextValue());
                            }
                            ++numValues;
                        }
                    }
                }
                if (numValues == 0) {
                    expected = missingValue;
                } else if (mode == MultiValueMode.AVG) {
                    expected = expected / numValues;
                }
                assertEquals(mode.toString() + " docId=" + root, expected, actual, 0.1);
                prevRoot = root;
            }
        }
    }
}
Also used : BitSetIterator(org.apache.lucene.util.BitSetIterator) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues)

Example 9 with NumericDoubleValues

use of org.opensearch.index.fielddata.NumericDoubleValues in project OpenSearch by opensearch-project.

the class FieldDataValueSource method getValues.

@Override
public DoubleValues getValues(LeafReaderContext leaf, DoubleValues scores) {
    LeafNumericFieldData leafData = (LeafNumericFieldData) fieldData.load(leaf);
    NumericDoubleValues docValues = multiValueMode.select(leafData.getDoubleValues());
    return new DoubleValues() {

        @Override
        public double doubleValue() throws IOException {
            return docValues.doubleValue();
        }

        @Override
        public boolean advanceExact(int doc) throws IOException {
            return docValues.advanceExact(doc);
        }
    };
}
Also used : LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) DoubleValues(org.apache.lucene.search.DoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues)

Example 10 with NumericDoubleValues

use of org.opensearch.index.fielddata.NumericDoubleValues in project OpenSearch by opensearch-project.

the class DateMethodValueSource method getValues.

@Override
public DoubleValues getValues(LeafReaderContext leaf, DoubleValues scores) {
    LeafNumericFieldData leafData = (LeafNumericFieldData) fieldData.load(leaf);
    final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT);
    NumericDoubleValues docValues = multiValueMode.select(leafData.getDoubleValues());
    return new DoubleValues() {

        @Override
        public double doubleValue() throws IOException {
            calendar.setTimeInMillis((long) docValues.doubleValue());
            return calendar.get(calendarType);
        }

        @Override
        public boolean advanceExact(int doc) throws IOException {
            return docValues.advanceExact(doc);
        }
    };
}
Also used : LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) Calendar(java.util.Calendar) DoubleValues(org.apache.lucene.search.DoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues)

Aggregations

NumericDoubleValues (org.opensearch.index.fielddata.NumericDoubleValues)12 SortedNumericDoubleValues (org.opensearch.index.fielddata.SortedNumericDoubleValues)8 BigArrays (org.opensearch.common.util.BigArrays)5 DoubleValues (org.apache.lucene.search.DoubleValues)3 LeafNumericFieldData (org.opensearch.index.fielddata.LeafNumericFieldData)3 LeafBucketCollectorBase (org.opensearch.search.aggregations.LeafBucketCollectorBase)3 IOException (java.io.IOException)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 CollectionTerminatedException (org.apache.lucene.search.CollectionTerminatedException)2 MultiGeoPointValues (org.opensearch.index.fielddata.MultiGeoPointValues)2 DocValueFormat (org.opensearch.search.DocValueFormat)2 Calendar (java.util.Calendar)1 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 Scorable (org.apache.lucene.search.Scorable)1 SortField (org.apache.lucene.search.SortField)1 DoubleComparator (org.apache.lucene.search.comparators.DoubleComparator)1 BitSet (org.apache.lucene.util.BitSet)1 BitSetIterator (org.apache.lucene.util.BitSetIterator)1 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)1