Search in sources :

Example 1 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class OldIndexBackwardsCompatibilityIT method assertBasicAggregationWorks.

void assertBasicAggregationWorks(String indexName) {
    // histogram on a long
    SearchResponse searchRsp = client().prepareSearch(indexName).addAggregation(AggregationBuilders.histogram("histo").field("long_sort").interval(10)).get();
    ElasticsearchAssertions.assertSearchResponse(searchRsp);
    Histogram histo = searchRsp.getAggregations().get("histo");
    assertNotNull(histo);
    long totalCount = 0;
    for (Histogram.Bucket bucket : histo.getBuckets()) {
        totalCount += bucket.getDocCount();
    }
    assertEquals(totalCount, searchRsp.getHits().getTotalHits());
    // terms on a boolean
    searchRsp = client().prepareSearch(indexName).addAggregation(AggregationBuilders.terms("bool_terms").field("bool")).get();
    Terms terms = searchRsp.getAggregations().get("bool_terms");
    totalCount = 0;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        totalCount += bucket.getDocCount();
    }
    assertEquals(totalCount, searchRsp.getHits().getTotalHits());
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class SharedSignificantTermsTestMethods method checkSignificantTermsAggregationCorrect.

private static void checkSignificantTermsAggregationCorrect(ESIntegTestCase testCase) {
    SearchResponse response = client().prepareSearch(INDEX_NAME).setTypes(DOC_TYPE).addAggregation(terms("class").field(CLASS_FIELD).subAggregation(significantTerms("sig_terms").field(TEXT_FIELD))).execute().actionGet();
    assertSearchResponse(response);
    StringTerms classes = response.getAggregations().get("class");
    Assert.assertThat(classes.getBuckets().size(), equalTo(2));
    for (Terms.Bucket classBucket : classes.getBuckets()) {
        Map<String, Aggregation> aggs = classBucket.getAggregations().asMap();
        Assert.assertTrue(aggs.containsKey("sig_terms"));
        SignificantTerms agg = (SignificantTerms) aggs.get("sig_terms");
        Assert.assertThat(agg.getBuckets().size(), equalTo(1));
        SignificantTerms.Bucket sigBucket = agg.iterator().next();
        String term = sigBucket.getKeyAsString();
        String classTerm = classBucket.getKeyAsString();
        Assert.assertTrue(term.equals(classTerm));
    }
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) AggregationBuilders.significantTerms(org.elasticsearch.search.aggregations.AggregationBuilders.significantTerms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class CopyToMapperIntegrationIT method testDynamicTemplateCopyTo.

public void testDynamicTemplateCopyTo() throws Exception {
    assertAcked(client().admin().indices().prepareCreate("test-idx").addMapping("doc", createDynamicTemplateMapping()));
    int recordCount = between(1, 200);
    for (int i = 0; i < recordCount * 2; i++) {
        client().prepareIndex("test-idx", "doc", Integer.toString(i)).setSource("test_field", "test " + i, "even", i % 2 == 0).get();
    }
    client().admin().indices().prepareRefresh("test-idx").execute().actionGet();
    SubAggCollectionMode aggCollectionMode = randomFrom(SubAggCollectionMode.values());
    SearchResponse response = client().prepareSearch("test-idx").setQuery(QueryBuilders.termQuery("even", true)).addAggregation(AggregationBuilders.terms("test").field("test_field").size(recordCount * 2).collectMode(aggCollectionMode)).addAggregation(AggregationBuilders.terms("test_raw").field("test_field_raw").size(recordCount * 2).collectMode(aggCollectionMode)).execute().actionGet();
    assertThat(response.getHits().getTotalHits(), equalTo((long) recordCount));
    assertThat(((Terms) response.getAggregations().get("test")).getBuckets().size(), equalTo(recordCount + 1));
    assertThat(((Terms) response.getAggregations().get("test_raw")).getBuckets().size(), equalTo(recordCount));
}
Also used : SubAggCollectionMode(org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 4 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class TokenCountFieldMapperIntegrationIT method testFacetByTokenCount.

/**
     * It is possible to search by token count.
     */
public void testFacetByTokenCount() throws IOException {
    init();
    String facetField = randomFrom(Arrays.asList("foo.token_count", "foo.token_count_unstored", "foo.token_count_with_doc_values"));
    SearchResponse result = searchByNumericRange(1, 10).addAggregation(AggregationBuilders.terms("facet").field(facetField)).get();
    assertSearchReturns(result, "single", "bulk1", "bulk2", "multi", "multibulk1", "multibulk2");
    assertThat(result.getAggregations().asList().size(), equalTo(1));
    Terms terms = (Terms) result.getAggregations().asList().get(0);
    assertThat(terms.getBuckets().size(), equalTo(9));
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 5 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class AbstractTermsTestCase method testOtherDocCount.

public void testOtherDocCount(String... fieldNames) {
    for (String fieldName : fieldNames) {
        SearchResponse allTerms = client().prepareSearch("idx").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(fieldName).size(10000).collectMode(randomFrom(SubAggCollectionMode.values()))).get();
        assertSearchResponse(allTerms);
        Terms terms = allTerms.getAggregations().get("terms");
        // size is 0
        assertEquals(0, terms.getSumOfOtherDocCounts());
        final long sumOfDocCounts = sumOfDocCounts(terms);
        final int totalNumTerms = terms.getBuckets().size();
        for (int size = 1; size < totalNumTerms + 2; size += randomIntBetween(1, 5)) {
            for (int shardSize = size; shardSize <= totalNumTerms + 2; shardSize += randomIntBetween(1, 5)) {
                SearchResponse resp = client().prepareSearch("idx").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(fieldName).size(size).shardSize(shardSize).collectMode(randomFrom(SubAggCollectionMode.values()))).get();
                assertSearchResponse(resp);
                terms = resp.getAggregations().get("terms");
                assertEquals(Math.min(size, totalNumTerms), terms.getBuckets().size());
                assertEquals(sumOfDocCounts, sumOfDocCounts(terms));
            }
        }
    }
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)300 SearchResponse (org.elasticsearch.action.search.SearchResponse)268 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)209 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)100 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)38 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)37 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)36 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)26 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)26 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)24 Script (org.elasticsearch.script.Script)22 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)16 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)16 ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)16 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)15 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)15 Aggregation (org.elasticsearch.search.aggregations.Aggregation)14 SearchHits (org.elasticsearch.search.SearchHits)13 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)13