Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a fixed-point mapping type #15939

Closed
wants to merge 4 commits into from
Closed
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
Expand Up @@ -89,6 +89,7 @@ public class IndexFieldDataService extends AbstractIndexComponent implements Clo
buildersByTypeBuilder.put(IndexFieldMapper.NAME, new IndexIndexFieldData.Builder());
buildersByTypeBuilder.put("binary", new DisabledIndexFieldData.Builder());
buildersByTypeBuilder.put(BooleanFieldMapper.CONTENT_TYPE, MISSING_DOC_VALUES_BUILDER);
buildersByTypeBuilder.put("fixed", MISSING_DOC_VALUES_BUILDER);
buildersByType = unmodifiableMap(buildersByTypeBuilder);


Expand All @@ -103,6 +104,7 @@ public class IndexFieldDataService extends AbstractIndexComponent implements Clo
.put("geo_point", new AbstractGeoPointDVIndexFieldData.Builder())
.put("binary", new BytesBinaryDVIndexFieldData.Builder())
.put(BooleanFieldMapper.CONTENT_TYPE, new DocValuesIndexFieldData.Builder().numericType(IndexNumericFieldData.NumericType.BOOLEAN))
.put("fixed", new DocValuesIndexFieldData.Builder().numericType(IndexNumericFieldData.NumericType.FIXED))
.immutableMap();

buildersByTypeAndFormat = MapBuilder.<Tuple<String, String>, IndexFieldData.Builder>newMapBuilder()
Expand Down Expand Up @@ -138,6 +140,9 @@ public class IndexFieldDataService extends AbstractIndexComponent implements Clo
.put(Tuple.tuple(BooleanFieldMapper.CONTENT_TYPE, DOC_VALUES_FORMAT), new DocValuesIndexFieldData.Builder().numericType(IndexNumericFieldData.NumericType.BOOLEAN))
.put(Tuple.tuple(BooleanFieldMapper.CONTENT_TYPE, DISABLED_FORMAT), new DisabledIndexFieldData.Builder())

.put(Tuple.tuple("fixed", DOC_VALUES_FORMAT), new DocValuesIndexFieldData.Builder().numericType(IndexNumericFieldData.NumericType.FIXED))
.put(Tuple.tuple("fixed", DISABLED_FORMAT), new DisabledIndexFieldData.Builder())

.immutableMap();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ public void toIndexForm(Number number, BytesRefBuilder bytes) {
public Number toNumber(BytesRef indexForm) {
return NumericUtils.sortableLongToDouble(NumericUtils.prefixCodedToLong(indexForm));
}
},
FIXED(64, false, SortField.Type.LONG, Long.MIN_VALUE, Long.MAX_VALUE) {
@Override
public long toLong(BytesRef indexForm) {
return NumericUtils.prefixCodedToLong(indexForm);
}

@Override
public void toIndexForm(Number number, BytesRefBuilder bytes) {
NumericUtils.longToPrefixCodedBytes(number.longValue(), 0, bytes);
}

@Override
public Number toNumber(BytesRef indexForm) {
return NumericUtils.prefixCodedToLong(indexForm);
}
};

private final int requiredBits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public IndexFieldData<?> build(IndexSettings indexSettings, MappedFieldType fiel
assert numericType == null;
return new BinaryDVIndexFieldData(indexSettings.getIndex(), fieldName, fieldType.fieldDataType());
} else if (numericType != null) {
return new SortedNumericDVIndexFieldData(indexSettings.getIndex(), fieldName, numericType, fieldType.fieldDataType());
return new SortedNumericDVIndexFieldData(indexSettings.getIndex(), fieldName, numericType, fieldType);
} else {
return new SortedSetDVOrdinalsIndexFieldData(indexSettings, cache, fieldName, breakerService, fieldType.fieldDataType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.elasticsearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource;
import org.elasticsearch.index.fielddata.fieldcomparator.FloatValuesComparatorSource;
import org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.fixed.FixedPointFieldMapper;
import org.elasticsearch.search.MultiValueMode;

import java.io.IOException;
Expand All @@ -50,13 +52,15 @@
*/
public class SortedNumericDVIndexFieldData extends DocValuesIndexFieldData implements IndexNumericFieldData {
private final NumericType numericType;
private final MappedFieldType fieldType;

public SortedNumericDVIndexFieldData(Index index, String fieldNames, NumericType numericType, FieldDataType fieldDataType) {
super(index, fieldNames, fieldDataType);
public SortedNumericDVIndexFieldData(Index index, String fieldNames, NumericType numericType, MappedFieldType fieldType) {
super(index, fieldNames, fieldType.fieldDataType());
if (numericType == null) {
throw new IllegalArgumentException("numericType must be non-null");
}
this.numericType = numericType;
this.fieldType = fieldType;
}

@Override
Expand All @@ -66,6 +70,8 @@ public org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource c
return new FloatValuesComparatorSource(this, missingValue, sortMode, nested);
case DOUBLE:
return new DoubleValuesComparatorSource(this, missingValue, sortMode, nested);
case FIXED:
return new LongValuesComparatorSource(this, missingValue, sortMode, nested);
default:
assert !numericType.isFloatingPoint();
return new LongValuesComparatorSource(this, missingValue, sortMode, nested);
Expand All @@ -92,6 +98,9 @@ public AtomicNumericFieldData load(LeafReaderContext context) {
return new SortedNumericFloatFieldData(reader, field);
case DOUBLE:
return new SortedNumericDoubleFieldData(reader, field);
case FIXED:
long decimalFactor = ((FixedPointFieldMapper.FixedPointFieldType)fieldType).getDecimalFactor();
return new SortedNumericFixedPointFieldData(reader, field, decimalFactor);
default:
return new SortedNumericLongFieldData(reader, field);
}
Expand Down Expand Up @@ -264,4 +273,61 @@ public Collection<Accountable> getChildResources() {
return Collections.emptyList();
}
}

static final class SortedNumericFixedPointFieldData extends AtomicDoubleFieldData {
final LeafReader reader;
final String field;
final long decimalFactor;

SortedNumericFixedPointFieldData(LeafReader reader, String field, long decimalFactor) {
super(0L);
this.reader = reader;
this.field = field;
this.decimalFactor = decimalFactor;
}

@Override
public SortedNumericDoubleValues getDoubleValues() {
try {
SortedNumericDocValues raw = DocValues.getSortedNumeric(reader, field);
return new SortedNumericFixedPointDocValues(raw, decimalFactor);
} catch (IOException e) {
throw new IllegalStateException("Cannot load doc values", e);
}
}

@Override
public Collection<Accountable> getChildResources() {
return Collections.emptyList();
}

/**
* Thin Wrapper class which converts the long into a double based on the scaling factor
*/
public static class SortedNumericFixedPointDocValues extends SortedNumericDoubleValues {
private final long decimalFactor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scalingFactor?

private final SortedNumericDocValues dv;

protected SortedNumericFixedPointDocValues(SortedNumericDocValues dv, long decimalFactor) {
super();
this.decimalFactor = decimalFactor;
this.dv = dv;
}

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

@Override
public double valueAt(int index) {
return ((double)dv.valueAt(index)) / decimalFactor;
}

@Override
public int count() {
return dv.count();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.index.mapper.core.ShortFieldMapper;
import org.elasticsearch.index.mapper.core.StringFieldMapper;
import org.elasticsearch.index.mapper.core.TokenCountFieldMapper;
import org.elasticsearch.index.mapper.fixed.FixedPointFieldMapper;
import org.elasticsearch.index.mapper.geo.GeoShapeFieldMapper;
import org.elasticsearch.index.mapper.ip.IpFieldMapper;
import org.elasticsearch.index.mapper.object.ObjectMapper;
Expand Down Expand Up @@ -107,4 +108,8 @@ public static GeoShapeFieldMapper.Builder geoShapeField(String name) {
public static CompletionFieldMapper.Builder completionField(String name) {
return new CompletionFieldMapper.Builder(name);
}

public static FixedPointFieldMapper.Builder fixedPointField(String name) {
return new FixedPointFieldMapper.Builder(name);
}
}
Loading