diff --git a/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java b/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java index 06a472b8ff1..5902edf020a 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java @@ -100,30 +100,47 @@ public class ByteArrayIndexFieldData extends AbstractIndexFieldData implements IndexNumericFieldData { +public class LongArrayIndexFieldData extends AbstractIndexFieldData implements IndexNumericFieldData { public static class Builder implements IndexFieldData.Builder { @@ -69,7 +70,7 @@ public class LongArrayIndexFieldData extends AbstractIndexFieldData max) { + max = value; + } + if (value < min) { + min = value; + } } Ordinals build = builder.build(fieldDataType.getSettings()); + + if (fieldDataType.getSettings().getAsBoolean("optimize_type", true)) { + // since the default mapping for numeric is long, its worth optimizing the actual type used to represent the data + if (min >= Byte.MIN_VALUE && max <= Byte.MAX_VALUE) { + return ByteArrayIndexFieldData.build(reader, builder, build, new ByteArrayIndexFieldData.BuilderBytes() { + @Override + public byte get(int index) { + return (byte) values.get(index); + } + + @Override + public byte[] toArray() { + byte[] bValues = new byte[values.size()]; + int i = 0; + for (TLongIterator it = values.iterator(); it.hasNext(); ) { + bValues[i++] = (byte) it.next(); + } + return bValues; + } + }); + } else if (min >= Short.MIN_VALUE && max <= Short.MAX_VALUE) { + return ShortArrayIndexFieldData.build(reader, builder, build, new ShortArrayIndexFieldData.BuilderShorts() { + @Override + public short get(int index) { + return (short) values.get(index); + } + + @Override + public short[] toArray() { + short[] sValues = new short[values.size()]; + int i = 0; + for (TLongIterator it = values.iterator(); it.hasNext(); ) { + sValues[i++] = (short) it.next(); + } + return sValues; + } + }); + } else if (min >= Integer.MIN_VALUE && max <= Integer.MAX_VALUE) { + return IntArrayIndexFieldData.build(reader, builder, build, new IntArrayIndexFieldData.BuilderIntegers() { + @Override + public int get(int index) { + return (int) values.get(index); + } + + @Override + public int[] toArray() { + int[] iValues = new int[values.size()]; + int i = 0; + for (TLongIterator it = values.iterator(); it.hasNext(); ) { + iValues[i++] = (int) it.next(); + } + return iValues; + } + }); + } + } + if (!build.isMultiValued()) { Docs ordinals = build.ordinals(); long[] sValues = new long[reader.maxDoc()]; @@ -114,10 +181,7 @@ public class LongArrayIndexFieldData extends AbstractIndexFieldData