Skip to content

Commit c2f3eab

Browse files
imotovkimchy
authored andcommitted
lucene 4: fix sorting
1 parent 2b58c2d commit c2f3eab

File tree

7 files changed

+59
-14
lines changed

7 files changed

+59
-14
lines changed

src/main/java/org/apache/lucene/search/ShardFieldDocSortedHitQueue.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919

2020
package org.apache.lucene.search;
2121

22+
import org.apache.lucene.util.BytesRef;
2223
import org.apache.lucene.util.PriorityQueue;
2324
import org.elasticsearch.ElasticSearchIllegalStateException;
2425
import org.elasticsearch.search.controller.ShardFieldDoc;
2526

2627
import java.io.IOException;
27-
import java.text.Collator;
28-
import java.util.Locale;
2928

3029
/**
3130
*
@@ -86,8 +85,8 @@ SortField[] getFields() {
8685
/**
8786
* Returns whether <code>a</code> is less relevant than <code>b</code>.
8887
*
89-
* @param a ScoreDoc
90-
* @param b ScoreDoc
88+
* @param docA ScoreDoc
89+
* @param docB ScoreDoc
9190
* @return <code>true</code> if document <code>a</code> should be sorted after document <code>b</code>.
9291
*/
9392
@SuppressWarnings("unchecked")
@@ -98,8 +97,8 @@ protected final boolean lessThan(final ShardFieldDoc docA, final ShardFieldDoc d
9897
for (int i = 0; i < n && c == 0; ++i) {
9998
final SortField.Type type = fields[i].getType();
10099
if (type == SortField.Type.STRING) {
101-
final String s1 = (String) docA.fields[i];
102-
final String s2 = (String) docB.fields[i];
100+
final BytesRef s1 = (BytesRef) docA.fields[i];
101+
final BytesRef s2 = (BytesRef) docB.fields[i];
103102
// null values need to be sorted first, because of how FieldCache.getStringIndex()
104103
// works - in that routine, any documents without a value in the given field are
105104
// put first. If both are null, the next SortField is used

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.lucene.index.*;
2525
import org.apache.lucene.search.*;
2626
import org.apache.lucene.store.Directory;
27+
import org.apache.lucene.util.BytesRef;
2728
import org.apache.lucene.util.Version;
2829
import org.elasticsearch.common.Nullable;
2930
import org.elasticsearch.common.io.stream.StreamInput;
@@ -173,6 +174,8 @@ public static TopDocs readTopDocs(StreamInput in) throws IOException {
173174
cFields[j] = in.readShort();
174175
} else if (type == 8) {
175176
cFields[j] = in.readBoolean();
177+
} else if (type == 9) {
178+
cFields[j] = in.readBytesRef();
176179
} else {
177180
throw new IOException("Can't match type [" + type + "]");
178181
}
@@ -258,6 +261,9 @@ public static void writeTopDocs(StreamOutput out, TopDocs topDocs, int from) thr
258261
} else if (type == Boolean.class) {
259262
out.writeByte((byte) 8);
260263
out.writeBoolean((Boolean) field);
264+
} else if (type == BytesRef.class) {
265+
out.writeByte((byte) 9);
266+
out.writeBytesRef((BytesRef) field);
261267
} else {
262268
throw new IOException("Can't handle sort field value of type [" + type + "]");
263269
}

src/main/java/org/elasticsearch/index/field/data/DocFieldData.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ public boolean isEmpty() {
4646
return !fieldData.hasValue(docId);
4747
}
4848

49-
public BytesRef stringValue() {
50-
return fieldData.stringValue(docId);
49+
public String stringValue() {
50+
BytesRef val = fieldData.stringValue(docId);
51+
if (val == null) {
52+
return null;
53+
}
54+
return val.utf8ToString();
5155
}
5256

53-
public BytesRef getStringValue() {
57+
public String getStringValue() {
5458
return stringValue();
5559
}
5660

src/main/java/org/elasticsearch/index/field/data/strings/StringDocFieldData.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,31 @@ public StringDocFieldData(StringFieldData fieldData) {
3131
super(fieldData);
3232
}
3333

34-
public BytesRef getValue() {
34+
public String getValue() {
35+
BytesRef value = fieldData.value(docId);
36+
if (value == null) {
37+
return null;
38+
}
39+
return value.utf8ToString();
40+
}
41+
42+
public String[] getValues() {
43+
BytesRef[] values = fieldData.values(docId);
44+
if (values == null) {
45+
return null;
46+
}
47+
String[] stringValues = new String[values.length];
48+
for (int i = 0; i < values.length; i++) {
49+
stringValues[i] = values[i].utf8ToString();
50+
}
51+
return stringValues;
52+
}
53+
54+
public BytesRef getBytesValue() {
3555
return fieldData.value(docId);
3656
}
3757

38-
public BytesRef[] getValues() {
58+
public BytesRef[] getBytesValues() {
3959
return fieldData.values(docId);
4060
}
4161
}

src/main/java/org/elasticsearch/index/field/data/support/FieldDataLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static <T extends FieldData> T load(AtomicReader reader, String field, Ty
4646

4747
Terms terms = reader.terms(field);
4848
if (terms == null) {
49-
return loader.buildSingleValue(field, new int[0]); // Return empty field data if field doesn't exists.
49+
return loader.buildSingleValue(field, ordinals.get(0)); // Return empty field data if field doesn't exists.
5050
}
5151

5252
TermsEnum termsEnum = terms.iterator(null);

src/main/java/org/elasticsearch/search/internal/InternalSearchHit.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.collect.ImmutableMap;
2323
import org.apache.lucene.search.Explanation;
24+
import org.apache.lucene.util.BytesRef;
2425
import org.elasticsearch.ElasticSearchParseException;
2526
import org.elasticsearch.common.Nullable;
2627
import org.elasticsearch.common.Strings;
@@ -29,6 +30,7 @@
2930
import org.elasticsearch.common.compress.CompressorFactory;
3031
import org.elasticsearch.common.io.stream.StreamInput;
3132
import org.elasticsearch.common.io.stream.StreamOutput;
33+
import org.elasticsearch.common.text.StringAndBytesText;
3234
import org.elasticsearch.common.text.Text;
3335
import org.elasticsearch.common.xcontent.XContentBuilder;
3436
import org.elasticsearch.common.xcontent.XContentBuilderString;
@@ -295,6 +297,14 @@ public void highlightFields(Map<String, HighlightField> highlightFields) {
295297
}
296298

297299
public void sortValues(Object[] sortValues) {
300+
// LUCENE 4 UPGRADE: There must be a better way
301+
if (sortValues != null) {
302+
for (int i=0; i<sortValues.length; i++) {
303+
if (sortValues[i] instanceof BytesRef) {
304+
sortValues[i] = new StringAndBytesText(new BytesArray((BytesRef)sortValues[i]));
305+
}
306+
}
307+
}
298308
this.sortValues = sortValues;
299309
}
300310

@@ -571,6 +581,9 @@ public void readFrom(StreamInput in, InternalSearchHits.StreamContext context) t
571581
sortValues[i] = in.readShort();
572582
} else if (type == 8) {
573583
sortValues[i] = in.readBoolean();
584+
} else if (type == 9) {
585+
// LUCENE 4 UPGRADE: There must be a better way
586+
sortValues[i] = new StringAndBytesText(new BytesArray(in.readBytesRef()));
574587
} else {
575588
throw new IOException("Can't match type [" + type + "]");
576589
}
@@ -664,6 +677,9 @@ public void writeTo(StreamOutput out, InternalSearchHits.StreamContext context)
664677
} else if (type == Boolean.class) {
665678
out.writeByte((byte) 8);
666679
out.writeBoolean((Boolean) sortValue);
680+
} else if (type == BytesRef.class) {
681+
out.writeByte((byte) 9);
682+
out.writeBytesRef((BytesRef) sortValue);
667683
} else {
668684
throw new IOException("Can't handle sort field value of type [" + type + "]");
669685
}

src/test/java/org/elasticsearch/test/unit/index/field/data/strings/StringFieldDataTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ public void stringFieldDataTests() throws Exception {
8282
// svalue
8383
assertThat(sFieldData.hasValue(0), equalTo(true));
8484
assertThat(sFieldData.value(0).utf8ToString(), equalTo("zzz"));
85-
assertThat(sFieldData.docFieldData(0).getValue().utf8ToString(), equalTo("zzz"));
85+
assertThat(sFieldData.docFieldData(0).getValue(), equalTo("zzz"));
8686
assertThat(sFieldData.values(0).length, equalTo(1));
8787
assertThat(sFieldData.docFieldData(0).getValues().length, equalTo(1));
8888
assertThat(sFieldData.values(0)[0].utf8ToString(), equalTo("zzz"));
89-
assertThat(sFieldData.docFieldData(0).getValues()[0].utf8ToString(), equalTo("zzz"));
89+
assertThat(sFieldData.docFieldData(0).getValues()[0], equalTo("zzz"));
9090

9191
assertThat(sFieldData.hasValue(1), equalTo(true));
9292
assertThat(sFieldData.value(1).utf8ToString(), equalTo("xxx"));

0 commit comments

Comments
 (0)