20
20
package org .elasticsearch .search ;
21
21
22
22
import org .apache .lucene .util .BytesRef ;
23
+ import org .elasticsearch .Version ;
23
24
import org .elasticsearch .common .io .stream .StreamInput ;
24
25
import org .elasticsearch .common .io .stream .StreamOutput ;
25
26
import org .elasticsearch .common .io .stream .Writeable ;
27
+ import org .elasticsearch .common .lucene .Lucene ;
26
28
import org .elasticsearch .common .xcontent .ToXContentFragment ;
27
29
import org .elasticsearch .common .xcontent .XContentBuilder ;
28
30
import org .elasticsearch .common .xcontent .XContentParser ;
35
37
36
38
public class SearchSortValues implements ToXContentFragment , Writeable {
37
39
38
- static final SearchSortValues EMPTY = new SearchSortValues (new Object [0 ]);
39
- private final Object [] sortValues ;
40
+ private static final Object [] EMPTY_ARRAY = new Object [0 ];
41
+ static final SearchSortValues EMPTY = new SearchSortValues (EMPTY_ARRAY );
42
+
43
+ private final Object [] formattedSortValues ;
44
+ private final Object [] rawSortValues ;
40
45
41
46
SearchSortValues (Object [] sortValues ) {
42
- this .sortValues = Objects .requireNonNull (sortValues , "sort values must not be empty" );
47
+ this .formattedSortValues = Objects .requireNonNull (sortValues , "sort values must not be empty" );
48
+ this .rawSortValues = EMPTY_ARRAY ;
43
49
}
44
50
45
- public SearchSortValues (Object [] sortValues , DocValueFormat [] sortValueFormats ) {
46
- Objects .requireNonNull (sortValues );
51
+ public SearchSortValues (Object [] rawSortValues , DocValueFormat [] sortValueFormats ) {
52
+ Objects .requireNonNull (rawSortValues );
47
53
Objects .requireNonNull (sortValueFormats );
48
- this .sortValues = Arrays .copyOf (sortValues , sortValues .length );
49
- for (int i = 0 ; i < sortValues .length ; ++i ) {
50
- if (this .sortValues [i ] instanceof BytesRef ) {
51
- this .sortValues [i ] = sortValueFormats [i ].format ((BytesRef ) sortValues [i ]);
54
+ if (rawSortValues .length != sortValueFormats .length ) {
55
+ throw new IllegalArgumentException ("formattedSortValues and sortValueFormats must hold the same number of items" );
56
+ }
57
+ this .rawSortValues = rawSortValues ;
58
+ this .formattedSortValues = Arrays .copyOf (rawSortValues , rawSortValues .length );
59
+ for (int i = 0 ; i < rawSortValues .length ; ++i ) {
60
+ //we currently format only BytesRef but we may want to change that in the future
61
+ Object sortValue = rawSortValues [i ];
62
+ if (sortValue instanceof BytesRef ) {
63
+ this .formattedSortValues [i ] = sortValueFormats [i ].format ((BytesRef ) sortValue );
52
64
}
53
65
}
54
66
}
55
67
56
- public SearchSortValues (StreamInput in ) throws IOException {
57
- int size = in .readVInt ();
58
- if (size > 0 ) {
59
- sortValues = new Object [size ];
60
- for (int i = 0 ; i < sortValues .length ; i ++) {
61
- byte type = in .readByte ();
62
- if (type == 0 ) {
63
- sortValues [i ] = null ;
64
- } else if (type == 1 ) {
65
- sortValues [i ] = in .readString ();
66
- } else if (type == 2 ) {
67
- sortValues [i ] = in .readInt ();
68
- } else if (type == 3 ) {
69
- sortValues [i ] = in .readLong ();
70
- } else if (type == 4 ) {
71
- sortValues [i ] = in .readFloat ();
72
- } else if (type == 5 ) {
73
- sortValues [i ] = in .readDouble ();
74
- } else if (type == 6 ) {
75
- sortValues [i ] = in .readByte ();
76
- } else if (type == 7 ) {
77
- sortValues [i ] = in .readShort ();
78
- } else if (type == 8 ) {
79
- sortValues [i ] = in .readBoolean ();
80
- } else {
81
- throw new IOException ("Can't match type [" + type + "]" );
82
- }
83
- }
68
+ SearchSortValues (StreamInput in ) throws IOException {
69
+ this .formattedSortValues = in .readArray (Lucene ::readSortValue , Object []::new );
70
+ if (in .getVersion ().onOrAfter (Version .V_6_6_0 )) {
71
+ this .rawSortValues = in .readArray (Lucene ::readSortValue , Object []::new );
84
72
} else {
85
- sortValues = new Object [ 0 ] ;
73
+ this . rawSortValues = EMPTY_ARRAY ;
86
74
}
87
75
}
88
76
89
77
@ Override
90
78
public void writeTo (StreamOutput out ) throws IOException {
91
- out .writeVInt (sortValues .length );
92
- for (Object sortValue : sortValues ) {
93
- if (sortValue == null ) {
94
- out .writeByte ((byte ) 0 );
95
- } else {
96
- Class type = sortValue .getClass ();
97
- if (type == String .class ) {
98
- out .writeByte ((byte ) 1 );
99
- out .writeString ((String ) sortValue );
100
- } else if (type == Integer .class ) {
101
- out .writeByte ((byte ) 2 );
102
- out .writeInt ((Integer ) sortValue );
103
- } else if (type == Long .class ) {
104
- out .writeByte ((byte ) 3 );
105
- out .writeLong ((Long ) sortValue );
106
- } else if (type == Float .class ) {
107
- out .writeByte ((byte ) 4 );
108
- out .writeFloat ((Float ) sortValue );
109
- } else if (type == Double .class ) {
110
- out .writeByte ((byte ) 5 );
111
- out .writeDouble ((Double ) sortValue );
112
- } else if (type == Byte .class ) {
113
- out .writeByte ((byte ) 6 );
114
- out .writeByte ((Byte ) sortValue );
115
- } else if (type == Short .class ) {
116
- out .writeByte ((byte ) 7 );
117
- out .writeShort ((Short ) sortValue );
118
- } else if (type == Boolean .class ) {
119
- out .writeByte ((byte ) 8 );
120
- out .writeBoolean ((Boolean ) sortValue );
121
- } else {
122
- throw new IOException ("Can't handle sort field value of type [" + type + "]" );
123
- }
124
- }
79
+ out .writeArray (Lucene ::writeSortValue , this .formattedSortValues );
80
+ if (out .getVersion ().onOrAfter (Version .V_6_6_0 )) {
81
+ out .writeArray (Lucene ::writeSortValue , this .rawSortValues );
125
82
}
126
83
}
127
84
128
85
@ Override
129
86
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
130
- if (sortValues .length > 0 ) {
87
+ if (formattedSortValues .length > 0 ) {
131
88
builder .startArray (Fields .SORT );
132
- for (Object sortValue : sortValues ) {
89
+ for (Object sortValue : formattedSortValues ) {
133
90
builder .value (sortValue );
134
91
}
135
92
builder .endArray ();
@@ -142,24 +99,37 @@ public static SearchSortValues fromXContent(XContentParser parser) throws IOExce
142
99
return new SearchSortValues (parser .list ().toArray ());
143
100
}
144
101
145
- public Object [] sortValues () {
146
- return sortValues ;
102
+ /**
103
+ * Returns the formatted version of the values that sorting was performed against
104
+ */
105
+ public Object [] getFormattedSortValues () {
106
+ return formattedSortValues ;
107
+ }
108
+
109
+ /**
110
+ * Returns the raw version of the values that sorting was performed against
111
+ */
112
+ public Object [] getRawSortValues () {
113
+ return rawSortValues ;
147
114
}
148
115
149
116
@ Override
150
- public boolean equals (Object obj ) {
151
- if (this == obj ) {
117
+ public boolean equals (Object o ) {
118
+ if (this == o ) {
152
119
return true ;
153
120
}
154
- if (obj == null || getClass () != obj .getClass ()) {
121
+ if (o == null || getClass () != o .getClass ()) {
155
122
return false ;
156
123
}
157
- SearchSortValues other = (SearchSortValues ) obj ;
158
- return Arrays .equals (sortValues , other .sortValues );
124
+ SearchSortValues that = (SearchSortValues ) o ;
125
+ return Arrays .equals (formattedSortValues , that .formattedSortValues ) &&
126
+ Arrays .equals (rawSortValues , that .rawSortValues );
159
127
}
160
128
161
129
@ Override
162
130
public int hashCode () {
163
- return Arrays .hashCode (sortValues );
131
+ int result = Arrays .hashCode (formattedSortValues );
132
+ result = 31 * result + Arrays .hashCode (rawSortValues );
133
+ return result ;
164
134
}
165
135
}
0 commit comments