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