Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.index.fielddata;

import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.search.DoubleValues;

import java.io.IOException;
import java.util.function.DoubleToLongFunction;

/**
* DoubleValues implementation that is guaranteed to have a value
* for every document in a reader
*/
public abstract class DenseDoubleValues extends DoubleValues {

@Override
public final boolean advanceExact(int doc) throws IOException {
doAdvanceExact(doc);
return true;
}

protected abstract void doAdvanceExact(int doc) throws IOException;

/**
* Represent a DenseDoubleValues as a NumericDocValues instance
* @param in the DenseDoubleValues to wrap
* @param maxDoc the maxDoc of the current reader
* @param converter a function to convert the double-valued output of DenseDoubleValues to a long
*/
public static NumericDocValues asNumericDocValues(DenseDoubleValues in, int maxDoc, DoubleToLongFunction converter) {
return new NumericDocValues() {

int doc = -1;

@Override
public long longValue() throws IOException {
return converter.applyAsLong(in.doubleValue());
}

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

@Override
public int docID() {
return doc;
}

@Override
public int nextDoc() throws IOException {
return advance(doc + 1);
}

@Override
public int advance(int target) throws IOException {
if (target >= maxDoc) {
return doc = NO_MORE_DOCS;
}
in.advanceExact(target);
return doc = target;
}

@Override
public long cost() {
return maxDoc;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.index.fielddata;

import org.apache.lucene.search.LongValues;

import java.io.IOException;

/**
* LongValues implementation that is guaranteed to have a value
* for every document in a reader
*/
public abstract class DenseLongValues extends LongValues {

@Override
public final boolean advanceExact(int doc) throws IOException {
doAdvanceExact(doc);
return true;
}

protected abstract void doAdvanceExact(int doc) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -614,19 +614,18 @@ public boolean advanceExact(int doc) throws IOException {
* document, returns the same value as {@code values} if there is a value
* for the current document and {@code missing} otherwise.
*/
public static LongValues replaceMissing(LongValues values, long missing) {
return new LongValues() {
public static DenseLongValues replaceMissing(LongValues values, long missing) {
return new DenseLongValues() {

private long value;

@Override
public boolean advanceExact(int target) throws IOException {
public void doAdvanceExact(int target) throws IOException {
if (values.advanceExact(target)) {
value = values.longValue();
} else {
value = missing;
}
return true;
}

@Override
Expand All @@ -641,19 +640,18 @@ public long longValue() {
* document, returns the same value as {@code values} if there is a value
* for the current document and {@code missing} otherwise.
*/
public static NumericDoubleValues replaceMissing(NumericDoubleValues values, double missing) {
return new NumericDoubleValues() {
public static DenseDoubleValues replaceMissing(NumericDoubleValues values, double missing) {
return new DenseDoubleValues() {

private double value;

@Override
public boolean advanceExact(int target) throws IOException {
public void doAdvanceExact(int target) throws IOException {
if (values.advanceExact(target)) {
value = values.doubleValue();
} else {
value = missing;
}
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.apache.lucene.util.BitSet;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.fielddata.DenseDoubleValues;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.fielddata.NumericDoubleValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.MultiValueMode;
Expand Down Expand Up @@ -59,7 +59,7 @@ protected SortedNumericDoubleValues getValues(LeafReaderContext context) throws
return indexFieldData.load(context).getDoubleValues();
}

private NumericDoubleValues getNumericDocValues(LeafReaderContext context, double missingValue) throws IOException {
private DenseDoubleValues getDenseDoubleValues(LeafReaderContext context, double missingValue) throws IOException {
final SortedNumericDoubleValues values = getValues(context);
if (nested == null) {
return FieldData.replaceMissing(sortMode.select(values), missingValue);
Expand All @@ -80,14 +80,17 @@ public FieldComparator<?> newComparator(String fieldname, int numHits, Pruning e
final double dMissingValue = (Double) missingObject(missingValue, reversed);
// NOTE: it's important to pass null as a missing value in the constructor so that
// the comparator doesn't check docsWithField since we replace missing values in select()
// TODO we can re-enable pruning here if we allow NumericDoubleValues to expose an iterator
return new DoubleComparator(numHits, fieldname, null, reversed, Pruning.NONE) {
return new DoubleComparator(numHits, fieldname, 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 DoubleValuesComparatorSource.this.getNumericDocValues(context, dMissingValue).getRawDoubleValues();
return DenseDoubleValues.asNumericDocValues(
getDenseDoubleValues(context, dMissingValue),
context.reader().maxDoc(),
Double::doubleToRawLongBits
);
}

@Override
Expand All @@ -113,7 +116,7 @@ public BucketedSort newBucketedSort(
@Override
public Leaf forLeaf(LeafReaderContext ctx) throws IOException {
return new Leaf(ctx) {
private final NumericDoubleValues docValues = getNumericDocValues(ctx, dMissingValue);
private final DenseDoubleValues docValues = getDenseDoubleValues(ctx, dMissingValue);
private double docValue;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import org.apache.lucene.util.BitSet;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.fielddata.DenseDoubleValues;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.fielddata.NumericDoubleValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.MultiValueMode;
Expand Down Expand Up @@ -54,7 +54,7 @@ public SortField.Type reducedType() {
return SortField.Type.FLOAT;
}

NumericDoubleValues getNumericDocValues(LeafReaderContext context, double missingValue) throws IOException {
DenseDoubleValues getDenseDoubleValues(LeafReaderContext context, double missingValue) throws IOException {
final SortedNumericDoubleValues values = indexFieldData.load(context).getDoubleValues();
if (nested == null) {
return FieldData.replaceMissing(sortMode.select(values), missingValue);
Expand All @@ -73,14 +73,17 @@ public FieldComparator<?> newComparator(String fieldname, int numHits, Pruning e
final float fMissingValue = (Float) missingObject(missingValue, reversed);
// NOTE: it's important to pass null as a missing value in the constructor so that
// the comparator doesn't check docsWithField since we replace missing values in select()
// TODO we can re-enable pruning here if we allow NumericDoubleValues to expose an iterator
return new FloatComparator(numHits, fieldname, null, reversed, Pruning.NONE) {
return new FloatComparator(numHits, fieldname, null, reversed, enableSkipping) {
@Override
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
return new FloatLeafComparator(context) {
@Override
protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
return FloatValuesComparatorSource.this.getNumericDocValues(context, fMissingValue).getRawFloatValues();
return DenseDoubleValues.asNumericDocValues(
getDenseDoubleValues(context, fMissingValue),
context.reader().maxDoc(),
v -> Float.floatToRawIntBits((float) v)
);
}
};
}
Expand All @@ -106,7 +109,7 @@ public boolean needsScores() {
@Override
public Leaf forLeaf(LeafReaderContext ctx) throws IOException {
return new Leaf(ctx) {
private final NumericDoubleValues docValues = getNumericDocValues(ctx, dMissingValue);
private final DenseDoubleValues docValues = getDenseDoubleValues(ctx, dMissingValue);
private float docValue;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.lucene.search.LeafFieldComparator;
import org.apache.lucene.search.Pruning;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.fielddata.DenseDoubleValues;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.search.MultiValueMode;

Expand All @@ -39,14 +40,17 @@ public FieldComparator<?> newComparator(String fieldname, int numHits, Pruning e
final float fMissingValue = (Float) missingObject(missingValue, reversed);
// NOTE: it's important to pass null as a missing value in the constructor so that
// the comparator doesn't check docsWithField since we replace missing values in select()
// TODO we can re-enable pruning here if we allow NumericDoubleValues to expose an iterator
return new HalfFloatComparator(numHits, fieldname, null, reversed, Pruning.NONE) {
return new HalfFloatComparator(numHits, fieldname, null, reversed, enableSkipping) {
@Override
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
return new HalfFloatLeafComparator(context) {
@Override
protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
return HalfFloatValuesComparatorSource.this.getNumericDocValues(context, fMissingValue).getRawFloatValues();
return DenseDoubleValues.asNumericDocValues(
getDenseDoubleValues(context, fMissingValue),
context.reader().maxDoc(),
v -> Float.floatToRawIntBits((float) v)
);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.fielddata.DenseLongValues;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
Expand Down Expand Up @@ -88,7 +89,7 @@ private SortedNumericLongValues loadDocValues(LeafReaderContext context) {
return converter != null ? converter.apply(values) : values;
}

LongValues getLongValues(LeafReaderContext context, long missingValue) throws IOException {
DenseLongValues getLongValues(LeafReaderContext context, long missingValue) throws IOException {
final SortedNumericLongValues values = loadDocValues(context);
if (nested == null) {
return FieldData.replaceMissing(sortMode.select(values), missingValue);
Expand Down Expand Up @@ -201,7 +202,7 @@ public Object missingObject(Object missingValue, boolean reversed) {
return super.missingObject(missingValue, reversed);
}

protected static NumericDocValues wrap(LongValues longValues, int maxDoc) {
protected static NumericDocValues wrap(DenseLongValues longValues, int maxDoc) {
return new NumericDocValues() {

int doc = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.elasticsearch.index.query.functionscore;

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.DoubleValues;
import org.apache.lucene.search.Explanation;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParsingException;
Expand All @@ -31,7 +32,6 @@
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.NumericDoubleValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.index.fielddata.SortingNumericDoubleValues;
import org.elasticsearch.index.mapper.DateFieldMapper;
Expand Down Expand Up @@ -391,7 +391,7 @@ public boolean needsScores() {
}

@Override
protected NumericDoubleValues distance(LeafReaderContext context) {
protected DoubleValues distance(LeafReaderContext context) {
final MultiGeoPointValues geoPointValues = fieldData.load(context).getPointValues();
return FieldData.replaceMissing(mode.select(new SortingNumericDoubleValues() {
@Override
Expand Down Expand Up @@ -484,7 +484,7 @@ public boolean needsScores() {
}

@Override
protected NumericDoubleValues distance(LeafReaderContext context) {
protected DoubleValues distance(LeafReaderContext context) {
final SortedNumericDoubleValues doubleValues = fieldData.load(context).getDoubleValues();
return FieldData.replaceMissing(mode.select(new SortingNumericDoubleValues() {
@Override
Expand Down Expand Up @@ -585,11 +585,11 @@ public AbstractDistanceScoreFunction(
* guaranteed that the value actually exists. If it does not, we assume
* the user handles this case in the query and return 0.
* */
protected abstract NumericDoubleValues distance(LeafReaderContext context);
protected abstract DoubleValues distance(LeafReaderContext context);

@Override
public final LeafScoreFunction getLeafScoreFunction(final LeafReaderContext ctx) {
final NumericDoubleValues distance = distance(ctx);
final DoubleValues distance = distance(ctx);
return new LeafScoreFunction() {

@Override
Expand Down
Loading