Search in sources :

Example 11 with Terms

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

the class EquivalenceIT method testDuelTermsHistogram.

// Duel between histograms and scripted terms
public void testDuelTermsHistogram() throws Exception {
    prepareCreate("idx").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("num").field("type", "double").endObject().endObject().endObject().endObject()).execute().actionGet();
    final int numDocs = scaledRandomIntBetween(500, 5000);
    final int maxNumTerms = randomIntBetween(10, 2000);
    final int interval = randomIntBetween(1, 100);
    final Integer[] values = new Integer[maxNumTerms];
    for (int i = 0; i < values.length; ++i) {
        values[i] = randomInt(maxNumTerms * 3) - maxNumTerms;
    }
    for (int i = 0; i < numDocs; ++i) {
        XContentBuilder source = jsonBuilder().startObject().field("num", randomDouble()).startArray("values");
        final int numValues = randomInt(4);
        for (int j = 0; j < numValues; ++j) {
            source = source.value(randomFrom(values));
        }
        source = source.endArray().endObject();
        client().prepareIndex("idx", "type").setSource(source).execute().actionGet();
    }
    assertNoFailures(client().admin().indices().prepareRefresh("idx").setIndicesOptions(IndicesOptions.lenientExpandOpen()).execute().get());
    Map<String, Object> params = new HashMap<>();
    params.put("interval", interval);
    SearchResponse resp = client().prepareSearch("idx").addAggregation(terms("terms").field("values").collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "floor(_value / interval)", params)).size(maxNumTerms)).addAggregation(histogram("histo").field("values").interval(interval).minDocCount(1)).execute().actionGet();
    assertSearchResponse(resp);
    Terms terms = resp.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    Histogram histo = resp.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(terms.getBuckets().size(), equalTo(histo.getBuckets().size()));
    for (Histogram.Bucket bucket : histo.getBuckets()) {
        final double key = ((Number) bucket.getKey()).doubleValue() / interval;
        final Terms.Bucket termsBucket = terms.getBucketByKey(String.valueOf(key));
        assertEquals(bucket.getDocCount(), termsBucket.getDocCount());
    }
}
Also used : Script(org.elasticsearch.script.Script) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) HashMap(java.util.HashMap) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 12 with Terms

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

the class GeoDistanceIT method testWithSubAggregation.

public void testWithSubAggregation() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(geoDistance("amsterdam_rings", new GeoPoint(52.3760, 4.894)).field("location").unit(DistanceUnit.KILOMETERS).addUnboundedTo(500).addRange(500, 1000).addUnboundedFrom(1000).subAggregation(terms("cities").field("city").collectMode(randomFrom(SubAggCollectionMode.values())))).execute().actionGet();
    assertSearchResponse(response);
    Range geoDist = response.getAggregations().get("amsterdam_rings");
    assertThat(geoDist, notNullValue());
    assertThat(geoDist.getName(), equalTo("amsterdam_rings"));
    List<? extends Bucket> buckets = geoDist.getBuckets();
    assertThat(geoDist.getBuckets().size(), equalTo(3));
    assertThat(geoDist.getProperty("_bucket_count"), equalTo(3));
    Object[] propertiesKeys = (Object[]) geoDist.getProperty("_key");
    Object[] propertiesDocCounts = (Object[]) geoDist.getProperty("_count");
    Object[] propertiesCities = (Object[]) geoDist.getProperty("cities");
    Range.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("*-500.0"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(0.0));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(500.0));
    assertThat(bucket.getFromAsString(), equalTo("0.0"));
    assertThat(bucket.getToAsString(), equalTo("500.0"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    Terms cities = bucket.getAggregations().get("cities");
    assertThat(cities, Matchers.notNullValue());
    Set<String> names = new HashSet<>();
    for (Terms.Bucket city : cities.getBuckets()) {
        names.add(city.getKeyAsString());
    }
    assertThat(names.contains("utrecht") && names.contains("haarlem"), is(true));
    assertThat((String) propertiesKeys[0], equalTo("*-500.0"));
    assertThat((long) propertiesDocCounts[0], equalTo(2L));
    assertThat((Terms) propertiesCities[0], sameInstance(cities));
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("500.0-1000.0"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(500.0));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(1000.0));
    assertThat(bucket.getFromAsString(), equalTo("500.0"));
    assertThat(bucket.getToAsString(), equalTo("1000.0"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    cities = bucket.getAggregations().get("cities");
    assertThat(cities, Matchers.notNullValue());
    names = new HashSet<>();
    for (Terms.Bucket city : cities.getBuckets()) {
        names.add(city.getKeyAsString());
    }
    assertThat(names.contains("berlin") && names.contains("prague"), is(true));
    assertThat((String) propertiesKeys[1], equalTo("500.0-1000.0"));
    assertThat((long) propertiesDocCounts[1], equalTo(2L));
    assertThat((Terms) propertiesCities[1], sameInstance(cities));
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat((String) bucket.getKey(), equalTo("1000.0-*"));
    assertThat(((Number) bucket.getFrom()).doubleValue(), equalTo(1000.0));
    assertThat(((Number) bucket.getTo()).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(bucket.getFromAsString(), equalTo("1000.0"));
    assertThat(bucket.getToAsString(), nullValue());
    assertThat(bucket.getDocCount(), equalTo(1L));
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    cities = bucket.getAggregations().get("cities");
    assertThat(cities, Matchers.notNullValue());
    names = new HashSet<>();
    for (Terms.Bucket city : cities.getBuckets()) {
        names.add(city.getKeyAsString());
    }
    assertThat(names.contains("tel-aviv"), is(true));
    assertThat((String) propertiesKeys[2], equalTo("1000.0-*"));
    assertThat((long) propertiesDocCounts[2], equalTo(1L));
    assertThat((Terms) propertiesCities[2], sameInstance(cities));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.range.Range.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Range(org.elasticsearch.search.aggregations.bucket.range.Range) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) GeoPoint(org.elasticsearch.common.geo.GeoPoint) HashSet(java.util.HashSet)

Example 13 with Terms

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

the class BooleanTermsIT method testSingleValueField.

public void testSingleValueField() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").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"));
    final int bucketCount = numSingleFalses > 0 && numSingleTrues > 0 ? 2 : numSingleFalses + numSingleTrues > 0 ? 1 : 0;
    assertThat(terms.getBuckets().size(), equalTo(bucketCount));
    Terms.Bucket bucket = terms.getBucketByKey("false");
    if (numSingleFalses == 0) {
        assertNull(bucket);
    } else {
        assertNotNull(bucket);
        assertEquals(numSingleFalses, bucket.getDocCount());
        assertEquals("false", bucket.getKeyAsString());
    }
    bucket = terms.getBucketByKey("true");
    if (numSingleTrues == 0) {
        assertNull(bucket);
    } else {
        assertNotNull(bucket);
        assertEquals(numSingleTrues, bucket.getDocCount());
        assertEquals("true", bucket.getKeyAsString());
    }
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 14 with Terms

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

the class BooleanTermsIT method testMultiValueField.

public void testMultiValueField() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").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"));
    final int bucketCount = numMultiFalses > 0 && numMultiTrues > 0 ? 2 : numMultiFalses + numMultiTrues > 0 ? 1 : 0;
    assertThat(terms.getBuckets().size(), equalTo(bucketCount));
    Terms.Bucket bucket = terms.getBucketByKey("false");
    if (numMultiFalses == 0) {
        assertNull(bucket);
    } else {
        assertNotNull(bucket);
        assertEquals(numMultiFalses, bucket.getDocCount());
        assertEquals("false", bucket.getKeyAsString());
    }
    bucket = terms.getBucketByKey("true");
    if (numMultiTrues == 0) {
        assertNull(bucket);
    } else {
        assertNotNull(bucket);
        assertEquals(numMultiTrues, bucket.getDocCount());
        assertEquals("true", bucket.getKeyAsString());
    }
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 15 with Terms

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

the class BooleanTermsIT method testUnmapped.

public void testUnmapped() throws Exception {
    SearchResponse response = client().prepareSearch("idx_unmapped").setTypes("type").addAggregation(terms("terms").field(SINGLE_VALUED_FIELD_NAME).size(between(1, 5)).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(0));
}
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