use of org.elasticsearch.search.SearchHits 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.SearchHits 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--;
}
}
use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.
the class TopHitsIT method testSortByBucket.
public void testSortByBucket() throws Exception {
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).order(Terms.Order.aggregation("max_sort", false)).subAggregation(topHits("hits").sort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC)).trackScores(true)).subAggregation(max("max_sort").field(SORT_FIELD))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
long higestSortValue = 50;
int currentBucket = 4;
for (Terms.Bucket bucket : terms.getBuckets()) {
assertThat(key(bucket), equalTo("val" + currentBucket--));
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((Long) hits.getAt(0).getSortValues()[0], equalTo(higestSortValue));
assertThat((Long) hits.getAt(1).getSortValues()[0], equalTo(higestSortValue - 1));
assertThat((Long) hits.getAt(2).getSortValues()[0], equalTo(higestSortValue - 2));
Max max = bucket.getAggregations().get("max_sort");
assertThat(max.getValue(), equalTo(((Long) higestSortValue).doubleValue()));
higestSortValue -= 10;
}
}
use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.
the class TopHitsIT method testFetchFeatures.
public void testFetchFeatures() {
SearchResponse response = client().prepareSearch("idx").setTypes("type").setQuery(matchQuery("text", "text").queryName("test")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").size(1).highlighter(new HighlightBuilder().field("text")).explain(true).storedField("text").fieldDataField("field1").scriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).fetchSource("text", null).version(true))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (Terms.Bucket bucket : terms.getBuckets()) {
TopHits topHits = bucket.getAggregations().get("hits");
SearchHits hits = topHits.getHits();
assertThat(hits.getTotalHits(), equalTo(10L));
assertThat(hits.getHits().length, equalTo(1));
SearchHit hit = hits.getAt(0);
HighlightField highlightField = hit.getHighlightFields().get("text");
assertThat(highlightField.getFragments().length, equalTo(1));
assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>text</em> to entertain"));
Explanation explanation = hit.getExplanation();
assertThat(explanation.toString(), containsString("text:text"));
long version = hit.getVersion();
assertThat(version, equalTo(1L));
assertThat(hit.getMatchedQueries()[0], equalTo("test"));
SearchHitField field = hit.field("field1");
assertThat(field.getValue().toString(), equalTo("5"));
assertThat(hit.getSourceAsMap().get("text").toString(), equalTo("some text to entertain"));
field = hit.field("script");
assertThat(field.getValue().toString(), equalTo("5"));
assertThat(hit.getSourceAsMap().size(), equalTo(1));
assertThat(hit.getSourceAsMap().get("text").toString(), equalTo("some text to entertain"));
}
}
use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.
the class TopHitsIT method testBasics.
public void testBasics() throws Exception {
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(TERMS_AGGS_FIELD).subAggregation(topHits("hits").sort(SortBuilders.fieldSort(SORT_FIELD).order(SortOrder.DESC)))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
long higestSortValue = 0;
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));
higestSortValue += 10;
assertThat((Long) hits.getAt(0).getSortValues()[0], equalTo(higestSortValue));
assertThat((Long) hits.getAt(1).getSortValues()[0], equalTo(higestSortValue - 1));
assertThat((Long) hits.getAt(2).getSortValues()[0], equalTo(higestSortValue - 2));
assertThat(hits.getAt(0).getSourceAsMap().size(), equalTo(4));
}
}
Aggregations