Search in sources :

Example 1 with Missing

use of org.elasticsearch.search.aggregations.bucket.missing.Missing in project elasticsearch by elastic.

the class CombiIT method testMultipleAggsOnSameField_WithDifferentRequiredValueSourceType.

/**
     * Making sure that if there are multiple aggregations, working on the same field, yet require different
     * value source type, they can all still work. It used to fail as we used to cache the ValueSource by the
     * field name. If the cached value source was of type "bytes" and another aggregation on the field required to see
     * it as "numeric", it didn't work. Now we cache the Value Sources by a custom key (field name + ValueSource type)
     * so there's no conflict there.
     */
public void testMultipleAggsOnSameField_WithDifferentRequiredValueSourceType() throws Exception {
    createIndex("idx");
    IndexRequestBuilder[] builders = new IndexRequestBuilder[randomInt(30)];
    IntIntMap values = new IntIntHashMap();
    long missingValues = 0;
    for (int i = 0; i < builders.length; i++) {
        String name = "name_" + randomIntBetween(1, 10);
        if (rarely()) {
            missingValues++;
            builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("name", name).endObject());
        } else {
            int value = randomIntBetween(1, 10);
            values.put(value, values.getOrDefault(value, 0) + 1);
            builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("name", name).field("value", value).endObject());
        }
    }
    indexRandom(true, builders);
    ensureSearchable();
    SubAggCollectionMode aggCollectionMode = randomFrom(SubAggCollectionMode.values());
    SearchResponse response = client().prepareSearch("idx").addAggregation(missing("missing_values").field("value")).addAggregation(terms("values").field("value").collectMode(aggCollectionMode)).execute().actionGet();
    assertSearchResponse(response);
    Aggregations aggs = response.getAggregations();
    Missing missing = aggs.get("missing_values");
    assertNotNull(missing);
    assertThat(missing.getDocCount(), equalTo(missingValues));
    Terms terms = aggs.get("values");
    assertNotNull(terms);
    Collection<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets.size(), equalTo(values.size()));
    for (Terms.Bucket bucket : buckets) {
        values.remove(((Number) bucket.getKey()).intValue());
    }
    assertTrue(values.isEmpty());
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) IntIntMap(com.carrotsearch.hppc.IntIntMap) IntIntHashMap(com.carrotsearch.hppc.IntIntHashMap) Missing(org.elasticsearch.search.aggregations.bucket.missing.Missing) SubAggCollectionMode(org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode)

Example 2 with Missing

use of org.elasticsearch.search.aggregations.bucket.missing.Missing in project elasticsearch by elastic.

the class MissingIT method testSimple.

public void testSimple() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(missing("missing_tag").field("tag")).execute().actionGet();
    assertSearchResponse(response);
    Missing missing = response.getAggregations().get("missing_tag");
    assertThat(missing, notNullValue());
    assertThat(missing.getName(), equalTo("missing_tag"));
    assertThat(missing.getDocCount(), equalTo((long) numDocsMissing));
}
Also used : Missing(org.elasticsearch.search.aggregations.bucket.missing.Missing) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with Missing

use of org.elasticsearch.search.aggregations.bucket.missing.Missing in project elasticsearch by elastic.

the class MissingIT method testEmptyAggregation.

public void testEmptyAggregation() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0).subAggregation(missing("missing").field("value"))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, Matchers.notNullValue());
    Histogram.Bucket bucket = histo.getBuckets().get(1);
    assertThat(bucket, Matchers.notNullValue());
    Missing missing = bucket.getAggregations().get("missing");
    assertThat(missing, Matchers.notNullValue());
    assertThat(missing.getName(), equalTo("missing"));
    assertThat(missing.getDocCount(), is(0L));
}
Also used : Missing(org.elasticsearch.search.aggregations.bucket.missing.Missing) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with Missing

use of org.elasticsearch.search.aggregations.bucket.missing.Missing in project elasticsearch by elastic.

the class MissingIT method testWithSubAggregation.

public void testWithSubAggregation() throws Exception {
    SearchResponse response = client().prepareSearch("idx", "unmapped_idx").addAggregation(missing("missing_tag").field("tag").subAggregation(avg("avg_value").field("value"))).execute().actionGet();
    assertSearchResponse(response);
    assertThat("Not all shards are initialized", response.getSuccessfulShards(), equalTo(response.getTotalShards()));
    Missing missing = response.getAggregations().get("missing_tag");
    assertThat(missing, notNullValue());
    assertThat(missing.getName(), equalTo("missing_tag"));
    assertThat(missing.getDocCount(), equalTo((long) numDocsMissing + numDocsUnmapped));
    assertThat((long) missing.getProperty("_count"), equalTo((long) numDocsMissing + numDocsUnmapped));
    assertThat(missing.getAggregations().asList().isEmpty(), is(false));
    long sum = 0;
    for (int i = 0; i < numDocsMissing; ++i) {
        sum += i;
    }
    for (int i = 0; i < numDocsUnmapped; ++i) {
        sum += i;
    }
    Avg avgValue = missing.getAggregations().get("avg_value");
    assertThat(avgValue, notNullValue());
    assertThat(avgValue.getName(), equalTo("avg_value"));
    assertThat(avgValue.getValue(), equalTo((double) sum / (numDocsMissing + numDocsUnmapped)));
    assertThat((double) missing.getProperty("avg_value.value"), equalTo((double) sum / (numDocsMissing + numDocsUnmapped)));
}
Also used : Missing(org.elasticsearch.search.aggregations.bucket.missing.Missing) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with Missing

use of org.elasticsearch.search.aggregations.bucket.missing.Missing in project elasticsearch by elastic.

the class ShardReduceIT method testMissing.

public void testMissing() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.matchAllQuery()).addAggregation(missing("missing").field("foobar").subAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).minDocCount(0))).execute().actionGet();
    assertSearchResponse(response);
    Missing missing = response.getAggregations().get("missing");
    Histogram histo = missing.getAggregations().get("histo");
    assertThat(histo.getBuckets().size(), equalTo(4));
}
Also used : Missing(org.elasticsearch.search.aggregations.bucket.missing.Missing) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)9 Missing (org.elasticsearch.search.aggregations.bucket.missing.Missing)9 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)9 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)3 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)2 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)2 IntIntHashMap (com.carrotsearch.hppc.IntIntHashMap)1 IntIntMap (com.carrotsearch.hppc.IntIntMap)1 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)1 SubAggCollectionMode (org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode)1 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)1 Global (org.elasticsearch.search.aggregations.bucket.global.Global)1 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)1 ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)1