diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ByteValuesComparator.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ByteValuesComparator.java new file mode 100644 index 00000000000..a96d678ac89 --- /dev/null +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ByteValuesComparator.java @@ -0,0 +1,105 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.fielddata.fieldcomparator; + +import org.apache.lucene.index.AtomicReaderContext; +import org.apache.lucene.search.FieldComparator; +import org.elasticsearch.index.fielddata.ByteValues; +import org.elasticsearch.index.fielddata.IndexNumericFieldData; + +import java.io.IOException; + +/** + */ +public class ByteValuesComparator extends FieldComparator { + + private final IndexNumericFieldData indexFieldData; + private final byte missingValue; + + protected final byte[] values; + private byte bottom; + private ByteValues readerValues; + + public ByteValuesComparator(IndexNumericFieldData indexFieldData, byte missingValue, int numHits) { + this.indexFieldData = indexFieldData; + this.missingValue = missingValue; + this.values = new byte[numHits]; + } + + @Override + public int compare(int slot1, int slot2) { + final byte v1 = values[slot1]; + final byte v2 = values[slot2]; + if (v1 > v2) { + return 1; + } else if (v1 < v2) { + return -1; + } else { + return 0; + } + } + + @Override + public void setBottom(int slot) { + this.bottom = values[slot]; + } + + @Override + public int compareBottom(int doc) throws IOException { + byte v2 = readerValues.getValueMissing(doc, missingValue); + + if (bottom > v2) { + return 1; + } else if (bottom < v2) { + return -1; + } else { + return 0; + } + } + + @Override + public void copy(int slot, int doc) throws IOException { + values[slot] = readerValues.getValueMissing(doc, missingValue); + } + + @Override + public FieldComparator setNextReader(AtomicReaderContext context) throws IOException { + this.readerValues = indexFieldData.load(context).getByteValues(); + return this; + } + + @Override + public Byte value(int slot) { + return Byte.valueOf(values[slot]); + } + + @Override + public int compareDocToValue(int doc, Byte valueObj) throws IOException { + final byte value = valueObj.byteValue(); + byte docValue = readerValues.getValueMissing(doc, missingValue); + if (docValue < value) { + return -1; + } else if (docValue > value) { + return 1; + } else { + return 0; + } + } +} diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ByteValuesComparatorSource.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ByteValuesComparatorSource.java new file mode 100644 index 00000000000..c4a81be3c8d --- /dev/null +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ByteValuesComparatorSource.java @@ -0,0 +1,62 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.fielddata.fieldcomparator; + +import org.apache.lucene.search.FieldComparator; +import org.apache.lucene.search.SortField; +import org.elasticsearch.common.Nullable; +import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.IndexNumericFieldData; + +import java.io.IOException; + +/** + */ +public class ByteValuesComparatorSource extends IndexFieldData.XFieldComparatorSource { + + private final IndexNumericFieldData indexFieldData; + private final Object missingValue; + + public ByteValuesComparatorSource(IndexNumericFieldData indexFieldData, @Nullable Object missingValue) { + this.indexFieldData = indexFieldData; + this.missingValue = missingValue; + } + + @Override + public SortField.Type reducedType() { + return SortField.Type.INT; + } + + @Override + public FieldComparator newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { + assert fieldname.equals(indexFieldData.getFieldNames().indexName()); + + byte dMissingValue; + if (missingValue == null || "_last".equals(missingValue)) { + dMissingValue = reversed ? Byte.MIN_VALUE : Byte.MAX_VALUE; + } else if ("_first".equals(missingValue)) { + dMissingValue = reversed ? Byte.MAX_VALUE : Byte.MIN_VALUE; + } else { + dMissingValue = missingValue instanceof Number ? ((Number) missingValue).byteValue() : Byte.parseByte(missingValue.toString()); + } + + return new ByteValuesComparator(indexFieldData, dMissingValue, numHits); + } +} diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/FloatValuesComparator.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/FloatValuesComparator.java new file mode 100644 index 00000000000..f63a6497267 --- /dev/null +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/FloatValuesComparator.java @@ -0,0 +1,105 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.fielddata.fieldcomparator; + +import org.apache.lucene.index.AtomicReaderContext; +import org.apache.lucene.search.FieldComparator; +import org.elasticsearch.index.fielddata.FloatValues; +import org.elasticsearch.index.fielddata.IndexNumericFieldData; + +import java.io.IOException; + +/** + */ +public class FloatValuesComparator extends FieldComparator { + + private final IndexNumericFieldData indexFieldData; + private final float missingValue; + + protected final float[] values; + private float bottom; + private FloatValues readerValues; + + public FloatValuesComparator(IndexNumericFieldData indexFieldData, float missingValue, int numHits) { + this.indexFieldData = indexFieldData; + this.missingValue = missingValue; + this.values = new float[numHits]; + } + + @Override + public int compare(int slot1, int slot2) { + final float v1 = values[slot1]; + final float v2 = values[slot2]; + if (v1 > v2) { + return 1; + } else if (v1 < v2) { + return -1; + } else { + return 0; + } + } + + @Override + public void setBottom(int slot) { + this.bottom = values[slot]; + } + + @Override + public int compareBottom(int doc) throws IOException { + float v2 = readerValues.getValueMissing(doc, missingValue); + + if (bottom > v2) { + return 1; + } else if (bottom < v2) { + return -1; + } else { + return 0; + } + } + + @Override + public void copy(int slot, int doc) throws IOException { + values[slot] = readerValues.getValueMissing(doc, missingValue); + } + + @Override + public FieldComparator setNextReader(AtomicReaderContext context) throws IOException { + this.readerValues = indexFieldData.load(context).getFloatValues(); + return this; + } + + @Override + public Float value(int slot) { + return Float.valueOf(values[slot]); + } + + @Override + public int compareDocToValue(int doc, Float valueObj) throws IOException { + final float value = valueObj.floatValue(); + float docValue = readerValues.getValueMissing(doc, missingValue); + if (docValue < value) { + return -1; + } else if (docValue > value) { + return 1; + } else { + return 0; + } + } +} diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/FloatValuesComparatorSource.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/FloatValuesComparatorSource.java new file mode 100644 index 00000000000..50949de6ab2 --- /dev/null +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/FloatValuesComparatorSource.java @@ -0,0 +1,62 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.fielddata.fieldcomparator; + +import org.apache.lucene.search.FieldComparator; +import org.apache.lucene.search.SortField; +import org.elasticsearch.common.Nullable; +import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.IndexNumericFieldData; + +import java.io.IOException; + +/** + */ +public class FloatValuesComparatorSource extends IndexFieldData.XFieldComparatorSource { + + private final IndexNumericFieldData indexFieldData; + private final Object missingValue; + + public FloatValuesComparatorSource(IndexNumericFieldData indexFieldData, @Nullable Object missingValue) { + this.indexFieldData = indexFieldData; + this.missingValue = missingValue; + } + + @Override + public SortField.Type reducedType() { + return SortField.Type.FLOAT; + } + + @Override + public FieldComparator newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { + assert fieldname.equals(indexFieldData.getFieldNames().indexName()); + + float dMissingValue; + if (missingValue == null || "_last".equals(missingValue)) { + dMissingValue = reversed ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY; + } else if ("_first".equals(missingValue)) { + dMissingValue = reversed ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY; + } else { + dMissingValue = missingValue instanceof Number ? ((Number) missingValue).floatValue() : Float.parseFloat(missingValue.toString()); + } + + return new FloatValuesComparator(indexFieldData, dMissingValue, numHits); + } +} diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/IntValuesComparator.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/IntValuesComparator.java new file mode 100644 index 00000000000..064ad2888b7 --- /dev/null +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/IntValuesComparator.java @@ -0,0 +1,105 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.fielddata.fieldcomparator; + +import org.apache.lucene.index.AtomicReaderContext; +import org.apache.lucene.search.FieldComparator; +import org.elasticsearch.index.fielddata.IndexNumericFieldData; +import org.elasticsearch.index.fielddata.IntValues; + +import java.io.IOException; + +/** + */ +public class IntValuesComparator extends FieldComparator { + + private final IndexNumericFieldData indexFieldData; + private final int missingValue; + + protected final int[] values; + private int bottom; + private IntValues readerValues; + + public IntValuesComparator(IndexNumericFieldData indexFieldData, int missingValue, int numHits) { + this.indexFieldData = indexFieldData; + this.missingValue = missingValue; + this.values = new int[numHits]; + } + + @Override + public int compare(int slot1, int slot2) { + final int v1 = values[slot1]; + final int v2 = values[slot2]; + if (v1 > v2) { + return 1; + } else if (v1 < v2) { + return -1; + } else { + return 0; + } + } + + @Override + public void setBottom(int slot) { + this.bottom = values[slot]; + } + + @Override + public int compareBottom(int doc) throws IOException { + int v2 = readerValues.getValueMissing(doc, missingValue); + + if (bottom > v2) { + return 1; + } else if (bottom < v2) { + return -1; + } else { + return 0; + } + } + + @Override + public void copy(int slot, int doc) throws IOException { + values[slot] = readerValues.getValueMissing(doc, missingValue); + } + + @Override + public FieldComparator setNextReader(AtomicReaderContext context) throws IOException { + this.readerValues = indexFieldData.load(context).getIntValues(); + return this; + } + + @Override + public Integer value(int slot) { + return Integer.valueOf(values[slot]); + } + + @Override + public int compareDocToValue(int doc, Integer valueObj) throws IOException { + final int value = valueObj.intValue(); + int docValue = readerValues.getValueMissing(doc, missingValue); + if (docValue < value) { + return -1; + } else if (docValue > value) { + return 1; + } else { + return 0; + } + } +} diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/IntValuesComparatorSource.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/IntValuesComparatorSource.java new file mode 100644 index 00000000000..bebbd479adf --- /dev/null +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/IntValuesComparatorSource.java @@ -0,0 +1,62 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.fielddata.fieldcomparator; + +import org.apache.lucene.search.FieldComparator; +import org.apache.lucene.search.SortField; +import org.elasticsearch.common.Nullable; +import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.IndexNumericFieldData; + +import java.io.IOException; + +/** + */ +public class IntValuesComparatorSource extends IndexFieldData.XFieldComparatorSource { + + private final IndexNumericFieldData indexFieldData; + private final Object missingValue; + + public IntValuesComparatorSource(IndexNumericFieldData indexFieldData, @Nullable Object missingValue) { + this.indexFieldData = indexFieldData; + this.missingValue = missingValue; + } + + @Override + public SortField.Type reducedType() { + return SortField.Type.INT; + } + + @Override + public FieldComparator newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { + assert fieldname.equals(indexFieldData.getFieldNames().indexName()); + + int dMissingValue; + if (missingValue == null || "_last".equals(missingValue)) { + dMissingValue = reversed ? Integer.MIN_VALUE : Integer.MAX_VALUE; + } else if ("_first".equals(missingValue)) { + dMissingValue = reversed ? Integer.MAX_VALUE : Integer.MIN_VALUE; + } else { + dMissingValue = missingValue instanceof Number ? ((Number) missingValue).intValue() : Integer.parseInt(missingValue.toString()); + } + + return new IntValuesComparator(indexFieldData, dMissingValue, numHits); + } +} diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/LongValuesComparator.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/LongValuesComparator.java new file mode 100644 index 00000000000..abef68b1a07 --- /dev/null +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/LongValuesComparator.java @@ -0,0 +1,105 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.fielddata.fieldcomparator; + +import org.apache.lucene.index.AtomicReaderContext; +import org.apache.lucene.search.FieldComparator; +import org.elasticsearch.index.fielddata.IndexNumericFieldData; +import org.elasticsearch.index.fielddata.LongValues; + +import java.io.IOException; + +/** + */ +public class LongValuesComparator extends FieldComparator { + + private final IndexNumericFieldData indexFieldData; + private final long missingValue; + + protected final long[] values; + private long bottom; + private LongValues readerValues; + + public LongValuesComparator(IndexNumericFieldData indexFieldData, long missingValue, int numHits) { + this.indexFieldData = indexFieldData; + this.missingValue = missingValue; + this.values = new long[numHits]; + } + + @Override + public int compare(int slot1, int slot2) { + final long v1 = values[slot1]; + final long v2 = values[slot2]; + if (v1 > v2) { + return 1; + } else if (v1 < v2) { + return -1; + } else { + return 0; + } + } + + @Override + public void setBottom(int slot) { + this.bottom = values[slot]; + } + + @Override + public int compareBottom(int doc) throws IOException { + long v2 = readerValues.getValueMissing(doc, missingValue); + + if (bottom > v2) { + return 1; + } else if (bottom < v2) { + return -1; + } else { + return 0; + } + } + + @Override + public void copy(int slot, int doc) throws IOException { + values[slot] = readerValues.getValueMissing(doc, missingValue); + } + + @Override + public FieldComparator setNextReader(AtomicReaderContext context) throws IOException { + this.readerValues = indexFieldData.load(context).getLongValues(); + return this; + } + + @Override + public Long value(int slot) { + return Long.valueOf(values[slot]); + } + + @Override + public int compareDocToValue(int doc, Long valueObj) throws IOException { + final long value = valueObj.longValue(); + long docValue = readerValues.getValueMissing(doc, missingValue); + if (docValue < value) { + return -1; + } else if (docValue > value) { + return 1; + } else { + return 0; + } + } +} diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/LongValuesComparatorSource.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/LongValuesComparatorSource.java new file mode 100644 index 00000000000..05878f3b0d6 --- /dev/null +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/LongValuesComparatorSource.java @@ -0,0 +1,62 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.fielddata.fieldcomparator; + +import org.apache.lucene.search.FieldComparator; +import org.apache.lucene.search.SortField; +import org.elasticsearch.common.Nullable; +import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.IndexNumericFieldData; + +import java.io.IOException; + +/** + */ +public class LongValuesComparatorSource extends IndexFieldData.XFieldComparatorSource { + + private final IndexNumericFieldData indexFieldData; + private final Object missingValue; + + public LongValuesComparatorSource(IndexNumericFieldData indexFieldData, @Nullable Object missingValue) { + this.indexFieldData = indexFieldData; + this.missingValue = missingValue; + } + + @Override + public SortField.Type reducedType() { + return SortField.Type.LONG; + } + + @Override + public FieldComparator newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { + assert fieldname.equals(indexFieldData.getFieldNames().indexName()); + + long dMissingValue; + if (missingValue == null || "_last".equals(missingValue)) { + dMissingValue = reversed ? Long.MIN_VALUE : Long.MAX_VALUE; + } else if ("_first".equals(missingValue)) { + dMissingValue = reversed ? Long.MAX_VALUE : Long.MIN_VALUE; + } else { + dMissingValue = missingValue instanceof Number ? ((Number) missingValue).longValue() : Long.parseLong(missingValue.toString()); + } + + return new LongValuesComparator(indexFieldData, dMissingValue, numHits); + } +} diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ShortValuesComparator.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ShortValuesComparator.java new file mode 100644 index 00000000000..4b85071f5a4 --- /dev/null +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ShortValuesComparator.java @@ -0,0 +1,105 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.fielddata.fieldcomparator; + +import org.apache.lucene.index.AtomicReaderContext; +import org.apache.lucene.search.FieldComparator; +import org.elasticsearch.index.fielddata.IndexNumericFieldData; +import org.elasticsearch.index.fielddata.ShortValues; + +import java.io.IOException; + +/** + */ +public class ShortValuesComparator extends FieldComparator { + + private final IndexNumericFieldData indexFieldData; + private final short missingValue; + + protected final short[] values; + private short bottom; + private ShortValues readerValues; + + public ShortValuesComparator(IndexNumericFieldData indexFieldData, short missingValue, int numHits) { + this.indexFieldData = indexFieldData; + this.missingValue = missingValue; + this.values = new short[numHits]; + } + + @Override + public int compare(int slot1, int slot2) { + final short v1 = values[slot1]; + final short v2 = values[slot2]; + if (v1 > v2) { + return 1; + } else if (v1 < v2) { + return -1; + } else { + return 0; + } + } + + @Override + public void setBottom(int slot) { + this.bottom = values[slot]; + } + + @Override + public int compareBottom(int doc) throws IOException { + short v2 = readerValues.getValueMissing(doc, missingValue); + + if (bottom > v2) { + return 1; + } else if (bottom < v2) { + return -1; + } else { + return 0; + } + } + + @Override + public void copy(int slot, int doc) throws IOException { + values[slot] = readerValues.getValueMissing(doc, missingValue); + } + + @Override + public FieldComparator setNextReader(AtomicReaderContext context) throws IOException { + this.readerValues = indexFieldData.load(context).getShortValues(); + return this; + } + + @Override + public Short value(int slot) { + return Short.valueOf(values[slot]); + } + + @Override + public int compareDocToValue(int doc, Short valueObj) throws IOException { + final short value = valueObj.shortValue(); + short docValue = readerValues.getValueMissing(doc, missingValue); + if (docValue < value) { + return -1; + } else if (docValue > value) { + return 1; + } else { + return 0; + } + } +} diff --git a/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ShortValuesComparatorSource.java b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ShortValuesComparatorSource.java new file mode 100644 index 00000000000..0b7a883b251 --- /dev/null +++ b/src/main/java/org/elasticsearch/index/fielddata/fieldcomparator/ShortValuesComparatorSource.java @@ -0,0 +1,62 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.fielddata.fieldcomparator; + +import org.apache.lucene.search.FieldComparator; +import org.apache.lucene.search.SortField; +import org.elasticsearch.common.Nullable; +import org.elasticsearch.index.fielddata.IndexFieldData; +import org.elasticsearch.index.fielddata.IndexNumericFieldData; + +import java.io.IOException; + +/** + */ +public class ShortValuesComparatorSource extends IndexFieldData.XFieldComparatorSource { + + private final IndexNumericFieldData indexFieldData; + private final Object missingValue; + + public ShortValuesComparatorSource(IndexNumericFieldData indexFieldData, @Nullable Object missingValue) { + this.indexFieldData = indexFieldData; + this.missingValue = missingValue; + } + + @Override + public SortField.Type reducedType() { + return SortField.Type.INT; + } + + @Override + public FieldComparator newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { + assert fieldname.equals(indexFieldData.getFieldNames().indexName()); + + short dMissingValue; + if (missingValue == null || "_last".equals(missingValue)) { + dMissingValue = reversed ? Short.MIN_VALUE : Short.MAX_VALUE; + } else if ("_first".equals(missingValue)) { + dMissingValue = reversed ? Short.MAX_VALUE : Short.MIN_VALUE; + } else { + dMissingValue = missingValue instanceof Number ? ((Number) missingValue).shortValue() : Short.parseShort(missingValue.toString()); + } + + return new ShortValuesComparator(indexFieldData, dMissingValue, numHits); + } +} 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 c4694089a84..dd6fdb76a24 100644 --- a/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java +++ b/src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java @@ -29,7 +29,7 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.Index; import org.elasticsearch.index.fielddata.*; -import org.elasticsearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource; +import org.elasticsearch.index.fielddata.fieldcomparator.ByteValuesComparatorSource; import org.elasticsearch.index.fielddata.ordinals.MultiFlatArrayOrdinals; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.settings.IndexSettings; @@ -152,6 +152,6 @@ public class ByteArrayIndexFieldData extends AbstractIndexFieldData