Search in sources :

Example 1 with TopHits

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.

the class ChildrenIT method testParentWithMultipleBuckets.

public void testParentWithMultipleBuckets() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("randomized", false)).addAggregation(terms("category").field("category").size(10000).subAggregation(children("to_comment", "comment").subAggregation(topHits("top_comments").sort("_uid", SortOrder.ASC)))).get();
    assertSearchResponse(searchResponse);
    Terms categoryTerms = searchResponse.getAggregations().get("category");
    assertThat(categoryTerms.getBuckets().size(), equalTo(3));
    for (Terms.Bucket bucket : categoryTerms.getBuckets()) {
        logger.info("bucket={}", bucket.getKey());
        Children childrenBucket = bucket.getAggregations().get("to_comment");
        TopHits topHits = childrenBucket.getAggregations().get("top_comments");
        logger.info("total_hits={}", topHits.getHits().getTotalHits());
        for (SearchHit searchHit : topHits.getHits()) {
            logger.info("hit= {} {} {}", searchHit.getSortValues()[0], searchHit.getType(), searchHit.getId());
        }
    }
    Terms.Bucket categoryBucket = categoryTerms.getBucketByKey("a");
    assertThat(categoryBucket.getKeyAsString(), equalTo("a"));
    assertThat(categoryBucket.getDocCount(), equalTo(3L));
    Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(2L));
    TopHits topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().getTotalHits(), equalTo(2L));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("a"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
    assertThat(topHits.getHits().getAt(1).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(1).getType(), equalTo("comment"));
    categoryBucket = categoryTerms.getBucketByKey("b");
    assertThat(categoryBucket.getKeyAsString(), equalTo("b"));
    assertThat(categoryBucket.getDocCount(), equalTo(2L));
    childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(1L));
    topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().getTotalHits(), equalTo(1L));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
    categoryBucket = categoryTerms.getBucketByKey("c");
    assertThat(categoryBucket.getKeyAsString(), equalTo("c"));
    assertThat(categoryBucket.getDocCount(), equalTo(2L));
    childrenBucket = categoryBucket.getAggregations().get("to_comment");
    assertThat(childrenBucket.getName(), equalTo("to_comment"));
    assertThat(childrenBucket.getDocCount(), equalTo(1L));
    topHits = childrenBucket.getAggregations().get("top_comments");
    assertThat(topHits.getHits().getTotalHits(), equalTo(1L));
    assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
    assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHit(org.elasticsearch.search.SearchHit) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Children(org.elasticsearch.search.aggregations.bucket.children.Children) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with TopHits

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits 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"));
}
Also used : Script(org.elasticsearch.script.Script) SearchHit(org.elasticsearch.search.SearchHit) Explanation(org.apache.lucene.search.Explanation) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 3 with TopHits

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.

the class TopHitsIT method testBreadthFirstWithScoreNeeded.

public void testBreadthFirstWithScoreNeeded() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).collectMode(SubAggCollectionMode.BREADTH_FIRST).field(TERMS_AGGS_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));
    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));
        assertThat(hits.getAt(0).getSourceAsMap().size(), equalTo(4));
    }
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with TopHits

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.

the class TopHitsIT method testPagination.

public void testPagination() throws Exception {
    int size = randomIntBetween(1, 10);
    int from = randomIntBetween(0, 10);
    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)).from(from).size(size))).get();
    assertSearchResponse(response);
    SearchResponse control = client().prepareSearch("idx").setTypes("type").setFrom(from).setSize(size).setPostFilter(QueryBuilders.termQuery(TERMS_AGGS_FIELD, "val0")).addSort(SORT_FIELD, SortOrder.DESC).get();
    assertSearchResponse(control);
    SearchHits controlHits = control.getHits();
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    Terms.Bucket bucket = terms.getBucketByKey("val0");
    assertThat(bucket, notNullValue());
    assertThat(bucket.getDocCount(), equalTo(10L));
    TopHits topHits = bucket.getAggregations().get("hits");
    SearchHits hits = topHits.getHits();
    assertThat(hits.getTotalHits(), equalTo(controlHits.getTotalHits()));
    assertThat(hits.getHits().length, equalTo(controlHits.getHits().length));
    for (int i = 0; i < hits.getHits().length; i++) {
        logger.info("{}: top_hits: [{}][{}] control: [{}][{}]", i, hits.getAt(i).getId(), hits.getAt(i).getSortValues()[0], controlHits.getAt(i).getId(), controlHits.getAt(i).getSortValues()[0]);
        assertThat(hits.getAt(i).getId(), equalTo(controlHits.getAt(i).getId()));
        assertThat(hits.getAt(i).getSortValues()[0], equalTo(controlHits.getAt(i).getSortValues()[0]));
    }
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with TopHits

use of org.elasticsearch.search.aggregations.metrics.tophits.TopHits in project elasticsearch by elastic.

the class TopHitsIT method testFieldCollapsing.

public void testFieldCollapsing() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("field-collapsing").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(matchQuery("text", "term rare")).addAggregation(terms("terms").executionHint(randomExecutionHint()).field("group").order(Terms.Order.aggregation("max_score", false)).subAggregation(topHits("hits").size(1)).subAggregation(max("max_score").field("value"))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(3));
    Iterator<Terms.Bucket> bucketIterator = terms.getBuckets().iterator();
    Terms.Bucket bucket = bucketIterator.next();
    assertThat(key(bucket), equalTo("b"));
    TopHits topHits = bucket.getAggregations().get("hits");
    SearchHits hits = topHits.getHits();
    assertThat(hits.getTotalHits(), equalTo(4L));
    assertThat(hits.getHits().length, equalTo(1));
    assertThat(hits.getAt(0).getId(), equalTo("6"));
    bucket = bucketIterator.next();
    assertThat(key(bucket), equalTo("c"));
    topHits = bucket.getAggregations().get("hits");
    hits = topHits.getHits();
    assertThat(hits.getTotalHits(), equalTo(3L));
    assertThat(hits.getHits().length, equalTo(1));
    assertThat(hits.getAt(0).getId(), equalTo("9"));
    bucket = bucketIterator.next();
    assertThat(key(bucket), equalTo("a"));
    topHits = bucket.getAggregations().get("hits");
    hits = topHits.getHits();
    assertThat(hits.getTotalHits(), equalTo(2L));
    assertThat(hits.getHits().length, equalTo(1));
    assertThat(hits.getAt(0).getId(), equalTo("2"));
}
Also used : TopHits(org.elasticsearch.search.aggregations.metrics.tophits.TopHits) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)18 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)18 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)18 SearchHits (org.elasticsearch.search.SearchHits)13 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)13 SearchHit (org.elasticsearch.search.SearchHit)5 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)4 SearchHitField (org.elasticsearch.search.SearchHitField)3 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)3 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)3 Explanation (org.apache.lucene.search.Explanation)2 Script (org.elasticsearch.script.Script)2 Children (org.elasticsearch.search.aggregations.bucket.children.Children)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Global (org.elasticsearch.search.aggregations.bucket.global.Global)1 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)1 Max (org.elasticsearch.search.aggregations.metrics.max.Max)1