1919
2020package org .elasticsearch .search .searchafter ;
2121
22+ import org .apache .lucene .document .LatLonDocValuesField ;
23+ import org .apache .lucene .search .FieldComparator ;
24+ import org .apache .lucene .search .SortField ;
25+ import org .apache .lucene .search .SortedNumericSortField ;
26+ import org .apache .lucene .search .SortedSetSortField ;
2227import org .elasticsearch .common .geo .GeoPoint ;
2328import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
2429import org .elasticsearch .common .text .Text ;
2732import org .elasticsearch .common .xcontent .XContentParser ;
2833import org .elasticsearch .common .xcontent .XContentType ;
2934import org .elasticsearch .common .xcontent .json .JsonXContent ;
35+ import org .elasticsearch .index .fielddata .IndexFieldData ;
36+ import org .elasticsearch .search .MultiValueMode ;
3037import org .elasticsearch .test .ESTestCase ;
31- import org .hamcrest .Matchers ;
3238
3339import java .io .IOException ;
3440import java .util .Collections ;
3541
42+ import static org .elasticsearch .search .searchafter .SearchAfterBuilder .extractSortType ;
3643import static org .elasticsearch .test .EqualsHashCodeTestUtils .checkEqualsAndHashCode ;
44+ import static org .hamcrest .Matchers .equalTo ;
3745
3846public class SearchAfterBuilderTests extends ESTestCase {
3947 private static final int NUMBER_OF_TESTBUILDERS = 20 ;
@@ -182,7 +190,7 @@ public void testWithNullArray() throws Exception {
182190 builder .setSortValues (null );
183191 fail ("Should fail on null array." );
184192 } catch (NullPointerException e ) {
185- assertThat (e .getMessage (), Matchers . equalTo ("Values cannot be null." ));
193+ assertThat (e .getMessage (), equalTo ("Values cannot be null." ));
186194 }
187195 }
188196
@@ -192,7 +200,7 @@ public void testWithEmptyArray() throws Exception {
192200 builder .setSortValues (new Object [0 ]);
193201 fail ("Should fail on empty array." );
194202 } catch (IllegalArgumentException e ) {
195- assertThat (e .getMessage (), Matchers . equalTo ("Values must contains at least one value." ));
203+ assertThat (e .getMessage (), equalTo ("Values must contains at least one value." ));
196204 }
197205 }
198206
@@ -215,4 +223,29 @@ private static void randomSearchFromBuilderWithSortValueThrows(Object containing
215223 Exception e = expectThrows (IllegalArgumentException .class , () -> builder .setSortValues (values ));
216224 assertEquals (e .getMessage (), "Can't handle search_after field value of type [" + containing .getClass () + "]" );
217225 }
226+
227+ public void testExtractSortType () throws Exception {
228+ SortField .Type type = extractSortType (LatLonDocValuesField .newDistanceSort ("field" , 0.0 , 180.0 ));
229+ assertThat (type , equalTo (SortField .Type .DOUBLE ));
230+ IndexFieldData .XFieldComparatorSource source = new IndexFieldData .XFieldComparatorSource (null , MultiValueMode .MIN , null ) {
231+ @ Override
232+ public SortField .Type reducedType () {
233+ return SortField .Type .STRING ;
234+ }
235+
236+ @ Override
237+ public FieldComparator <?> newComparator (String fieldname , int numHits , int sortPos , boolean reversed ) {
238+ return null ;
239+ }
240+ };
241+
242+ type = extractSortType (new SortField ("field" , source ));
243+ assertThat (type , equalTo (SortField .Type .STRING ));
244+
245+ type = extractSortType (new SortedNumericSortField ("field" , SortField .Type .DOUBLE ));
246+ assertThat (type , equalTo (SortField .Type .DOUBLE ));
247+
248+ type = extractSortType (new SortedSetSortField ("field" , false ));
249+ assertThat (type , equalTo (SortField .Type .STRING ));
250+ }
218251}
0 commit comments