Search in sources :

Example 11 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 12 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)

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