Search in sources :

Example 11 with SearchHits

use of org.elasticsearch.search.SearchHits 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 12 with SearchHits

use of org.elasticsearch.search.SearchHits 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 13 with SearchHits

use of org.elasticsearch.search.SearchHits 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)

Example 14 with SearchHits

use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.

the class TopHitsAggregatorTests method testTopLevel.

public void testTopLevel() throws Exception {
    Aggregation result;
    if (randomBoolean()) {
        result = testCase(new MatchAllDocsQuery(), topHits("_name").sort("string", SortOrder.DESC));
    } else {
        Query query = new QueryParser("string", new KeywordAnalyzer()).parse("d^1000 c^100 b^10 a^1");
        result = testCase(query, topHits("_name"));
    }
    SearchHits searchHits = ((TopHits) result).getHits();
    assertEquals(3L, searchHits.getTotalHits());
    assertEquals("3", searchHits.getAt(0).getId());
    assertEquals("type", searchHits.getAt(0).getType());
    assertEquals("2", searchHits.getAt(1).getId());
    assertEquals("type", searchHits.getAt(1).getType());
    assertEquals("1", searchHits.getAt(2).getId());
    assertEquals("type", searchHits.getAt(2).getType());
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SearchHits(org.elasticsearch.search.SearchHits) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 15 with SearchHits

use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.

the class InnerHitsIT method testNestedMultipleLayers.

public void testNestedMultipleLayers() throws Exception {
    assertAcked(prepareCreate("articles").addMapping("article", jsonBuilder().startObject().startObject("article").startObject("properties").startObject("comments").field("type", "nested").startObject("properties").startObject("message").field("type", "text").endObject().startObject("remarks").field("type", "nested").startObject("properties").startObject("message").field("type", "text").endObject().endObject().endObject().endObject().endObject().startObject("title").field("type", "text").endObject().endObject().endObject().endObject()));
    List<IndexRequestBuilder> requests = new ArrayList<>();
    requests.add(client().prepareIndex("articles", "article", "1").setSource(jsonBuilder().startObject().field("title", "quick brown fox").startArray("comments").startObject().field("message", "fox eat quick").startArray("remarks").startObject().field("message", "good").endObject().endArray().endObject().endArray().endObject()));
    requests.add(client().prepareIndex("articles", "article", "2").setSource(jsonBuilder().startObject().field("title", "big gray elephant").startArray("comments").startObject().field("message", "elephant captured").startArray("remarks").startObject().field("message", "bad").endObject().endArray().endObject().endArray().endObject()));
    indexRandom(true, requests);
    SearchResponse response = client().prepareSearch("articles").setQuery(nestedQuery("comments", nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "good"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("remark"), false), ScoreMode.Avg).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("1"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comments");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    innerHits = innerHits.getAt(0).getInnerHits().get("remark");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getId(), equalTo("1"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(innerHits.getAt(0).getNestedIdentity().getChild().getField().string(), equalTo("remarks"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0));
    // Directly refer to the second level:
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad"), ScoreMode.Avg).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("2"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    innerHits = response.getHits().getAt(0).getInnerHits().get("comments.remarks");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(innerHits.getAt(0).getNestedIdentity().getChild().getField().string(), equalTo("remarks"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0));
    response = client().prepareSearch("articles").setQuery(nestedQuery("comments", nestedQuery("comments.remarks", matchQuery("comments.remarks.message", "bad"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("remark"), false), ScoreMode.Avg).innerHit(new InnerHitBuilder(), false)).get();
    assertNoFailures(response);
    assertHitCount(response, 1);
    assertSearchHit(response, 1, hasId("2"));
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    innerHits = response.getHits().getAt(0).getInnerHits().get("comments");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    innerHits = innerHits.getAt(0).getInnerHits().get("remark");
    assertThat(innerHits.getTotalHits(), equalTo(1L));
    assertThat(innerHits.getHits().length, equalTo(1));
    assertThat(innerHits.getAt(0).getId(), equalTo("2"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
    assertThat(innerHits.getAt(0).getNestedIdentity().getChild().getField().string(), equalTo("remarks"));
    assertThat(innerHits.getAt(0).getNestedIdentity().getChild().getOffset(), equalTo(0));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) InnerHitBuilder(org.elasticsearch.index.query.InnerHitBuilder) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchHits (org.elasticsearch.search.SearchHits)136 SearchResponse (org.elasticsearch.action.search.SearchResponse)92 SearchHit (org.elasticsearch.search.SearchHit)86 ArrayList (java.util.ArrayList)45 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)27 IOException (java.io.IOException)24 Map (java.util.Map)23 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 HashMap (java.util.HashMap)19 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)17 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)16 SearchRequest (org.elasticsearch.action.search.SearchRequest)15 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)15 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)13 TotalHits (org.apache.lucene.search.TotalHits)12 Test (org.junit.Test)12 ScoreDoc (org.apache.lucene.search.ScoreDoc)11 Text (org.elasticsearch.common.text.Text)11 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)11 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)10