diff --git a/src/main/java/org/elasticsearch/index/search/nested/BlockJoinQuery.java b/src/main/java/org/elasticsearch/index/search/nested/BlockJoinQuery.java index 80825fa4203..f7e9bf92806 100644 --- a/src/main/java/org/elasticsearch/index/search/nested/BlockJoinQuery.java +++ b/src/main/java/org/elasticsearch/index/search/nested/BlockJoinQuery.java @@ -408,9 +408,17 @@ public class BlockJoinQuery extends Query { public Explanation explain(int docBase) throws IOException { int start = docBase + prevParentDoc + 1; // +1 b/c prevParentDoc is previous parent doc int end = docBase + parentDoc - 1; // -1 b/c parentDoc is parent doc - return new ComplexExplanation( - true, score(), String.format(Locale.ROOT, "Score based on child doc range from %d to %d", start, end) + ComplexExplanation explanation = new ComplexExplanation( + true, score(), String.format(Locale.ROOT, "Score based on score mode %s and child doc range from %d to %d", scoreMode, start, end) ); + + for (int i = 0; i < childDocUpto; i++) { + int childDoc = pendingChildDocs[i]; + float childScore = pendingChildScores[i]; + explanation.addDetail(new Explanation(childScore, String.format(Locale.ROOT, "Child[%d]", childDoc))); + } + + return explanation; } } diff --git a/src/test/java/org/elasticsearch/test/integration/nested/SimpleNestedTests.java b/src/test/java/org/elasticsearch/test/integration/nested/SimpleNestedTests.java index 429e86d4057..5c2ea691830 100644 --- a/src/test/java/org/elasticsearch/test/integration/nested/SimpleNestedTests.java +++ b/src/test/java/org/elasticsearch/test/integration/nested/SimpleNestedTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.test.integration.nested; +import org.apache.lucene.search.Explanation; import org.elasticsearch.action.admin.indices.status.IndicesStatusResponse; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; @@ -26,7 +27,6 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Client; import org.elasticsearch.index.query.FilterBuilders; -import org.elasticsearch.index.query.FilteredQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.facet.FacetBuilders; import org.elasticsearch.search.facet.termsstats.TermsStatsFacet; @@ -517,4 +517,48 @@ public class SimpleNestedTests extends AbstractNodesTests { assertThat(client.prepareGet("test", "type1", "1").execute().actionGet().exists(), equalTo(false)); } + @Test + public void testExplain() throws Exception { + client.admin().indices().prepareDelete().execute().actionGet(); + + client.admin().indices().prepareCreate("test") + .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") + .startObject("nested1") + .field("type", "nested") + .endObject() + .endObject().endObject().endObject()) + .execute().actionGet(); + + client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); + + client.prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() + .field("field1", "value1") + .startArray("nested1") + .startObject() + .field("n_field1", "n_value1") + .endObject() + .startObject() + .field("n_field1", "n_value1") + .endObject() + .endArray() + .endObject()) + .setRefresh(true) + .execute().actionGet(); + + SearchResponse searchResponse = client.prepareSearch("test") + .setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1")).scoreMode("total")) + .setExplain(true) + .execute().actionGet(); + assertThat(Arrays.toString(searchResponse.shardFailures()), searchResponse.failedShards(), equalTo(0)); + assertThat(searchResponse.hits().totalHits(), equalTo(1l)); + Explanation explanation = searchResponse.hits().hits()[0].explanation(); + assertThat(explanation.getValue(), equalTo(2f)); + assertThat(explanation.getDescription(), equalTo("Score based on score mode Total and child doc range from 0 to 1")); + assertThat(explanation.getDetails().length, equalTo(2)); + assertThat(explanation.getDetails()[0].getValue(), equalTo(1f)); + assertThat(explanation.getDetails()[0].getDescription(), equalTo("Child[0]")); + assertThat(explanation.getDetails()[1].getValue(), equalTo(1f)); + assertThat(explanation.getDetails()[1].getDescription(), equalTo("Child[1]")); + } + } \ No newline at end of file