19
19
20
20
package org .elasticsearch .search .sort ;
21
21
22
+ import org .apache .lucene .document .InetAddressPoint ;
23
+ import org .apache .lucene .util .BytesRef ;
24
+ import org .elasticsearch .Version ;
22
25
import org .elasticsearch .common .Strings ;
23
26
import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
27
+ import org .elasticsearch .common .network .InetAddresses ;
24
28
import org .elasticsearch .common .time .DateFormatter ;
25
29
import org .elasticsearch .common .xcontent .ToXContentFragment ;
26
30
import org .elasticsearch .common .xcontent .XContentBuilder ;
27
31
import org .elasticsearch .index .mapper .DateFieldMapper ;
28
32
import org .elasticsearch .search .DocValueFormat ;
29
33
import org .elasticsearch .test .AbstractNamedWriteableTestCase ;
34
+ import org .elasticsearch .test .VersionUtils ;
30
35
31
36
import java .io .IOException ;
32
37
import java .time .ZoneId ;
@@ -51,7 +56,12 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() {
51
56
52
57
@ Override
53
58
protected SortValue createTestInstance () {
54
- return randomBoolean () ? SortValue .from (randomDouble ()) : SortValue .from (randomLong ());
59
+ switch (between (0 , 2 )) {
60
+ case 0 : return SortValue .from (randomDouble ());
61
+ case 1 : return SortValue .from (randomLong ());
62
+ case 2 : return SortValue .from (new BytesRef (randomAlphaOfLength (5 )));
63
+ default : throw new AssertionError ();
64
+ }
55
65
}
56
66
57
67
@ Override
@@ -81,11 +91,23 @@ public void testToXContentLong() {
81
91
assertThat (toXContent (SortValue .from (1 ), STRICT_DATE_TIME ), equalTo ("{\" test\" :\" 1970-01-01T00:00:00.001Z\" }" ));
82
92
}
83
93
94
+ public void testToXContentBytes () {
95
+ assertThat (toXContent (SortValue .from (new BytesRef ("cat" )), DocValueFormat .RAW ), equalTo ("{\" test\" :\" cat\" }" ));
96
+ assertThat (
97
+ toXContent (SortValue .from (new BytesRef (InetAddressPoint .encode (InetAddresses .forString ("127.0.0.1" )))), DocValueFormat .IP ),
98
+ equalTo ("{\" test\" :\" 127.0.0.1\" }" )
99
+ );
100
+ }
101
+
84
102
public void testCompareDifferentTypes () {
85
103
assertThat (SortValue .from (1.0 ), lessThan (SortValue .from (1 )));
86
104
assertThat (SortValue .from (Double .MAX_VALUE ), lessThan (SortValue .from (Long .MIN_VALUE )));
87
105
assertThat (SortValue .from (1 ), greaterThan (SortValue .from (1.0 )));
88
106
assertThat (SortValue .from (Long .MIN_VALUE ), greaterThan (SortValue .from (Double .MAX_VALUE )));
107
+ assertThat (SortValue .from (new BytesRef ("cat" )), lessThan (SortValue .from (1 )));
108
+ assertThat (SortValue .from (1 ), greaterThan (SortValue .from (new BytesRef ("cat" ))));
109
+ assertThat (SortValue .from (new BytesRef ("cat" )), lessThan (SortValue .from (1.0 )));
110
+ assertThat (SortValue .from (1.0 ), greaterThan (SortValue .from (new BytesRef ("cat" ))));
89
111
}
90
112
91
113
public void testCompareDoubles () {
@@ -102,6 +124,25 @@ public void testCompareLongs() {
102
124
assertThat (SortValue .from (r ), greaterThan (SortValue .from (r - 1 )));
103
125
}
104
126
127
+ public void testBytes () {
128
+ String r = randomAlphaOfLength (5 );
129
+ assertThat (SortValue .from (new BytesRef (r )), equalTo (SortValue .from (new BytesRef (r ))));
130
+ assertThat (SortValue .from (new BytesRef (r )), lessThan (SortValue .from (new BytesRef (r + "with_suffix" ))));
131
+ assertThat (SortValue .from (new BytesRef (r )), greaterThan (SortValue .from (new BytesRef (new byte [] {}))));
132
+ }
133
+
134
+ public void testSerializeBytesToOldVersion () {
135
+ SortValue value = SortValue .from (new BytesRef ("can't send me!" ));
136
+ Version version = VersionUtils .randomVersionBetween (random (), Version .V_7_0_0 , Version .V_7_10_1 );
137
+ Exception e = expectThrows (IllegalArgumentException .class , () -> copyInstance (value , version ));
138
+ assertThat (
139
+ e .getMessage (),
140
+ equalTo (
141
+ "versions of Elasticsearch before 7.11.0 can't handle non-numeric sort values and attempted to send to [" + version + "]"
142
+ )
143
+ );
144
+ }
145
+
105
146
public String toXContent (SortValue sortValue , DocValueFormat format ) {
106
147
return Strings .toString (new ToXContentFragment () {
107
148
@ Override
0 commit comments