Skip to content

Commit 5b7173f

Browse files
committed
move sorting to work with new field data
1 parent b739bf9 commit 5b7173f

16 files changed

+40
-27
lines changed

src/main/java/org/elasticsearch/common/lucene/Lucene.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.elasticsearch.common.logging.ESLogger;
3333
import org.elasticsearch.index.analysis.AnalyzerScope;
3434
import org.elasticsearch.index.analysis.NamedAnalyzer;
35-
import org.elasticsearch.index.field.data.FieldDataType;
35+
import org.elasticsearch.index.fielddata.IndexFieldData;
3636

3737
import java.io.IOException;
3838
import java.lang.reflect.Field;
@@ -215,7 +215,7 @@ public static void writeTopDocs(StreamOutput out, TopDocs topDocs, int from) thr
215215
out.writeString(sortField.getField());
216216
}
217217
if (sortField.getComparatorSource() != null) {
218-
writeSortType(out, ((FieldDataType.ExtendedFieldComparatorSource) sortField.getComparatorSource()).reducedType());
218+
writeSortType(out, ((IndexFieldData.XFieldComparatorSource) sortField.getComparatorSource()).reducedType());
219219
} else {
220220
writeSortType(out, sortField.getType());
221221
}

src/main/java/org/elasticsearch/index/fielddata/IndexFieldDataService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class IndexFieldDataService extends AbstractIndexComponent {
5656

5757
buildersByTypeAndFormat = MapBuilder.<Tuple<String, String>, IndexFieldData.Builder>newMapBuilder()
5858
.put(Tuple.tuple("string", "concrete_bytes"), new ConcreteBytesRefIndexFieldData.Builder())
59-
.put(Tuple.tuple("string", "packed_bytes"), new PackedBytesIndexFieldData.Builder())
59+
.put(Tuple.tuple("string", "paged_bytes"), new PagesBytesIndexFieldData.Builder())
6060
.put(Tuple.tuple("float", "array"), new FloatArrayIndexFieldData.Builder())
6161
.put(Tuple.tuple("double", "array"), new DoubleArrayIndexFieldData.Builder())
6262
.put(Tuple.tuple("byte", "array"), new ByteArrayIndexFieldData.Builder())

src/main/java/org/elasticsearch/index/fielddata/plain/ByteArrayIndexFieldData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public ByteArrayAtomicFieldData loadDirect(AtomicReaderContext context) throws E
8383

8484
Terms terms = reader.terms(getFieldNames().indexName());
8585
if (terms == null) {
86-
return new ByteArrayAtomicFieldData.Single(new byte[0], 0);
86+
return new ByteArrayAtomicFieldData.SingleFixedSet(new byte[1], 0, new FixedBitSet(1));
8787
}
8888

8989
// TODO: how can we guess the number of terms? numerics end up creating more terms per value...

src/main/java/org/elasticsearch/index/fielddata/plain/ConcreteBytesRefAtomicFieldData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public BytesRef getValueScratchByOrd(int ord, BytesRef ret) {
131131
} else {
132132
ret.bytes = value.bytes;
133133
ret.offset = value.offset;
134-
ret.length = value.offset;
134+
ret.length = value.length;
135135
}
136136
return ret;
137137
}

src/main/java/org/elasticsearch/index/fielddata/plain/DoubleArrayIndexFieldData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public DoubleArrayAtomicFieldData loadDirect(AtomicReaderContext context) throws
8383

8484
Terms terms = reader.terms(getFieldNames().indexName());
8585
if (terms == null) {
86-
return new DoubleArrayAtomicFieldData.Single(new double[0], 0);
86+
return new DoubleArrayAtomicFieldData.SingleFixedSet(new double[1], 0, new FixedBitSet(1));
8787
}
8888

8989
// TODO: how can we guess the number of terms? numerics end up creating more terms per value...

src/main/java/org/elasticsearch/index/fielddata/plain/FloatArrayIndexFieldData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public FloatArrayAtomicFieldData loadDirect(AtomicReaderContext context) throws
8383

8484
Terms terms = reader.terms(getFieldNames().indexName());
8585
if (terms == null) {
86-
return new FloatArrayAtomicFieldData.Single(new float[0], 0);
86+
return new FloatArrayAtomicFieldData.SingleFixedSet(new float[1], 0, new FixedBitSet(1));
8787
}
8888

8989
// TODO: how can we guess the number of terms? numerics end up creating more terms per value...

src/main/java/org/elasticsearch/index/fielddata/plain/GeoPointDoubleArrayIndexFieldData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public GeoPointDoubleArrayAtomicFieldData loadDirect(AtomicReaderContext context
7777

7878
Terms terms = reader.terms(getFieldNames().indexName());
7979
if (terms == null) {
80-
return new GeoPointDoubleArrayAtomicFieldData.Single(new double[0], new double[0], 0);
80+
return new GeoPointDoubleArrayAtomicFieldData.SingleFixedSet(new double[1], new double[1], 0, new FixedBitSet(1));
8181
}
8282

8383
// TODO: how can we guess the number of terms? numerics end up creating more terms per value...

src/main/java/org/elasticsearch/index/fielddata/plain/IntArrayIndexFieldData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public IntArrayAtomicFieldData loadDirect(AtomicReaderContext context) throws Ex
8383

8484
Terms terms = reader.terms(getFieldNames().indexName());
8585
if (terms == null) {
86-
return new IntArrayAtomicFieldData.Single(new int[0], 0);
86+
return new IntArrayAtomicFieldData.SingleFixedSet(new int[1], 0, new FixedBitSet(1));
8787
}
8888

8989
// TODO: how can we guess the number of terms? numerics end up creating more terms per value...

src/main/java/org/elasticsearch/index/fielddata/plain/LongArrayIndexFieldData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public LongArrayAtomicFieldData loadDirect(AtomicReaderContext context) throws E
8383

8484
Terms terms = reader.terms(getFieldNames().indexName());
8585
if (terms == null) {
86-
return new LongArrayAtomicFieldData.Single(new long[0], 0);
86+
return new LongArrayAtomicFieldData.SingleFixedSet(new long[1], 0, new FixedBitSet(1));
8787
}
8888

8989
// TODO: how can we guess the number of terms? numerics end up creating more terms per value...

src/main/java/org/elasticsearch/index/fielddata/plain/PackedBytesAtomicFieldData.java renamed to src/main/java/org/elasticsearch/index/fielddata/plain/PagedBytesAtomicFieldData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
/**
3434
*/
35-
public class PackedBytesAtomicFieldData implements AtomicOrdinalFieldData<ScriptDocValues.Strings> {
35+
public class PagedBytesAtomicFieldData implements AtomicOrdinalFieldData<ScriptDocValues.Strings> {
3636

3737
// 0 ordinal in values means no value (its null)
3838
private final PagedBytes.Reader bytes;
@@ -42,7 +42,7 @@ public class PackedBytesAtomicFieldData implements AtomicOrdinalFieldData<Script
4242
private int[] hashes;
4343
private long size = -1;
4444

45-
public PackedBytesAtomicFieldData(PagedBytes.Reader bytes, PackedInts.Reader termOrdToBytesOffset, Ordinals ordinals) {
45+
public PagedBytesAtomicFieldData(PagedBytes.Reader bytes, PackedInts.Reader termOrdToBytesOffset, Ordinals ordinals) {
4646
this.bytes = bytes;
4747
this.termOrdToBytesOffset = termOrdToBytesOffset;
4848
this.ordinals = ordinals;

src/main/java/org/elasticsearch/index/fielddata/plain/PackedBytesIndexFieldData.java renamed to src/main/java/org/elasticsearch/index/fielddata/plain/PagesBytesIndexFieldData.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@
4141

4242
/**
4343
*/
44-
public class PackedBytesIndexFieldData extends AbstractIndexFieldData<PackedBytesAtomicFieldData> implements IndexOrdinalFieldData<PackedBytesAtomicFieldData> {
44+
public class PagesBytesIndexFieldData extends AbstractIndexFieldData<PagedBytesAtomicFieldData> implements IndexOrdinalFieldData<PagedBytesAtomicFieldData> {
4545

4646
public static class Builder implements IndexFieldData.Builder {
4747

4848
@Override
4949
public IndexFieldData build(Index index, @IndexSettings Settings indexSettings, FieldMapper.Names fieldNames, FieldDataType type, IndexFieldDataCache cache) {
50-
return new PackedBytesIndexFieldData(index, indexSettings, fieldNames, type, cache);
50+
return new PagesBytesIndexFieldData(index, indexSettings, fieldNames, type, cache);
5151
}
5252
}
5353

54-
public PackedBytesIndexFieldData(Index index, @IndexSettings Settings indexSettings, FieldMapper.Names fieldNames, FieldDataType fieldDataType, IndexFieldDataCache cache) {
54+
public PagesBytesIndexFieldData(Index index, @IndexSettings Settings indexSettings, FieldMapper.Names fieldNames, FieldDataType fieldDataType, IndexFieldDataCache cache) {
5555
super(index, indexSettings, fieldNames, fieldDataType, cache);
5656
}
5757

@@ -61,7 +61,7 @@ public boolean valuesOrdered() {
6161
}
6262

6363
@Override
64-
public PackedBytesAtomicFieldData load(AtomicReaderContext context) {
64+
public PagedBytesAtomicFieldData load(AtomicReaderContext context) {
6565
try {
6666
return cache.load(context, this);
6767
} catch (Throwable e) {
@@ -74,7 +74,7 @@ public PackedBytesAtomicFieldData load(AtomicReaderContext context) {
7474
}
7575

7676
@Override
77-
public PackedBytesAtomicFieldData loadDirect(AtomicReaderContext context) throws Exception {
77+
public PagedBytesAtomicFieldData loadDirect(AtomicReaderContext context) throws Exception {
7878
AtomicReader reader = context.reader();
7979

8080
Terms terms = reader.terms(getFieldNames().indexName());
@@ -83,7 +83,7 @@ public PackedBytesAtomicFieldData loadDirect(AtomicReaderContext context) throws
8383
// 0 is reserved for "unset"
8484
bytes.copyUsingLengthPrefix(new BytesRef());
8585
GrowableWriter termOrdToBytesOffset = new GrowableWriter(1, 2, PackedInts.FASTEST);
86-
return new PackedBytesAtomicFieldData(bytes.freeze(true), termOrdToBytesOffset.getMutable(), new EmptyOrdinals(reader.maxDoc()));
86+
return new PagedBytesAtomicFieldData(bytes.freeze(true), termOrdToBytesOffset.getMutable(), new EmptyOrdinals(reader.maxDoc()));
8787
}
8888

8989
final PagedBytes bytes = new PagedBytes(15);
@@ -174,13 +174,13 @@ public PackedBytesAtomicFieldData loadDirect(AtomicReaderContext context) throws
174174
PackedInts.Reader termOrdToBytesOffsetReader = termOrdToBytesOffset.getMutable();
175175

176176
if (ordinals.size() == 1) {
177-
return new PackedBytesAtomicFieldData(bytesReader, termOrdToBytesOffsetReader, new SingleArrayOrdinals(ordinals.get(0), termOrd));
177+
return new PagedBytesAtomicFieldData(bytesReader, termOrdToBytesOffsetReader, new SingleArrayOrdinals(ordinals.get(0), termOrd));
178178
} else {
179179
int[][] nativeOrdinals = new int[ordinals.size()][];
180180
for (int i = 0; i < nativeOrdinals.length; i++) {
181181
nativeOrdinals[i] = ordinals.get(i);
182182
}
183-
return new PackedBytesAtomicFieldData(bytesReader, termOrdToBytesOffsetReader, new MultiFlatArrayOrdinals(nativeOrdinals, termOrd));
183+
return new PagedBytesAtomicFieldData(bytesReader, termOrdToBytesOffsetReader, new MultiFlatArrayOrdinals(nativeOrdinals, termOrd));
184184
}
185185

186186
}

src/main/java/org/elasticsearch/index/fielddata/plain/ShortArrayIndexFieldData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public ShortArrayAtomicFieldData loadDirect(AtomicReaderContext context) throws
8383

8484
Terms terms = reader.terms(getFieldNames().indexName());
8585
if (terms == null) {
86-
return new ShortArrayAtomicFieldData.Single(new short[0], 0);
86+
return new ShortArrayAtomicFieldData.SingleFixedSet(new short[1], 0, new FixedBitSet(1));
8787
}
8888

8989
// TODO: how can we guess the number of terms? numerics end up creating more terms per value...

src/main/java/org/elasticsearch/search/sort/SortParseElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private void addSortField(SearchContext context, List<SortField> sortFields, Str
163163
}
164164
throw new SearchParseException(context, "No mapping found for [" + fieldName + "] in order to sort on");
165165
}
166-
sortFields.add(new SortField(fieldMapper.names().indexName(), fieldMapper.fieldDataType().newFieldComparatorSource(context.fieldDataCache(), missing), reverse));
166+
sortFields.add(new SortField(fieldMapper.names().indexName(), context.fieldData().getForField(fieldMapper).comparatorSource(missing), reverse));
167167
}
168168
}
169169
}

src/test/java/org/elasticsearch/test/integration/search/sort/SimpleSortTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,22 +531,24 @@ public void testSortMissing() throws Exception {
531531

532532
client.admin().indices().prepareFlush().setRefresh(true).execute().actionGet();
533533

534-
logger.info("--> sort with no missing");
534+
logger.info("--> sort with no missing (same as missing _last)");
535535
SearchResponse searchResponse = client.prepareSearch()
536536
.setQuery(matchAllQuery())
537537
.addSort(SortBuilders.fieldSort("i_value").order(SortOrder.ASC))
538538
.execute().actionGet();
539+
assertThat(Arrays.toString(searchResponse.shardFailures()), searchResponse.failedShards(), equalTo(0));
539540

540541
assertThat(searchResponse.hits().getTotalHits(), equalTo(3l));
541542
assertThat(searchResponse.hits().getAt(0).id(), equalTo("1"));
542-
assertThat(searchResponse.hits().getAt(1).id(), equalTo("2"));
543-
assertThat(searchResponse.hits().getAt(2).id(), equalTo("3"));
543+
assertThat(searchResponse.hits().getAt(1).id(), equalTo("3"));
544+
assertThat(searchResponse.hits().getAt(2).id(), equalTo("2"));
544545

545546
logger.info("--> sort with missing _last");
546547
searchResponse = client.prepareSearch()
547548
.setQuery(matchAllQuery())
548549
.addSort(SortBuilders.fieldSort("i_value").order(SortOrder.ASC).missing("_last"))
549550
.execute().actionGet();
551+
assertThat(Arrays.toString(searchResponse.shardFailures()), searchResponse.failedShards(), equalTo(0));
550552

551553
assertThat(searchResponse.hits().getTotalHits(), equalTo(3l));
552554
assertThat(searchResponse.hits().getAt(0).id(), equalTo("1"));
@@ -558,6 +560,7 @@ public void testSortMissing() throws Exception {
558560
.setQuery(matchAllQuery())
559561
.addSort(SortBuilders.fieldSort("i_value").order(SortOrder.ASC).missing("_first"))
560562
.execute().actionGet();
563+
assertThat(Arrays.toString(searchResponse.shardFailures()), searchResponse.failedShards(), equalTo(0));
561564

562565
assertThat(searchResponse.hits().getTotalHits(), equalTo(3l));
563566
assertThat(searchResponse.hits().getAt(0).id(), equalTo("2"));

src/test/java/org/elasticsearch/test/unit/index/fielddata/PackedBytesStringFieldDataTests.java renamed to src/test/java/org/elasticsearch/test/unit/index/fielddata/PagedBytesStringFieldDataTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
/**
2727
*/
2828
@Test
29-
public class PackedBytesStringFieldDataTests extends StringFieldDataTests {
29+
public class PagedBytesStringFieldDataTests extends StringFieldDataTests {
3030

3131
@Override
3232
protected FieldDataType getFieldDataType() {
33-
return new FieldDataType("string", "packed_bytes", ImmutableMap.<String, String>of());
33+
return new FieldDataType("string", "paged_bytes", ImmutableMap.<String, String>of());
3434
}
3535
}

src/test/java/org/elasticsearch/test/unit/index/fielddata/StringFieldDataTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ protected String four() {
5656
return "4";
5757
}
5858

59+
private String toString(Object value) {
60+
if (value instanceof BytesRef) {
61+
return ((BytesRef) value).utf8ToString();
62+
}
63+
return value.toString();
64+
}
65+
5966
protected void fillSingleValueAllSet() throws Exception {
6067
Document d = new Document();
6168
d.add(new StringField("_id", "1", Field.Store.NO));
@@ -218,8 +225,11 @@ public void testSingleValueAllSet() throws Exception {
218225
new Sort(new SortField("value", indexFieldData.comparatorSource(null))));
219226
assertThat(topDocs.totalHits, equalTo(3));
220227
assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
228+
assertThat(toString(((FieldDoc) topDocs.scoreDocs[0]).fields[0]), equalTo(one()));
221229
assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
230+
assertThat(toString(((FieldDoc) topDocs.scoreDocs[1]).fields[0]), equalTo(two()));
222231
assertThat(topDocs.scoreDocs[2].doc, equalTo(2));
232+
assertThat(toString(((FieldDoc) topDocs.scoreDocs[2]).fields[0]), equalTo(three()));
223233

224234
topDocs = searcher.search(new MatchAllDocsQuery(), 10,
225235
new Sort(new SortField("value", indexFieldData.comparatorSource(null), true)));

0 commit comments

Comments
 (0)