Search in sources :

Example 66 with Terms

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

the class StringTermsIT method testSingleValueFieldOrderedByTermDesc.

public void testSingleValueFieldOrderedByTermDesc() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.term(false))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    int i = 4;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        assertThat(bucket.getDocCount(), equalTo(1L));
        i--;
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 67 with Terms

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

the class StringTermsIT method runTestFieldWithPartitionedFiltering.

private void runTestFieldWithPartitionedFiltering(String field) throws Exception {
    // Find total number of unique terms
    SearchResponse allResponse = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(field).size(10000).collectMode(randomFrom(SubAggCollectionMode.values()))).execute().actionGet();
    assertSearchResponse(allResponse);
    Terms terms = allResponse.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    int expectedCardinality = terms.getBuckets().size();
    // Gather terms using partitioned aggregations
    final int numPartitions = randomIntBetween(2, 4);
    Set<String> foundTerms = new HashSet<>();
    for (int partition = 0; partition < numPartitions; partition++) {
        SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(field).includeExclude(new IncludeExclude(partition, numPartitions)).collectMode(randomFrom(SubAggCollectionMode.values()))).execute().actionGet();
        assertSearchResponse(response);
        terms = response.getAggregations().get("terms");
        assertThat(terms, notNullValue());
        assertThat(terms.getName(), equalTo("terms"));
        for (Bucket bucket : terms.getBuckets()) {
            assertTrue(foundTerms.add(bucket.getKeyAsString()));
        }
    }
    assertEquals(expectedCardinality, foundTerms.size());
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) HashSet(java.util.HashSet)

Example 68 with Terms

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

the class StringTermsIT method testSingleValueField.

public void testSingleValueField() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values()))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    Object[] propertiesKeys = (Object[]) terms.getProperty("_key");
    Object[] propertiesDocCounts = (Object[]) terms.getProperty("_count");
    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(1L));
        assertThat((String) propertiesKeys[i], equalTo("val" + i));
        assertThat((long) propertiesDocCounts[i], equalTo(1L));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 69 with Terms

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

the class StringTermsIT method testSingleValuedFieldWithValueScript.

public void testSingleValuedFieldWithValueScript() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "'foo_' + _value", Collections.emptyMap()))).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("foo_val" + i);
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("foo_val" + i));
        assertThat(bucket.getDocCount(), equalTo(1L));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Script(org.elasticsearch.script.Script) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 70 with Terms

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

the class StringTermsIT method testMultiValuedField.

public void testMultiValuedField() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values()))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(6));
    for (int i = 0; i < 6; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("val" + i);
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        if (i == 0 || i == 5) {
            assertThat(bucket.getDocCount(), equalTo(1L));
        } else {
            assertThat(bucket.getDocCount(), equalTo(2L));
        }
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) 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)252 SearchResponse (org.elasticsearch.action.search.SearchResponse)238 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)209 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)97 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)37 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)36 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)30 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)26 Script (org.elasticsearch.script.Script)22 ArrayList (java.util.ArrayList)21 HashMap (java.util.HashMap)21 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)16 ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)16 SearchHits (org.elasticsearch.search.SearchHits)13 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)13 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)13 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)12 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)11 Sampler (org.elasticsearch.search.aggregations.bucket.sampler.Sampler)10 PipelineAggregatorBuilders.percentilesBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.percentilesBucket)9