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;
}
};
}
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;
}
}
};
}
}
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");
}
}
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);
}
}
};
}
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));
}
Aggregations