27
27
import org .elasticsearch .action .search .ShardSearchFailure ;
28
28
import org .elasticsearch .common .UUIDs ;
29
29
import org .elasticsearch .common .xcontent .XContentBuilder ;
30
- import org .elasticsearch .search .SearchContextException ;
31
30
import org .elasticsearch .search .SearchHit ;
32
31
import org .elasticsearch .search .sort .SortOrder ;
33
32
import org .elasticsearch .test .ESIntegTestCase ;
38
37
import java .util .Comparator ;
39
38
import java .util .Collections ;
40
39
import java .util .Arrays ;
41
- import java .util .concurrent .ExecutionException ;
42
40
43
41
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
44
42
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
45
43
import static org .elasticsearch .index .query .QueryBuilders .matchAllQuery ;
44
+ import static org .hamcrest .Matchers .containsString ;
46
45
import static org .hamcrest .Matchers .equalTo ;
47
46
48
47
public class SearchAfterIT extends ESIntegTestCase {
@@ -52,106 +51,87 @@ public class SearchAfterIT extends ESIntegTestCase {
52
51
53
52
public void testsShouldFail () throws Exception {
54
53
assertAcked (client ().admin ().indices ().prepareCreate ("test" )
55
- .addMapping ("type1" , "field2" , "type=keyword" ).get ());
54
+ .addMapping ("type1" , "field2" , "type=keyword" )
55
+ .get ()
56
+ );
56
57
ensureGreen ();
57
58
indexRandom (true , client ().prepareIndex ("test" , "type1" , "0" ).setSource ("field1" , 0 , "field2" , "toto" ));
58
- try {
59
- client ().prepareSearch ("test" )
60
- .addSort ("field1" , SortOrder .ASC )
61
- .setQuery (matchAllQuery ())
62
- .searchAfter (new Object []{0 })
63
- .setScroll ("1m" )
64
- .get ();
65
-
66
- fail ("Should fail on search_after cannot be used with scroll." );
67
- } catch (SearchPhaseExecutionException e ) {
59
+ {
60
+ SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () -> client ().prepareSearch ("test" )
61
+ .addSort ("field1" , SortOrder .ASC )
62
+ .setQuery (matchAllQuery ())
63
+ .searchAfter (new Object []{0 })
64
+ .setScroll ("1m" )
65
+ .get ());
68
66
assertTrue (e .shardFailures ().length > 0 );
69
67
for (ShardSearchFailure failure : e .shardFailures ()) {
70
- assertThat (failure .getCause ().getClass (), Matchers .equalTo (SearchContextException .class ));
71
- assertThat (failure .getCause ().getMessage (), Matchers .equalTo ("`search_after` cannot be used in a scroll context." ));
68
+ assertThat (failure .toString (), containsString ("`search_after` cannot be used in a scroll context." ));
72
69
}
73
70
}
74
- try {
75
- client ().prepareSearch ("test" )
71
+
72
+ {
73
+ SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () -> client ().prepareSearch ("test" )
76
74
.addSort ("field1" , SortOrder .ASC )
77
75
.setQuery (matchAllQuery ())
78
76
.searchAfter (new Object []{0 })
79
77
.setFrom (10 )
80
- .get ();
81
-
82
- fail ("Should fail on search_after cannot be used with from > 0." );
83
- } catch (SearchPhaseExecutionException e ) {
78
+ .get ());
84
79
assertTrue (e .shardFailures ().length > 0 );
85
80
for (ShardSearchFailure failure : e .shardFailures ()) {
86
- assertThat (failure .getCause ().getClass (), Matchers .equalTo (SearchContextException .class ));
87
- assertThat (failure .getCause ().getMessage (), Matchers .equalTo ("`from` parameter must be set to 0 when `search_after` is used." ));
81
+ assertThat (failure .toString (), containsString ("`from` parameter must be set to 0 when `search_after` is used." ));
88
82
}
89
83
}
90
84
91
- try {
92
- client ().prepareSearch ("test" )
93
- .setQuery (matchAllQuery ())
94
- .searchAfter (new Object []{0.75f })
95
- .get ();
96
-
97
- fail ("Should fail on search_after on score only is disabled" );
98
- } catch (SearchPhaseExecutionException e ) {
85
+ {
86
+ SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () -> client ().prepareSearch ("test" )
87
+ .setQuery (matchAllQuery ())
88
+ .searchAfter (new Object []{0.75f })
89
+ .get ());
99
90
assertTrue (e .shardFailures ().length > 0 );
100
91
for (ShardSearchFailure failure : e .shardFailures ()) {
101
- assertThat (failure .getCause ().getClass (), Matchers .equalTo (IllegalArgumentException .class ));
102
- assertThat (failure .getCause ().getMessage (), Matchers .equalTo ("Sort must contain at least one field." ));
92
+ assertThat (failure .toString (), containsString ("Sort must contain at least one field." ));
103
93
}
104
94
}
105
95
106
- try {
107
- client ().prepareSearch ("test" )
108
- .addSort ("field2" , SortOrder .DESC )
109
- .addSort ("field1" , SortOrder .ASC )
110
- .setQuery (matchAllQuery ())
111
- .searchAfter (new Object []{1 })
112
- .get ();
113
- fail ("Should fail on search_after size differs from sort field size" );
114
- } catch (SearchPhaseExecutionException e ) {
96
+ {
97
+ SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () -> client ().prepareSearch ("test" )
98
+ .addSort ("field2" , SortOrder .DESC )
99
+ .addSort ("field1" , SortOrder .ASC )
100
+ .setQuery (matchAllQuery ())
101
+ .searchAfter (new Object []{1 })
102
+ .get ());
115
103
assertTrue (e .shardFailures ().length > 0 );
116
104
for (ShardSearchFailure failure : e .shardFailures ()) {
117
- assertThat (failure .getCause ().getClass (), Matchers .equalTo (IllegalArgumentException .class ));
118
- assertThat (failure .getCause ().getMessage (), Matchers .equalTo ("search_after has 1 value(s) but sort has 2." ));
105
+ assertThat (failure .toString (), containsString ("search_after has 1 value(s) but sort has 2." ));
119
106
}
120
107
}
121
108
122
- try {
123
- client ().prepareSearch ("test" )
124
- .setQuery (matchAllQuery ())
125
- .addSort ("field1" , SortOrder .ASC )
126
- .searchAfter (new Object []{1 , 2 })
127
- .get ();
128
- fail ("Should fail on search_after size differs from sort field size" );
129
- } catch (SearchPhaseExecutionException e ) {
109
+ {
110
+ SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () -> client ().prepareSearch ("test" )
111
+ .setQuery (matchAllQuery ())
112
+ .addSort ("field1" , SortOrder .ASC )
113
+ .searchAfter (new Object []{1 , 2 })
114
+ .get ());
130
115
for (ShardSearchFailure failure : e .shardFailures ()) {
131
116
assertTrue (e .shardFailures ().length > 0 );
132
- assertThat (failure .getCause ().getClass (), Matchers .equalTo (IllegalArgumentException .class ));
133
- assertThat (failure .getCause ().getMessage (), Matchers .equalTo ("search_after has 2 value(s) but sort has 1." ));
117
+ assertThat (failure .toString (), containsString ("search_after has 2 value(s) but sort has 1." ));
134
118
}
135
119
}
136
120
137
- try {
138
- client ().prepareSearch ("test" )
139
- .setQuery (matchAllQuery ())
140
- .addSort ("field1" , SortOrder .ASC )
141
- .searchAfter (new Object []{"toto" })
142
- .get ();
143
-
144
- fail ("Should fail on search_after on score only is disabled" );
145
- } catch (SearchPhaseExecutionException e ) {
121
+ {
122
+ SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () -> client ().prepareSearch ("test" )
123
+ .setQuery (matchAllQuery ())
124
+ .addSort ("field1" , SortOrder .ASC )
125
+ .searchAfter (new Object []{"toto" })
126
+ .get ());
146
127
assertTrue (e .shardFailures ().length > 0 );
147
128
for (ShardSearchFailure failure : e .shardFailures ()) {
148
- assertThat (failure .getCause ().getClass (), Matchers .equalTo (IllegalArgumentException .class ));
149
- assertThat (failure .getCause ().getMessage (), Matchers .equalTo ("Failed to parse search_after value for field [field1]." ));
129
+ assertThat (failure .toString (), containsString ("Failed to parse search_after value for field [field1]." ));
150
130
}
151
131
}
152
132
}
153
133
154
- public void testWithNullStrings () throws ExecutionException , InterruptedException {
134
+ public void testWithNullStrings () throws Exception {
155
135
assertAcked (client ().admin ().indices ().prepareCreate ("test" )
156
136
.addMapping ("type1" , "field2" , "type=keyword" ).get ());
157
137
ensureGreen ();
0 commit comments