Skip to content

Commit f27e7c6

Browse files
committed
Fix expectations in SearchAfter integration tests (#48372)
This commit fixes the expectations of SearchAfterIT#shouldFail regarding the inner exceptions that should be thrown when testing failures. The exception is sometimes wrapped in a QueryShardException so this change only checks that the toString representation contains the expected message. Closes #43143
1 parent 5ca5987 commit f27e7c6

File tree

1 file changed

+46
-66
lines changed

1 file changed

+46
-66
lines changed

server/src/test/java/org/elasticsearch/search/searchafter/SearchAfterIT.java

Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.action.search.ShardSearchFailure;
2828
import org.elasticsearch.common.UUIDs;
2929
import org.elasticsearch.common.xcontent.XContentBuilder;
30-
import org.elasticsearch.search.SearchContextException;
3130
import org.elasticsearch.search.SearchHit;
3231
import org.elasticsearch.search.sort.SortOrder;
3332
import org.elasticsearch.test.ESIntegTestCase;
@@ -38,11 +37,11 @@
3837
import java.util.Comparator;
3938
import java.util.Collections;
4039
import java.util.Arrays;
41-
import java.util.concurrent.ExecutionException;
4240

4341
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
4442
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
4543
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
44+
import static org.hamcrest.Matchers.containsString;
4645
import static org.hamcrest.Matchers.equalTo;
4746

4847
public class SearchAfterIT extends ESIntegTestCase {
@@ -52,106 +51,87 @@ public class SearchAfterIT extends ESIntegTestCase {
5251

5352
public void testsShouldFail() throws Exception {
5453
assertAcked(client().admin().indices().prepareCreate("test")
55-
.addMapping("type1", "field2", "type=keyword").get());
54+
.addMapping("type1", "field2", "type=keyword")
55+
.get()
56+
);
5657
ensureGreen();
5758
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());
6866
assertTrue(e.shardFailures().length > 0);
6967
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."));
7269
}
7370
}
74-
try {
75-
client().prepareSearch("test")
71+
72+
{
73+
SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearch("test")
7674
.addSort("field1", SortOrder.ASC)
7775
.setQuery(matchAllQuery())
7876
.searchAfter(new Object[]{0})
7977
.setFrom(10)
80-
.get();
81-
82-
fail("Should fail on search_after cannot be used with from > 0.");
83-
} catch (SearchPhaseExecutionException e) {
78+
.get());
8479
assertTrue(e.shardFailures().length > 0);
8580
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."));
8882
}
8983
}
9084

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());
9990
assertTrue(e.shardFailures().length > 0);
10091
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."));
10393
}
10494
}
10595

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());
115103
assertTrue(e.shardFailures().length > 0);
116104
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."));
119106
}
120107
}
121108

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());
130115
for (ShardSearchFailure failure : e.shardFailures()) {
131116
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."));
134118
}
135119
}
136120

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());
146127
assertTrue(e.shardFailures().length > 0);
147128
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]."));
150130
}
151131
}
152132
}
153133

154-
public void testWithNullStrings() throws ExecutionException, InterruptedException {
134+
public void testWithNullStrings() throws Exception {
155135
assertAcked(client().admin().indices().prepareCreate("test")
156136
.addMapping("type1", "field2", "type=keyword").get());
157137
ensureGreen();

0 commit comments

Comments
 (0)