diff --git a/core/src/main/java/org/elasticsearch/index/fielddata/IndexNumericFieldData.java b/core/src/main/java/org/elasticsearch/index/fielddata/IndexNumericFieldData.java index 44d90d9dd13..2acea212c4c 100644 --- a/core/src/main/java/org/elasticsearch/index/fielddata/IndexNumericFieldData.java +++ b/core/src/main/java/org/elasticsearch/index/fielddata/IndexNumericFieldData.java @@ -19,193 +19,29 @@ package org.elasticsearch.index.fielddata; -import org.apache.lucene.index.TermsEnum; -import org.apache.lucene.search.SortField; -import org.apache.lucene.util.BytesRef; -import org.apache.lucene.util.BytesRefBuilder; -import org.apache.lucene.util.NumericUtils; -import org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder; -import org.elasticsearch.index.mapper.core.BooleanFieldMapper; - /** */ public interface IndexNumericFieldData extends IndexFieldData { public static enum NumericType { - BOOLEAN(1, false, SortField.Type.INT, 0, 1) { - @Override - public long toLong(BytesRef indexForm) { - if (indexForm.equals(BooleanFieldMapper.Values.FALSE)) { - return 0; - } else if (indexForm.equals(BooleanFieldMapper.Values.TRUE)) { - return 1; - } else { - throw new IllegalArgumentException("Cannot convert " + indexForm + " to a boolean"); - } - } + BOOLEAN(false), + BYTE(false), + SHORT(false), + INT(false), + LONG(false), + FLOAT(true), + DOUBLE(true); - @Override - public void toIndexForm(Number number, BytesRefBuilder bytes) { - bytes.append(number.intValue() != 0 ? BooleanFieldMapper.Values.TRUE : BooleanFieldMapper.Values.FALSE); - } - - @Override - public Number toNumber(BytesRef indexForm) { - return toLong(indexForm); - } - - }, - BYTE(8, false, SortField.Type.INT, Byte.MIN_VALUE, Byte.MAX_VALUE) { - @Override - public long toLong(BytesRef indexForm) { - return INT.toLong(indexForm); - } - - @Override - public void toIndexForm(Number number, BytesRefBuilder bytes) { - INT.toIndexForm(number, bytes); - } - - @Override - public Number toNumber(BytesRef indexForm) { - return INT.toNumber(indexForm); - } - }, - SHORT(16, false, SortField.Type.INT, Short.MIN_VALUE, Short.MAX_VALUE) { - @Override - public long toLong(BytesRef indexForm) { - return INT.toLong(indexForm); - } - - @Override - public void toIndexForm(Number number, BytesRefBuilder bytes) { - INT.toIndexForm(number, bytes); - } - - @Override - public Number toNumber(BytesRef indexForm) { - return INT.toNumber(indexForm); - } - }, - INT(32, false, SortField.Type.INT, Integer.MIN_VALUE, Integer.MAX_VALUE) { - @Override - public long toLong(BytesRef indexForm) { - return NumericUtils.prefixCodedToInt(indexForm); - } - - @Override - public void toIndexForm(Number number, BytesRefBuilder bytes) { - NumericUtils.intToPrefixCodedBytes(number.intValue(), 0, bytes); - } - - @Override - public Number toNumber(BytesRef indexForm) { - return NumericUtils.prefixCodedToInt(indexForm); - } - }, - LONG(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); - } - }, - FLOAT(32, true, SortField.Type.FLOAT, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY) { - @Override - public double toDouble(BytesRef indexForm) { - return NumericUtils.sortableIntToFloat(NumericUtils.prefixCodedToInt(indexForm)); - } - - @Override - public void toIndexForm(Number number, BytesRefBuilder bytes) { - NumericUtils.intToPrefixCodedBytes(NumericUtils.floatToSortableInt(number.floatValue()), 0, bytes); - } - - @Override - public Number toNumber(BytesRef indexForm) { - return NumericUtils.sortableIntToFloat(NumericUtils.prefixCodedToInt(indexForm)); - } - }, - DOUBLE(64, true, SortField.Type.DOUBLE, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY) { - @Override - public double toDouble(BytesRef indexForm) { - return NumericUtils.sortableLongToDouble(NumericUtils.prefixCodedToLong(indexForm)); - } - - @Override - public void toIndexForm(Number number, BytesRefBuilder bytes) { - NumericUtils.longToPrefixCodedBytes(NumericUtils.doubleToSortableLong(number.doubleValue()), 0, bytes); - } - - @Override - public Number toNumber(BytesRef indexForm) { - return NumericUtils.sortableLongToDouble(NumericUtils.prefixCodedToLong(indexForm)); - } - }; - - private final int requiredBits; private final boolean floatingPoint; - private final SortField.Type type; - private final Number minValue, maxValue; - private NumericType(int requiredBits, boolean floatingPoint, SortField.Type type, Number minValue, Number maxValue) { - this.requiredBits = requiredBits; + private NumericType(boolean floatingPoint) { this.floatingPoint = floatingPoint; - this.type = type; - this.minValue = minValue; - this.maxValue = maxValue; - } - - public final SortField.Type sortFieldType() { - return type; - } - - public final Number minValue() { - return minValue; - } - - public final Number maxValue() { - return maxValue; } public final boolean isFloatingPoint() { return floatingPoint; } - public final int requiredBits() { - return requiredBits; - } - - public abstract void toIndexForm(Number number, BytesRefBuilder bytes); - - public long toLong(BytesRef indexForm) { - return (long) toDouble(indexForm); - } - - public double toDouble(BytesRef indexForm) { - return (double) toLong(indexForm); - } - - public abstract Number toNumber(BytesRef indexForm); - - public final TermsEnum wrapTermsEnum(TermsEnum termsEnum) { - if (requiredBits() == 1) { // boolean, no prefix-terms - return termsEnum; - } else if (requiredBits() > 32) { - return OrdinalsBuilder.wrapNumeric64Bit(termsEnum); - } else { - return OrdinalsBuilder.wrapNumeric32Bit(termsEnum); - } - } } NumericType getNumericType();