Search in sources :

Example 21 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class GeoHashGridIT method testUnmapped.

public void testUnmapped() throws Exception {
    for (int precision = 1; precision <= PRECISION; precision++) {
        SearchResponse response = client().prepareSearch("idx_unmapped").addAggregation(geohashGrid("geohashgrid").field("location").precision(precision)).execute().actionGet();
        assertSearchResponse(response);
        GeoHashGrid geoGrid = response.getAggregations().get("geohashgrid");
        assertThat(geoGrid.getBuckets().size(), equalTo(0));
    }
}
Also used : GeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 22 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class GeoHashGridIT method testMultivalued.

public void testMultivalued() throws Exception {
    for (int precision = 1; precision <= PRECISION; precision++) {
        SearchResponse response = client().prepareSearch("multi_valued_idx").addAggregation(geohashGrid("geohashgrid").field("location").precision(precision)).execute().actionGet();
        assertSearchResponse(response);
        GeoHashGrid geoGrid = response.getAggregations().get("geohashgrid");
        for (GeoHashGrid.Bucket cell : geoGrid.getBuckets()) {
            String geohash = cell.getKeyAsString();
            long bucketCount = cell.getDocCount();
            int expectedBucketCount = multiValuedExpectedDocCountsForGeoHash.get(geohash);
            assertNotSame(bucketCount, 0);
            assertEquals("Geohash " + geohash + " has wrong doc count ", expectedBucketCount, bucketCount);
        }
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid.Bucket) GeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 23 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class GlobalIT method testWithStatsSubAggregator.

public void testWithStatsSubAggregator() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.termQuery("tag", "tag1")).addAggregation(global("global").subAggregation(stats("value_stats").field("value"))).execute().actionGet();
    assertSearchResponse(response);
    Global global = response.getAggregations().get("global");
    assertThat(global, notNullValue());
    assertThat(global.getName(), equalTo("global"));
    assertThat(global.getDocCount(), equalTo((long) numDocs));
    assertThat((long) global.getProperty("_count"), equalTo((long) numDocs));
    assertThat(global.getAggregations().asList().isEmpty(), is(false));
    Stats stats = global.getAggregations().get("value_stats");
    assertThat((Stats) global.getProperty("value_stats"), sameInstance(stats));
    assertThat(stats, notNullValue());
    assertThat(stats.getName(), equalTo("value_stats"));
    long sum = 0;
    for (int i = 0; i < numDocs; ++i) {
        sum += i + 1;
    }
    assertThat(stats.getAvg(), equalTo((double) sum / numDocs));
    assertThat(stats.getMin(), equalTo(1.0));
    assertThat(stats.getMax(), equalTo((double) numDocs));
    assertThat(stats.getCount(), equalTo((long) numDocs));
    assertThat(stats.getSum(), equalTo((double) sum));
}
Also used : Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) Global(org.elasticsearch.search.aggregations.bucket.global.Global) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 24 with SearchResponse

use of org.elasticsearch.action.search.SearchResponse 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 25 with SearchResponse

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

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1451 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)239 Script (org.elasticsearch.script.Script)223 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)208 Matchers.containsString (org.hamcrest.Matchers.containsString)156 ArrayList (java.util.ArrayList)143 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)131 SearchHit (org.elasticsearch.search.SearchHit)112 HashMap (java.util.HashMap)102 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)93 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)78 GeoPoint (org.elasticsearch.common.geo.GeoPoint)70 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)69 SearchHits (org.elasticsearch.search.SearchHits)60 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)60 DateTime (org.joda.time.DateTime)59 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)58 Range (org.elasticsearch.search.aggregations.bucket.range.Range)50