Skip to content

Commit 332b4d1

Browse files
committed
test: Use a single primary shard so that the exception can caught in the same way
1 parent 2567210 commit 332b4d1

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

core/src/test/java/org/elasticsearch/search/fetch/subphase/InnerHitsIT.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.action.search.SearchPhaseExecutionException;
2727
import org.elasticsearch.action.search.SearchResponse;
2828
import org.elasticsearch.cluster.health.ClusterHealthStatus;
29+
import org.elasticsearch.common.settings.Settings;
2930
import org.elasticsearch.common.xcontent.XContentBuilder;
3031
import org.elasticsearch.common.xcontent.XContentType;
3132
import org.elasticsearch.index.IndexSettings;
@@ -386,6 +387,9 @@ public void testNestedDefinedAsObject() throws Exception {
386387

387388
public void testInnerHitsWithObjectFieldThatHasANestedField() throws Exception {
388389
assertAcked(prepareCreate("articles")
390+
// number_of_shards = 1, because then we catch the expected exception in the same way.
391+
// (See expectThrows(...) below)
392+
.setSettings(Settings.builder().put("index.number_of_shards", 1))
389393
.addMapping("article", jsonBuilder().startObject()
390394
.startObject("properties")
391395
.startObject("comments")
@@ -418,18 +422,18 @@ public void testInnerHitsWithObjectFieldThatHasANestedField() throws Exception {
418422
.endObject()));
419423
indexRandom(true, requests);
420424

421-
SearchResponse response = client().prepareSearch("articles").setQuery(nestedQuery("comments.messages",
422-
matchQuery("comments.messages.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder())).get();
425+
Exception e = expectThrows(Exception.class, () -> client().prepareSearch("articles").setQuery(nestedQuery("comments.messages",
426+
matchQuery("comments.messages.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder())).get());
423427
assertEquals("Cannot execute inner hits. One or more parent object fields of nested field [comments.messages] are " +
424-
"not nested. All parent fields need to be nested fields too", response.getShardFailures()[0].getCause().getMessage());
428+
"not nested. All parent fields need to be nested fields too", e.getCause().getCause().getMessage());
425429

426-
response = client().prepareSearch("articles").setQuery(nestedQuery("comments.messages",
430+
e = expectThrows(Exception.class, () -> client().prepareSearch("articles").setQuery(nestedQuery("comments.messages",
427431
matchQuery("comments.messages.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder()
428-
.setFetchSourceContext(new FetchSourceContext(true)))).get();
432+
.setFetchSourceContext(new FetchSourceContext(true)))).get());
429433
assertEquals("Cannot execute inner hits. One or more parent object fields of nested field [comments.messages] are " +
430-
"not nested. All parent fields need to be nested fields too", response.getShardFailures()[0].getCause().getMessage());
434+
"not nested. All parent fields need to be nested fields too", e.getCause().getCause().getMessage());
431435

432-
response = client().prepareSearch("articles")
436+
SearchResponse response = client().prepareSearch("articles")
433437
.setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "fox"), ScoreMode.Avg)
434438
.innerHit(new InnerHitBuilder().setFetchSourceContext(new FetchSourceContext(false)))).get();
435439
assertNoFailures(response);

0 commit comments

Comments
 (0)