Search in sources :

Example 1 with ValueCount

use of org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount in project elasticsearch by elastic.

the class StringTermsIT method testSingleValuedFieldWithSubAggregation.

public void testSingleValuedFieldWithSubAggregation() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).subAggregation(count("count").field(MULTI_VALUED_FIELD_NAME))).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");
    Object[] propertiesCounts = (Object[]) terms.getProperty("count.value");
    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));
        ValueCount valueCount = bucket.getAggregations().get("count");
        assertThat(valueCount, notNullValue());
        assertThat(valueCount.getValue(), equalTo(2L));
        assertThat((String) propertiesKeys[i], equalTo("val" + i));
        assertThat((long) propertiesDocCounts[i], equalTo(1L));
        assertThat((double) propertiesCounts[i], equalTo(2.0));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) ValueCount(org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with ValueCount

use of org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount in project elasticsearch by elastic.

the class ValueCountIT method testSingleValuedScriptWithParams.

public void testSingleValuedScriptWithParams() throws Exception {
    Map<String, Object> params = Collections.singletonMap("s", "value");
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(count("count").script(new Script(ScriptType.INLINE, FieldValueScriptEngine.NAME, "", params))).execute().actionGet();
    assertHitCount(searchResponse, 10);
    ValueCount valueCount = searchResponse.getAggregations().get("count");
    assertThat(valueCount, notNullValue());
    assertThat(valueCount.getName(), equalTo("count"));
    assertThat(valueCount.getValue(), equalTo(10L));
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) ValueCount(org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with ValueCount

use of org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount in project elasticsearch by elastic.

the class ValueCountIT method testMultiValuedField.

public void testMultiValuedField() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(count("count").field("values")).execute().actionGet();
    assertHitCount(searchResponse, 10);
    ValueCount valueCount = searchResponse.getAggregations().get("count");
    assertThat(valueCount, notNullValue());
    assertThat(valueCount.getName(), equalTo("count"));
    assertThat(valueCount.getValue(), equalTo(20L));
}
Also used : ValueCount(org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with ValueCount

use of org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount in project elasticsearch by elastic.

the class ValueCountIT method testMultiValuedScript.

public void testMultiValuedScript() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(count("count").script(new Script(ScriptType.INLINE, FieldValueScriptEngine.NAME, "values", Collections.emptyMap()))).execute().actionGet();
    assertHitCount(searchResponse, 10);
    ValueCount valueCount = searchResponse.getAggregations().get("count");
    assertThat(valueCount, notNullValue());
    assertThat(valueCount.getName(), equalTo("count"));
    assertThat(valueCount.getValue(), equalTo(20L));
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) ValueCount(org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with ValueCount

use of org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount in project elasticsearch by elastic.

the class ValueCountIT method testSingleValuedField.

public void testSingleValuedField() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(count("count").field("value")).execute().actionGet();
    assertHitCount(searchResponse, 10);
    ValueCount valueCount = searchResponse.getAggregations().get("count");
    assertThat(valueCount, notNullValue());
    assertThat(valueCount.getName(), equalTo("count"));
    assertThat(valueCount.getValue(), equalTo(10L));
}
Also used : ValueCount(org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)11 ValueCount (org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount)11 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)11 CompiledScript (org.elasticsearch.script.CompiledScript)4 ExecutableScript (org.elasticsearch.script.ExecutableScript)4 LeafSearchScript (org.elasticsearch.script.LeafSearchScript)4 Script (org.elasticsearch.script.Script)4 SearchScript (org.elasticsearch.script.SearchScript)4 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 AggregationBuilders.reverseNested (org.elasticsearch.search.aggregations.AggregationBuilders.reverseNested)1 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)1 Global (org.elasticsearch.search.aggregations.bucket.global.Global)1 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)1 ReverseNested (org.elasticsearch.search.aggregations.bucket.nested.ReverseNested)1 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)1