Skip to content

Commit

Permalink
test: Use a single primary shard so that the exception can caught in …
Browse files Browse the repository at this point in the history
…the same way
  • Loading branch information
martijnvg committed Sep 19, 2017
1 parent 2567210 commit 332b4d1
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexSettings;
Expand Down Expand Up @@ -386,6 +387,9 @@ public void testNestedDefinedAsObject() throws Exception {

public void testInnerHitsWithObjectFieldThatHasANestedField() throws Exception {
assertAcked(prepareCreate("articles")
// number_of_shards = 1, because then we catch the expected exception in the same way.
// (See expectThrows(...) below)
.setSettings(Settings.builder().put("index.number_of_shards", 1))
.addMapping("article", jsonBuilder().startObject()
.startObject("properties")
.startObject("comments")
Expand Down Expand Up @@ -418,18 +422,18 @@ public void testInnerHitsWithObjectFieldThatHasANestedField() throws Exception {
.endObject()));
indexRandom(true, requests);

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

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

response = client().prepareSearch("articles")
SearchResponse response = client().prepareSearch("articles")
.setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "fox"), ScoreMode.Avg)
.innerHit(new InnerHitBuilder().setFetchSourceContext(new FetchSourceContext(false)))).get();
assertNoFailures(response);
Expand Down

0 comments on commit 332b4d1

Please sign in to comment.