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 ;
22
24
import org .elasticsearch .common .Strings ;
23
25
import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
26
+ import org .elasticsearch .common .network .InetAddresses ;
24
27
import org .elasticsearch .common .time .DateFormatter ;
25
28
import org .elasticsearch .common .xcontent .ToXContentFragment ;
26
29
import org .elasticsearch .common .xcontent .XContentBuilder ;
@@ -51,7 +54,12 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() {
51
54
52
55
@ Override
53
56
protected SortValue createTestInstance () {
54
- return randomBoolean () ? SortValue .from (randomDouble ()) : SortValue .from (randomLong ());
57
+ switch (between (0 , 2 )) {
58
+ case 0 : return SortValue .from (randomDouble ());
59
+ case 1 : return SortValue .from (randomLong ());
60
+ case 2 : return SortValue .from (new BytesRef (randomAlphaOfLength (5 )));
61
+ default : throw new AssertionError ();
62
+ }
55
63
}
56
64
57
65
@ Override
@@ -81,11 +89,23 @@ public void testToXContentLong() {
81
89
assertThat (toXContent (SortValue .from (1 ), STRICT_DATE_TIME ), equalTo ("{\" test\" :\" 1970-01-01T00:00:00.001Z\" }" ));
82
90
}
83
91
92
+ public void testToXContentBytes () {
93
+ assertThat (toXContent (SortValue .from (new BytesRef ("cat" )), DocValueFormat .RAW ), equalTo ("{\" test\" :\" cat\" }" ));
94
+ assertThat (
95
+ toXContent (SortValue .from (new BytesRef (InetAddressPoint .encode (InetAddresses .forString ("127.0.0.1" )))), DocValueFormat .IP ),
96
+ equalTo ("{\" test\" :\" 127.0.0.1\" }" )
97
+ );
98
+ }
99
+
84
100
public void testCompareDifferentTypes () {
85
101
assertThat (SortValue .from (1.0 ), lessThan (SortValue .from (1 )));
86
102
assertThat (SortValue .from (Double .MAX_VALUE ), lessThan (SortValue .from (Long .MIN_VALUE )));
87
103
assertThat (SortValue .from (1 ), greaterThan (SortValue .from (1.0 )));
88
104
assertThat (SortValue .from (Long .MIN_VALUE ), greaterThan (SortValue .from (Double .MAX_VALUE )));
105
+ assertThat (SortValue .from (new BytesRef ("cat" )), lessThan (SortValue .from (1 )));
106
+ assertThat (SortValue .from (1 ), greaterThan (SortValue .from (new BytesRef ("cat" ))));
107
+ assertThat (SortValue .from (new BytesRef ("cat" )), lessThan (SortValue .from (1.0 )));
108
+ assertThat (SortValue .from (1.0 ), greaterThan (SortValue .from (new BytesRef ("cat" ))));
89
109
}
90
110
91
111
public void testCompareDoubles () {
@@ -102,6 +122,13 @@ public void testCompareLongs() {
102
122
assertThat (SortValue .from (r ), greaterThan (SortValue .from (r - 1 )));
103
123
}
104
124
125
+ public void testBytes () {
126
+ String r = randomAlphaOfLength (5 );
127
+ assertThat (SortValue .from (new BytesRef (r )), equalTo (SortValue .from (new BytesRef (r ))));
128
+ assertThat (SortValue .from (new BytesRef (r )), lessThan (SortValue .from (new BytesRef (r + "with_suffix" ))));
129
+ assertThat (SortValue .from (new BytesRef (r )), greaterThan (SortValue .from (new BytesRef (new byte [] {}))));
130
+ }
131
+
105
132
public String toXContent (SortValue sortValue , DocValueFormat format ) {
106
133
return Strings .toString (new ToXContentFragment () {
107
134
@ Override
0 commit comments