Search in sources :

Example 1 with NumericDoubleValues

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

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)) {
                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) throws IOException {
            // loop over fields
            for (int i = 0; i < fieldVals.length; ++i) {
                final NumericDoubleValues doubleValues = values[i];
                if (doubleValues.advanceExact(doc)) {
                    final double value = doubleValues.doubleValue();
                    if (value == Double.NEGATIVE_INFINITY) {
                        // TODO: Fix matrix stats to treat neg inf as any other value
                        return false;
                    }
                    fieldVals[i] = value;
                } else {
                    return false;
                }
            }
            return true;
        }
    };
}
Also used : BigArrays(org.opensearch.common.util.BigArrays) LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues)

Example 2 with NumericDoubleValues

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

the class GeoUtils method distanceValues.

/**
 * Return a {@link SortedNumericDoubleValues} instance that returns the distances to a list of geo-points
 * for each document.
 */
public static SortedNumericDoubleValues distanceValues(final GeoDistance distance, final DistanceUnit unit, final MultiGeoPointValues geoPointValues, final GeoPoint... fromPoints) {
    final GeoPointValues singleValues = FieldData.unwrapSingleton(geoPointValues);
    if (singleValues != null && fromPoints.length == 1) {
        return FieldData.singleton(new NumericDoubleValues() {

            @Override
            public boolean advanceExact(int doc) throws IOException {
                return singleValues.advanceExact(doc);
            }

            @Override
            public double doubleValue() throws IOException {
                final GeoPoint from = fromPoints[0];
                final GeoPoint to = singleValues.geoPointValue();
                return distance.calculate(from.lat(), from.lon(), to.lat(), to.lon(), unit);
            }
        });
    } else {
        return new SortingNumericDoubleValues() {

            @Override
            public boolean advanceExact(int target) throws IOException {
                if (geoPointValues.advanceExact(target)) {
                    resize(geoPointValues.docValueCount() * fromPoints.length);
                    int v = 0;
                    for (int i = 0; i < geoPointValues.docValueCount(); ++i) {
                        final GeoPoint point = geoPointValues.nextValue();
                        for (GeoPoint from : fromPoints) {
                            values[v] = distance.calculate(from.lat(), from.lon(), point.lat(), point.lon(), unit);
                            v++;
                        }
                    }
                    sort();
                    return true;
                } else {
                    return false;
                }
            }
        };
    }
}
Also used : SortingNumericDoubleValues(org.opensearch.index.fielddata.SortingNumericDoubleValues) MultiGeoPointValues(org.opensearch.index.fielddata.MultiGeoPointValues) GeoPointValues(org.opensearch.index.fielddata.GeoPointValues) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) SortingNumericDoubleValues(org.opensearch.index.fielddata.SortingNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) IOException(java.io.IOException)

Example 3 with NumericDoubleValues

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

the class ScriptSortBuilder method fieldComparatorSource.

private IndexFieldData.XFieldComparatorSource fieldComparatorSource(QueryShardContext context) throws IOException {
    MultiValueMode valueMode = null;
    if (sortMode != null) {
        valueMode = MultiValueMode.fromString(sortMode.toString());
    }
    if (valueMode == null) {
        valueMode = order == SortOrder.DESC ? MultiValueMode.MAX : MultiValueMode.MIN;
    }
    final Nested nested;
    if (nestedSort != null) {
        // new nested sorts takes priority
        validateMaxChildrenExistOnlyInTopLevelNestedSort(context, nestedSort);
        nested = resolveNested(context, nestedSort);
    } else {
        nested = resolveNested(context, nestedPath, nestedFilter);
    }
    switch(type) {
        case STRING:
            final StringSortScript.Factory factory = context.compile(script, StringSortScript.CONTEXT);
            final StringSortScript.LeafFactory searchScript = factory.newFactory(script.getParams(), context.lookup());
            return new BytesRefFieldComparatorSource(null, null, valueMode, nested) {

                StringSortScript leafScript;

                @Override
                protected SortedBinaryDocValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = searchScript.newInstance(context);
                    final BinaryDocValues values = new AbstractBinaryDocValues() {

                        final BytesRefBuilder spare = new BytesRefBuilder();

                        @Override
                        public boolean advanceExact(int doc) throws IOException {
                            leafScript.setDocument(doc);
                            return true;
                        }

                        @Override
                        public BytesRef binaryValue() {
                            spare.copyChars(leafScript.execute());
                            return spare.get();
                        }
                    };
                    return FieldData.singleton(values);
                }

                @Override
                protected void setScorer(Scorable scorer) {
                    leafScript.setScorer(scorer);
                }

                @Override
                public BucketedSort newBucketedSort(BigArrays bigArrays, SortOrder sortOrder, DocValueFormat format, int bucketSize, BucketedSort.ExtraData extra) {
                    throw new IllegalArgumentException("error building sort for [_script]: " + "script sorting only supported on [numeric] scripts but was [" + type + "]");
                }
            };
        case NUMBER:
            final NumberSortScript.Factory numberSortFactory = context.compile(script, NumberSortScript.CONTEXT);
            final NumberSortScript.LeafFactory numberSortScript = numberSortFactory.newFactory(script.getParams(), context.lookup());
            return new DoubleValuesComparatorSource(null, Double.MAX_VALUE, valueMode, nested) {

                NumberSortScript leafScript;

                @Override
                protected SortedNumericDoubleValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = numberSortScript.newInstance(context);
                    final NumericDoubleValues values = new NumericDoubleValues() {

                        @Override
                        public boolean advanceExact(int doc) throws IOException {
                            leafScript.setDocument(doc);
                            return true;
                        }

                        @Override
                        public double doubleValue() {
                            return leafScript.execute();
                        }
                    };
                    return FieldData.singleton(values);
                }

                @Override
                protected void setScorer(Scorable scorer) {
                    leafScript.setScorer(scorer);
                }
            };
        default:
            throw new QueryShardException(context, "custom script sort type [" + type + "] not supported");
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DoubleValuesComparatorSource(org.opensearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource) Scorable(org.apache.lucene.search.Scorable) DocValueFormat(org.opensearch.search.DocValueFormat) Nested(org.opensearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) BytesRefFieldComparatorSource(org.opensearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource) StringSortScript(org.opensearch.script.StringSortScript) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) AbstractBinaryDocValues(org.opensearch.index.fielddata.AbstractBinaryDocValues) MultiValueMode(org.opensearch.search.MultiValueMode) AbstractBinaryDocValues(org.opensearch.index.fielddata.AbstractBinaryDocValues) SortedBinaryDocValues(org.opensearch.index.fielddata.SortedBinaryDocValues) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) NumberSortScript(org.opensearch.script.NumberSortScript) BigArrays(org.opensearch.common.util.BigArrays) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 4 with NumericDoubleValues

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

the class MinAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        if (parent == null) {
            return LeafBucketCollector.NO_OP_COLLECTOR;
        } else {
            // we have no parent and the values source is empty so we can skip collecting hits.
            throw new CollectionTerminatedException();
        }
    }
    if (pointConverter != null) {
        Number segMin = findLeafMinValue(ctx.reader(), pointField, pointConverter);
        if (segMin != null) {
            /*
                 * There is no parent aggregator (see {@link MinAggregator#getPointReaderOrNull}
                 * so the ordinal for the bucket is always 0.
                 */
            double min = mins.get(0);
            min = Math.min(min, segMin.doubleValue());
            mins.set(0, min);
            // the minimum value has been extracted, we don't need to collect hits on this segment.
            throw new CollectionTerminatedException();
        }
    }
    final BigArrays bigArrays = context.bigArrays();
    final SortedNumericDoubleValues allValues = valuesSource.doubleValues(ctx);
    final NumericDoubleValues values = MultiValueMode.MIN.select(allValues);
    return new LeafBucketCollectorBase(sub, allValues) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            if (bucket >= mins.size()) {
                long from = mins.size();
                mins = bigArrays.grow(mins, bucket + 1);
                mins.fill(from, mins.size(), Double.POSITIVE_INFINITY);
            }
            if (values.advanceExact(doc)) {
                final double value = values.doubleValue();
                double min = mins.get(bucket);
                min = Math.min(min, value);
                mins.set(bucket, min);
            }
        }
    };
}
Also used : BigArrays(org.opensearch.common.util.BigArrays) CollectionTerminatedException(org.apache.lucene.search.CollectionTerminatedException) LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues)

Example 5 with NumericDoubleValues

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

the class MultiValueModeTests method testSingleValuedDoubles.

public void testSingleValuedDoubles() 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 Supplier<SortedNumericDoubleValues> multiValues = () -> FieldData.singleton(new NumericDoubleValues() {

        int docID;

        @Override
        public boolean advanceExact(int doc) throws IOException {
            docID = doc;
            return docsWithValue == null || docsWithValue.get(doc);
        }

        @Override
        public double doubleValue() {
            return array[docID];
        }
    });
    verifySortedNumericDouble(multiValues, numDocs);
    final FixedBitSet rootDocs = randomRootDocs(numDocs);
    final FixedBitSet innerDocs = randomInnerDocs(rootDocs);
    verifySortedNumericDouble(multiValues, numDocs, rootDocs, innerDocs, Integer.MAX_VALUE);
    verifySortedNumericDouble(multiValues, numDocs, rootDocs, innerDocs, randomIntBetween(1, numDocs));
}
Also used : FixedBitSet(org.apache.lucene.util.FixedBitSet) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) IOException(java.io.IOException) 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