2020package org .elasticsearch .search ;
2121
2222import org .apache .lucene .util .BytesRef ;
23+ import org .elasticsearch .Version ;
2324import org .elasticsearch .common .io .stream .StreamInput ;
2425import org .elasticsearch .common .io .stream .StreamOutput ;
2526import org .elasticsearch .common .io .stream .Writeable ;
27+ import org .elasticsearch .common .lucene .Lucene ;
2628import org .elasticsearch .common .xcontent .ToXContentFragment ;
2729import org .elasticsearch .common .xcontent .XContentBuilder ;
2830import org .elasticsearch .common .xcontent .XContentParser ;
3537
3638public class SearchSortValues implements ToXContentFragment , Writeable {
3739
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 ;
4045
4146 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 ;
4349 }
4450
45- public SearchSortValues (Object [] sortValues , DocValueFormat [] sortValueFormats ) {
46- Objects .requireNonNull (sortValues );
51+ public SearchSortValues (Object [] rawSortValues , DocValueFormat [] sortValueFormats ) {
52+ Objects .requireNonNull (rawSortValues );
4753 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 );
5264 }
5365 }
5466 }
5567
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_7_0_0 )) {
71+ this .rawSortValues = in .readArray (Lucene ::readSortValue , Object []::new );
8472 } else {
85- sortValues = new Object [ 0 ] ;
73+ this . rawSortValues = EMPTY_ARRAY ;
8674 }
8775 }
8876
8977 @ Override
9078 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_7_0_0 )) {
81+ out .writeArray (Lucene ::writeSortValue , this .rawSortValues );
12582 }
12683 }
12784
12885 @ Override
12986 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
130- if (sortValues .length > 0 ) {
87+ if (formattedSortValues .length > 0 ) {
13188 builder .startArray (Fields .SORT );
132- for (Object sortValue : sortValues ) {
89+ for (Object sortValue : formattedSortValues ) {
13390 builder .value (sortValue );
13491 }
13592 builder .endArray ();
@@ -142,24 +99,37 @@ public static SearchSortValues fromXContent(XContentParser parser) throws IOExce
14299 return new SearchSortValues (parser .list ().toArray ());
143100 }
144101
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 ;
147114 }
148115
149116 @ Override
150- public boolean equals (Object obj ) {
151- if (this == obj ) {
117+ public boolean equals (Object o ) {
118+ if (this == o ) {
152119 return true ;
153120 }
154- if (obj == null || getClass () != obj .getClass ()) {
121+ if (o == null || getClass () != o .getClass ()) {
155122 return false ;
156123 }
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 );
159127 }
160128
161129 @ Override
162130 public int hashCode () {
163- return Arrays .hashCode (sortValues );
131+ int result = Arrays .hashCode (formattedSortValues );
132+ result = 31 * result + Arrays .hashCode (rawSortValues );
133+ return result ;
164134 }
165135}
0 commit comments