use of org.elasticsearch.search.aggregations.bucket.nested.Nested in project elasticsearch by elastic.
the class ShardReduceIT method testNested.
public void testNested() throws Exception {
SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.matchAllQuery()).addAggregation(nested("nested", "nested").subAggregation(dateHistogram("histo").field("nested.date").dateHistogramInterval(DateHistogramInterval.DAY).minDocCount(0))).execute().actionGet();
assertSearchResponse(response);
Nested nested = response.getAggregations().get("nested");
Histogram histo = nested.getAggregations().get("histo");
assertThat(histo.getBuckets().size(), equalTo(4));
}
use of org.elasticsearch.search.aggregations.bucket.nested.Nested in project elasticsearch by elastic.
the class TopHitsIT method testNestedFetchFeatures.
public void testNestedFetchFeatures() {
String hlType = randomFrom("plain", "fvh", "postings");
HighlightBuilder.Field hlField = new HighlightBuilder.Field("comments.message").highlightQuery(matchQuery("comments.message", "comment")).forceSource(// randomly from stored field or _source
randomBoolean()).highlighterType(hlType);
SearchResponse searchResponse = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "comment").queryName("test"), ScoreMode.Avg)).addAggregation(nested("to-comments", "comments").subAggregation(topHits("top-comments").size(1).highlighter(new HighlightBuilder().field(hlField)).explain(true).fieldDataField("comments.user").scriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).fetchSource("comments.message", null).version(true).sort("comments.date", SortOrder.ASC))).get();
assertHitCount(searchResponse, 2);
Nested nested = searchResponse.getAggregations().get("to-comments");
assertThat(nested.getDocCount(), equalTo(4L));
SearchHits hits = ((TopHits) nested.getAggregations().get("top-comments")).getHits();
assertThat(hits.getTotalHits(), equalTo(4L));
SearchHit searchHit = hits.getAt(0);
assertThat(searchHit.getId(), equalTo("1"));
assertThat(searchHit.getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHit.getNestedIdentity().getOffset(), equalTo(0));
HighlightField highlightField = searchHit.getHighlightFields().get("comments.message");
assertThat(highlightField.getFragments().length, equalTo(1));
assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>comment</em>"));
// Can't explain nested hit with the main query, since both are in a different scopes, also the nested doc may not even have matched with the main query
// If top_hits would have a query option then we can explain that query
Explanation explanation = searchHit.getExplanation();
assertFalse(explanation.isMatch());
// Returns the version of the root document. Nested docs don't have a separate version
long version = searchHit.getVersion();
assertThat(version, equalTo(1L));
assertThat(searchHit.getMatchedQueries(), arrayContaining("test"));
SearchHitField field = searchHit.field("comments.user");
assertThat(field.getValue().toString(), equalTo("a"));
field = searchHit.field("script");
assertThat(field.getValue().toString(), equalTo("5"));
assertThat(searchHit.getSourceAsMap().size(), equalTo(1));
assertThat(XContentMapValues.extractValue("comments.message", searchHit.getSourceAsMap()), equalTo("some comment"));
}
use of org.elasticsearch.search.aggregations.bucket.nested.Nested in project elasticsearch by elastic.
the class TopHitsIT method testTopHitsInNested.
public void testTopHitsInNested() throws Exception {
SearchResponse searchResponse = client().prepareSearch("articles").addAggregation(histogram("dates").field("date").interval(5).order(Histogram.Order.aggregation("to-comments", true)).subAggregation(nested("to-comments", "comments").subAggregation(topHits("comments").highlighter(new HighlightBuilder().field(new HighlightBuilder.Field("comments.message").highlightQuery(matchQuery("comments.message", "text")))).sort("comments.id", SortOrder.ASC)))).get();
Histogram histogram = searchResponse.getAggregations().get("dates");
for (int i = 0; i < numArticles; i += 5) {
Histogram.Bucket bucket = histogram.getBuckets().get(i / 5);
assertThat(bucket.getDocCount(), equalTo(5L));
long numNestedDocs = 10 + (5 * i);
Nested nested = bucket.getAggregations().get("to-comments");
assertThat(nested.getDocCount(), equalTo(numNestedDocs));
TopHits hits = nested.getAggregations().get("comments");
SearchHits searchHits = hits.getHits();
assertThat(searchHits.getTotalHits(), equalTo(numNestedDocs));
for (int j = 0; j < 3; j++) {
assertThat(searchHits.getAt(j).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(j).getNestedIdentity().getOffset(), equalTo(0));
assertThat((Integer) searchHits.getAt(j).getSourceAsMap().get("id"), equalTo(0));
HighlightField highlightField = searchHits.getAt(j).getHighlightFields().get("comments.message");
assertThat(highlightField.getFragments().length, equalTo(1));
assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em>"));
}
}
}
use of org.elasticsearch.search.aggregations.bucket.nested.Nested in project elasticsearch by elastic.
the class TopHitsIT method testTopHitsInSecondLayerNested.
public void testTopHitsInSecondLayerNested() throws Exception {
SearchResponse searchResponse = client().prepareSearch("articles").setQuery(matchQuery("title", "title")).addAggregation(nested("to-comments", "comments").subAggregation(nested("to-reviewers", "comments.reviewers").subAggregation(// Also need to sort on _doc because there are two reviewers with the same name
topHits("top-reviewers").sort("comments.reviewers.name", SortOrder.ASC).sort("_doc", SortOrder.DESC).size(7))).subAggregation(topHits("top-comments").sort("comments.date", SortOrder.DESC).size(4))).get();
assertNoFailures(searchResponse);
Nested toComments = searchResponse.getAggregations().get("to-comments");
assertThat(toComments.getDocCount(), equalTo(4L));
TopHits topComments = toComments.getAggregations().get("top-comments");
assertThat(topComments.getHits().getTotalHits(), equalTo(4L));
assertThat(topComments.getHits().getHits().length, equalTo(4));
assertThat(topComments.getHits().getAt(0).getId(), equalTo("2"));
assertThat(topComments.getHits().getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topComments.getHits().getAt(0).getNestedIdentity().getOffset(), equalTo(1));
assertThat(topComments.getHits().getAt(0).getNestedIdentity().getChild(), nullValue());
assertThat(topComments.getHits().getAt(1).getId(), equalTo("2"));
assertThat(topComments.getHits().getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topComments.getHits().getAt(1).getNestedIdentity().getOffset(), equalTo(0));
assertThat(topComments.getHits().getAt(1).getNestedIdentity().getChild(), nullValue());
assertThat(topComments.getHits().getAt(2).getId(), equalTo("1"));
assertThat(topComments.getHits().getAt(2).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topComments.getHits().getAt(2).getNestedIdentity().getOffset(), equalTo(1));
assertThat(topComments.getHits().getAt(2).getNestedIdentity().getChild(), nullValue());
assertThat(topComments.getHits().getAt(3).getId(), equalTo("1"));
assertThat(topComments.getHits().getAt(3).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topComments.getHits().getAt(3).getNestedIdentity().getOffset(), equalTo(0));
assertThat(topComments.getHits().getAt(3).getNestedIdentity().getChild(), nullValue());
Nested toReviewers = toComments.getAggregations().get("to-reviewers");
assertThat(toReviewers.getDocCount(), equalTo(7L));
TopHits topReviewers = toReviewers.getAggregations().get("top-reviewers");
assertThat(topReviewers.getHits().getTotalHits(), equalTo(7L));
assertThat(topReviewers.getHits().getHits().length, equalTo(7));
assertThat(topReviewers.getHits().getAt(0).getId(), equalTo("1"));
assertThat((String) topReviewers.getHits().getAt(0).getSourceAsMap().get("name"), equalTo("user a"));
assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getOffset(), equalTo(0));
assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0));
assertThat(topReviewers.getHits().getAt(1).getId(), equalTo("1"));
assertThat((String) topReviewers.getHits().getAt(1).getSourceAsMap().get("name"), equalTo("user b"));
assertThat(topReviewers.getHits().getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topReviewers.getHits().getAt(1).getNestedIdentity().getOffset(), equalTo(0));
assertThat(topReviewers.getHits().getAt(1).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
assertThat(topReviewers.getHits().getAt(1).getNestedIdentity().getChild().getOffset(), equalTo(1));
assertThat(topReviewers.getHits().getAt(2).getId(), equalTo("1"));
assertThat((String) topReviewers.getHits().getAt(2).getSourceAsMap().get("name"), equalTo("user c"));
assertThat(topReviewers.getHits().getAt(2).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topReviewers.getHits().getAt(2).getNestedIdentity().getOffset(), equalTo(0));
assertThat(topReviewers.getHits().getAt(2).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
assertThat(topReviewers.getHits().getAt(2).getNestedIdentity().getChild().getOffset(), equalTo(2));
assertThat(topReviewers.getHits().getAt(3).getId(), equalTo("1"));
assertThat((String) topReviewers.getHits().getAt(3).getSourceAsMap().get("name"), equalTo("user c"));
assertThat(topReviewers.getHits().getAt(3).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topReviewers.getHits().getAt(3).getNestedIdentity().getOffset(), equalTo(1));
assertThat(topReviewers.getHits().getAt(3).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
assertThat(topReviewers.getHits().getAt(3).getNestedIdentity().getChild().getOffset(), equalTo(0));
assertThat(topReviewers.getHits().getAt(4).getId(), equalTo("1"));
assertThat((String) topReviewers.getHits().getAt(4).getSourceAsMap().get("name"), equalTo("user d"));
assertThat(topReviewers.getHits().getAt(4).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topReviewers.getHits().getAt(4).getNestedIdentity().getOffset(), equalTo(1));
assertThat(topReviewers.getHits().getAt(4).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
assertThat(topReviewers.getHits().getAt(4).getNestedIdentity().getChild().getOffset(), equalTo(1));
assertThat(topReviewers.getHits().getAt(5).getId(), equalTo("1"));
assertThat((String) topReviewers.getHits().getAt(5).getSourceAsMap().get("name"), equalTo("user e"));
assertThat(topReviewers.getHits().getAt(5).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topReviewers.getHits().getAt(5).getNestedIdentity().getOffset(), equalTo(1));
assertThat(topReviewers.getHits().getAt(5).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
assertThat(topReviewers.getHits().getAt(5).getNestedIdentity().getChild().getOffset(), equalTo(2));
assertThat(topReviewers.getHits().getAt(6).getId(), equalTo("2"));
assertThat((String) topReviewers.getHits().getAt(6).getSourceAsMap().get("name"), equalTo("user f"));
assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getOffset(), equalTo(0));
assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getChild().getField().string(), equalTo("reviewers"));
assertThat(topReviewers.getHits().getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0));
}
use of org.elasticsearch.search.aggregations.bucket.nested.Nested in project elasticsearch by elastic.
the class TopHitsIT method testTopHitsInNestedSimple.
public void testTopHitsInNestedSimple() throws Exception {
SearchResponse searchResponse = client().prepareSearch("articles").setQuery(matchQuery("title", "title")).addAggregation(nested("to-comments", "comments").subAggregation(terms("users").field("comments.user").subAggregation(topHits("top-comments").sort("comments.date", SortOrder.ASC)))).get();
Nested nested = searchResponse.getAggregations().get("to-comments");
assertThat(nested.getDocCount(), equalTo(4L));
Terms terms = nested.getAggregations().get("users");
Terms.Bucket bucket = terms.getBucketByKey("a");
assertThat(bucket.getDocCount(), equalTo(1L));
TopHits topHits = bucket.getAggregations().get("top-comments");
SearchHits searchHits = topHits.getHits();
assertThat(searchHits.getTotalHits(), equalTo(1L));
assertThat(searchHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
assertThat((Integer) searchHits.getAt(0).getSourceAsMap().get("date"), equalTo(1));
bucket = terms.getBucketByKey("b");
assertThat(bucket.getDocCount(), equalTo(2L));
topHits = bucket.getAggregations().get("top-comments");
searchHits = topHits.getHits();
assertThat(searchHits.getTotalHits(), equalTo(2L));
assertThat(searchHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(0).getNestedIdentity().getOffset(), equalTo(1));
assertThat((Integer) searchHits.getAt(0).getSourceAsMap().get("date"), equalTo(2));
assertThat(searchHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(1).getNestedIdentity().getOffset(), equalTo(0));
assertThat((Integer) searchHits.getAt(1).getSourceAsMap().get("date"), equalTo(3));
bucket = terms.getBucketByKey("c");
assertThat(bucket.getDocCount(), equalTo(1L));
topHits = bucket.getAggregations().get("top-comments");
searchHits = topHits.getHits();
assertThat(searchHits.getTotalHits(), equalTo(1L));
assertThat(searchHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(0).getNestedIdentity().getOffset(), equalTo(1));
assertThat((Integer) searchHits.getAt(0).getSourceAsMap().get("date"), equalTo(4));
}
Aggregations