Search in sources :

Example 1 with NumericDoubleValues

use of org.elasticsearch.index.fielddata.NumericDoubleValues in project elasticsearch by elastic.

the class MultiValueModeTests method testUnsortedSingleValuedDoubles.

public void testUnsortedSingleValuedDoubles() throws Exception {
    final int numDocs = scaledRandomIntBetween(1, 100);
    final double[] array = new double[numDocs];
    final FixedBitSet docsWithValue = randomBoolean() ? null : new FixedBitSet(numDocs);
    for (int i = 0; i < array.length; ++i) {
        if (randomBoolean()) {
            array[i] = randomDouble();
            if (docsWithValue != null) {
                docsWithValue.set(i);
            }
        } else if (docsWithValue != null && randomBoolean()) {
            docsWithValue.set(i);
        }
    }
    final NumericDoubleValues singleValues = new NumericDoubleValues() {

        @Override
        public double get(int docID) {
            return array[docID];
        }
    };
    final SortedNumericDoubleValues singletonValues = FieldData.singleton(singleValues, docsWithValue);
    final MultiValueMode.UnsortedNumericDoubleValues multiValues = new MultiValueMode.UnsortedNumericDoubleValues() {

        @Override
        public int count() {
            return singletonValues.count();
        }

        @Override
        public void setDocument(int doc) {
            singletonValues.setDocument(doc);
        }

        @Override
        public double valueAt(int index) {
            return Math.cos(singletonValues.valueAt(index));
        }
    };
    verify(multiValues, numDocs);
}
Also used : FixedBitSet(org.apache.lucene.util.FixedBitSet) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues)

Example 2 with NumericDoubleValues

use of org.elasticsearch.index.fielddata.NumericDoubleValues in project elasticsearch by elastic.

the class MultiValueModeTests method verify.

private void verify(SortedNumericDoubleValues values, int maxDoc, FixedBitSet rootDocs, FixedBitSet innerDocs) throws IOException {
    for (long missingValue : new long[] { 0, randomLong() }) {
        for (MultiValueMode mode : new MultiValueMode[] { MultiValueMode.MIN, MultiValueMode.MAX, MultiValueMode.SUM, MultiValueMode.AVG }) {
            final NumericDoubleValues selected = mode.select(values, missingValue, rootDocs, new BitSetIterator(innerDocs, 0L), maxDoc);
            int prevRoot = -1;
            for (int root = rootDocs.nextSetBit(0); root != -1; root = root + 1 < maxDoc ? rootDocs.nextSetBit(root + 1) : -1) {
                final double actual = selected.get(root);
                double expected = 0.0;
                if (mode == MultiValueMode.MAX) {
                    expected = Long.MIN_VALUE;
                } else if (mode == MultiValueMode.MIN) {
                    expected = Long.MAX_VALUE;
                }
                int numValues = 0;
                for (int child = innerDocs.nextSetBit(prevRoot + 1); child != -1 && child < root; child = innerDocs.nextSetBit(child + 1)) {
                    values.setDocument(child);
                    for (int j = 0; j < values.count(); ++j) {
                        if (mode == MultiValueMode.SUM || mode == MultiValueMode.AVG) {
                            expected += values.valueAt(j);
                        } else if (mode == MultiValueMode.MIN) {
                            expected = Math.min(expected, values.valueAt(j));
                        } else if (mode == MultiValueMode.MAX) {
                            expected = Math.max(expected, values.valueAt(j));
                        }
                        ++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) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues)

Example 3 with NumericDoubleValues

use of org.elasticsearch.index.fielddata.NumericDoubleValues in project elasticsearch by elastic.

the class MultiValueModeTests method verify.

private void verify(MultiValueMode.UnsortedNumericDoubleValues values, int maxDoc) {
    for (double missingValue : new double[] { 0, randomDouble() }) {
        for (MultiValueMode mode : new MultiValueMode[] { MultiValueMode.MIN, MultiValueMode.MAX, MultiValueMode.SUM, MultiValueMode.AVG }) {
            final NumericDoubleValues selected = mode.select(values, missingValue);
            for (int i = 0; i < maxDoc; ++i) {
                final double actual = selected.get(i);
                double expected = 0.0;
                values.setDocument(i);
                int numValues = values.count();
                if (numValues == 0) {
                    expected = missingValue;
                } else {
                    if (mode == MultiValueMode.MAX) {
                        expected = Long.MIN_VALUE;
                    } else if (mode == MultiValueMode.MIN) {
                        expected = Long.MAX_VALUE;
                    }
                    for (int j = 0; j < numValues; ++j) {
                        if (mode == MultiValueMode.SUM || mode == MultiValueMode.AVG) {
                            expected += values.valueAt(j);
                        } else if (mode == MultiValueMode.MIN) {
                            expected = Math.min(expected, values.valueAt(j));
                        } else if (mode == MultiValueMode.MAX) {
                            expected = Math.max(expected, values.valueAt(j));
                        }
                    }
                    if (mode == MultiValueMode.AVG) {
                        expected = expected / numValues;
                    }
                }
                assertEquals(mode.toString() + " docId=" + i, expected, actual, 0.1);
            }
        }
    }
}
Also used : NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues)

Example 4 with NumericDoubleValues

use of org.elasticsearch.index.fielddata.NumericDoubleValues in project elasticsearch by elastic.

the class MultiValueModeTests method verify.

private void verify(SortedNumericDoubleValues values, int maxDoc) {
    for (long missingValue : new long[] { 0, randomLong() }) {
        for (MultiValueMode mode : MultiValueMode.values()) {
            if (MultiValueMode.MEDIAN.equals(mode)) {
                continue;
            }
            final NumericDoubleValues selected = mode.select(values, missingValue);
            for (int i = 0; i < maxDoc; ++i) {
                final double actual = selected.get(i);
                double expected = 0.0;
                values.setDocument(i);
                int numValues = values.count();
                if (numValues == 0) {
                    expected = missingValue;
                } else {
                    if (mode == MultiValueMode.MAX) {
                        expected = Long.MIN_VALUE;
                    } else if (mode == MultiValueMode.MIN) {
                        expected = Long.MAX_VALUE;
                    }
                    for (int j = 0; j < numValues; ++j) {
                        if (mode == MultiValueMode.SUM || mode == MultiValueMode.AVG) {
                            expected += values.valueAt(j);
                        } else if (mode == MultiValueMode.MIN) {
                            expected = Math.min(expected, values.valueAt(j));
                        } else if (mode == MultiValueMode.MAX) {
                            expected = Math.max(expected, values.valueAt(j));
                        }
                    }
                    if (mode == MultiValueMode.AVG) {
                        expected = expected / numValues;
                    } else if (mode == MultiValueMode.MEDIAN) {
                        int value = numValues / 2;
                        if (numValues % 2 == 0) {
                            expected = (values.valueAt(value - 1) + values.valueAt(value)) / 2.0;
                        } else {
                            expected = values.valueAt(value);
                        }
                    }
                }
                assertEquals(mode.toString() + " docId=" + i, expected, actual, 0.1);
            }
        }
    }
}
Also used : NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues)

Example 5 with NumericDoubleValues

use of org.elasticsearch.index.fielddata.NumericDoubleValues in project elasticsearch by elastic.

the class MatrixStatsAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    if (valuesSources == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final BigArrays bigArrays = context.bigArrays();
    final NumericDoubleValues[] values = new NumericDoubleValues[valuesSources.fieldNames().length];
    for (int i = 0; i < values.length; ++i) {
        values[i] = valuesSources.getField(i, ctx);
    }
    return new LeafBucketCollectorBase(sub, values) {

        final String[] fieldNames = valuesSources.fieldNames();

        final double[] fieldVals = new double[fieldNames.length];

        @Override
        public void collect(int doc, long bucket) throws IOException {
            // get fields
            if (includeDocument(doc) == true) {
                stats = bigArrays.grow(stats, bucket + 1);
                RunningStats stat = stats.get(bucket);
                // add document fields to correlation stats
                if (stat == null) {
                    stat = new RunningStats(fieldNames, fieldVals);
                    stats.set(bucket, stat);
                } else {
                    stat.add(fieldNames, fieldVals);
                }
            }
        }

        /**
             * return a map of field names and data
             */
        private boolean includeDocument(int doc) {
            // loop over fields
            for (int i = 0; i < fieldVals.length; ++i) {
                final NumericDoubleValues doubleValues = values[i];
                final double value = doubleValues.get(doc);
                // skip if value is missing
                if (value == Double.NEGATIVE_INFINITY) {
                    return false;
                }
                fieldVals[i] = value;
            }
            return true;
        }
    };
}
Also used : BigArrays(org.elasticsearch.common.util.BigArrays) LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues)

Aggregations

NumericDoubleValues (org.elasticsearch.index.fielddata.NumericDoubleValues)16 SortedNumericDoubleValues (org.elasticsearch.index.fielddata.SortedNumericDoubleValues)12 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)4 DoubleDocValues (org.apache.lucene.queries.function.docvalues.DoubleDocValues)3 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)3 BitSet (org.apache.lucene.util.BitSet)3 BigArrays (org.elasticsearch.common.util.BigArrays)3 AtomicNumericFieldData (org.elasticsearch.index.fielddata.AtomicNumericFieldData)3 LeafBucketCollectorBase (org.elasticsearch.search.aggregations.LeafBucketCollectorBase)3 Scorer (org.apache.lucene.search.Scorer)2 SortField (org.apache.lucene.search.SortField)2 FixedBitSet (org.apache.lucene.util.FixedBitSet)2 IndexFieldData (org.elasticsearch.index.fielddata.IndexFieldData)2 Nested (org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested)2 MultiGeoPointValues (org.elasticsearch.index.fielddata.MultiGeoPointValues)2 MultiValueMode (org.elasticsearch.search.MultiValueMode)2 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 BitSetIterator (org.apache.lucene.util.BitSetIterator)1