use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits 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.metrics.tophits.TopHits in project elasticsearch by elastic.
the class TopHitsIT method testNoStoredFields.
public void testNoStoredFields() throws Exception {
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").storedField("_none_"))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("val" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + i));
assertThat(bucket.getDocCount(), equalTo(10L));
TopHits topHits = bucket.getAggregations().get("hits");
SearchHits hits = topHits.getHits();
assertThat(hits.getTotalHits(), equalTo(10L));
assertThat(hits.getHits().length, equalTo(3));
for (SearchHit hit : hits) {
assertThat(hit.getSourceAsMap(), nullValue());
assertThat(hit.getId(), nullValue());
assertThat(hit.getType(), nullValue());
}
}
}
use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.
the class TopHitsIT method testBasicsGetProperty.
public void testBasicsGetProperty() throws Exception {
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(topHits("hits"))).execute().actionGet();
assertSearchResponse(searchResponse);
Global global = searchResponse.getAggregations().get("global");
assertThat(global, notNullValue());
assertThat(global.getName(), equalTo("global"));
assertThat(global.getAggregations(), notNullValue());
assertThat(global.getAggregations().asMap().size(), equalTo(1));
TopHits topHits = global.getAggregations().get("hits");
assertThat(topHits, notNullValue());
assertThat(topHits.getName(), equalTo("hits"));
assertThat((TopHits) global.getProperty("hits"), sameInstance(topHits));
}
use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.
the class TopHitsIT method testIssue11119.
public void testIssue11119() throws Exception {
// Test that top_hits aggregation is fed scores if query results size=0
SearchResponse response = client().prepareSearch("idx").setTypes("field-collapsing").setSize(0).setQuery(matchQuery("text", "x y z")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field("group").subAggregation(topHits("hits"))).get();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(8L));
assertThat(response.getHits().getHits().length, equalTo(0));
assertThat(response.getHits().getMaxScore(), equalTo(0f));
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(3));
for (Terms.Bucket bucket : terms.getBuckets()) {
assertThat(bucket, notNullValue());
TopHits topHits = bucket.getAggregations().get("hits");
SearchHits hits = topHits.getHits();
float bestScore = Float.MAX_VALUE;
for (int h = 0; h < hits.getHits().length; h++) {
float score = hits.getAt(h).getScore();
assertThat(score, lessThanOrEqualTo(bestScore));
assertThat(score, greaterThan(0f));
bestScore = hits.getAt(h).getScore();
}
}
// Also check that min_score setting works when size=0
// (technically not a test of top_hits but implementation details are
// tied up with the need to feed scores into the agg tree even when
// users don't want ranked set of query results.)
response = client().prepareSearch("idx").setTypes("field-collapsing").setSize(0).setMinScore(0.0001f).setQuery(matchQuery("text", "x y z")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field("group")).get();
assertSearchResponse(response);
assertThat(response.getHits().getTotalHits(), equalTo(8L));
assertThat(response.getHits().getHits().length, equalTo(0));
assertThat(response.getHits().getMaxScore(), equalTo(0f));
terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(3));
}
use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.
the class TopHitsIT method testBreadthFirstWithAggOrderAndScoreNeeded.
public void testBreadthFirstWithAggOrderAndScoreNeeded() throws Exception {
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).collectMode(SubAggCollectionMode.BREADTH_FIRST).field(TERMS_AGGS_FIELD).order(Terms.Order.aggregation("max", false)).subAggregation(max("max").field(SORT_FIELD)).subAggregation(topHits("hits").size(3))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
int id = 4;
for (Terms.Bucket bucket : terms.getBuckets()) {
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("val" + id));
assertThat(bucket.getDocCount(), equalTo(10L));
TopHits topHits = bucket.getAggregations().get("hits");
SearchHits hits = topHits.getHits();
assertThat(hits.getTotalHits(), equalTo(10L));
assertThat(hits.getHits().length, equalTo(3));
assertThat(hits.getAt(0).getSourceAsMap().size(), equalTo(4));
id--;
}
}
Aggregations