From ef1f834cf90f31fa3baa3c1d16a31ebcf6827dc8 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Tue, 31 Mar 2015 22:42:44 +0200 Subject: [PATCH] inner hits: Make sure inner hits also work for nested fields defined in object field Closes #10334 --- .../search/fetch/FetchPhase.java | 8 +++- .../search/innerhits/InnerHitsTests.java | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java b/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java index 54e1d13b9fa..60807526e22 100644 --- a/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java +++ b/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java @@ -376,8 +376,14 @@ public class FetchPhase implements SearchPhase { String field; Filter parentFilter; nestedParentObjectMapper = documentMapper.findParentObjectMapper(nestedObjectMapper); - if (nestedParentObjectMapper != null && nestedObjectMapper.nested().isNested()) { + if (nestedParentObjectMapper != null) { field = nestedObjectMapper.name(); + if (!nestedParentObjectMapper.nested().isNested()) { + nestedObjectMapper = nestedParentObjectMapper; + // all right, the parent is a normal object field, so this is the best identiy we can give for that: + nestedIdentity = new InternalSearchHit.InternalNestedIdentity(field, 0, nestedIdentity); + continue; + } parentFilter = nestedParentObjectMapper.nestedTypeFilter(); } else { field = nestedObjectMapper.fullPath(); diff --git a/src/test/java/org/elasticsearch/search/innerhits/InnerHitsTests.java b/src/test/java/org/elasticsearch/search/innerhits/InnerHitsTests.java index def6eed468f..7300331cab2 100644 --- a/src/test/java/org/elasticsearch/search/innerhits/InnerHitsTests.java +++ b/src/test/java/org/elasticsearch/search/innerhits/InnerHitsTests.java @@ -846,4 +846,42 @@ public class InnerHitsTests extends ElasticsearchIntegrationTest { assertThat(String.valueOf(response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).highlightFields().get("comments.message").getFragments()[0]), equalTo("fox eat quick")); } + @Test + public void testInnerHitsWithObjectFieldThatHasANestedField() throws Exception { + assertAcked(prepareCreate("articles") + .addMapping("article", jsonBuilder().startObject() + .startObject("properties") + .startObject("comments") + .field("type", "object") + .startObject("properties") + .startObject("messages").field("type", "nested").endObject() + .endObject() + .endObject() + .endObject() + .endObject() + ) + ); + + List requests = new ArrayList<>(); + requests.add(client().prepareIndex("articles", "article", "1").setSource(jsonBuilder().startObject() + .field("title", "quick brown fox") + .startObject("comments").startObject("messages").field("message", "fox eat quick").endObject().endObject() + .endObject())); + indexRandom(true, requests); + + SearchResponse response = client().prepareSearch("articles") + .setQuery(nestedQuery("comments.messages", matchQuery("comments.messages.message", "fox")).innerHit(new QueryInnerHitBuilder())) + .get(); + assertNoFailures(response); + assertHitCount(response, 1); + assertThat(response.getHits().getAt(0).id(), equalTo("1")); + assertThat(response.getHits().getAt(0).getInnerHits().get("comments.messages").getTotalHits(), equalTo(1l)); + assertThat(response.getHits().getAt(0).getInnerHits().get("comments.messages").getAt(0).id(), equalTo("1")); + assertThat(response.getHits().getAt(0).getInnerHits().get("comments.messages").getAt(0).getNestedIdentity().getField().string(), equalTo("comments")); + assertThat(response.getHits().getAt(0).getInnerHits().get("comments.messages").getAt(0).getNestedIdentity().getOffset(), equalTo(0)); + assertThat(response.getHits().getAt(0).getInnerHits().get("comments.messages").getAt(0).getNestedIdentity().getChild().getField().string(), equalTo("messages")); + assertThat(response.getHits().getAt(0).getInnerHits().get("comments.messages").getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0)); + assertThat(response.getHits().getAt(0).getInnerHits().get("comments.messages").getAt(0).getNestedIdentity().getChild().getChild(), nullValue()); + } + }