Search in sources :

Example 6 with Sampler

use of org.elasticsearch.search.aggregations.bucket.sampler.Sampler in project elasticsearch by elastic.

the class SamplerIT method testSimpleSampler.

public void testSimpleSampler() throws Exception {
    SamplerAggregationBuilder sampleAgg = sampler("sample").shardSize(100);
    sampleAgg.subAggregation(terms("authors").field("author"));
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("genre", "fantasy")).setFrom(0).setSize(60).addAggregation(sampleAgg).execute().actionGet();
    assertSearchResponse(response);
    Sampler sample = response.getAggregations().get("sample");
    Terms authors = sample.getAggregations().get("authors");
    Collection<Bucket> testBuckets = authors.getBuckets();
    long maxBooksPerAuthor = 0;
    for (Terms.Bucket testBucket : testBuckets) {
        maxBooksPerAuthor = Math.max(testBucket.getDocCount(), maxBooksPerAuthor);
    }
    assertThat(maxBooksPerAuthor, equalTo(3L));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SamplerAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 7 with Sampler

use of org.elasticsearch.search.aggregations.bucket.sampler.Sampler in project elasticsearch by elastic.

the class SamplerIT method testIssue10719.

public void testIssue10719() throws Exception {
    // Tests that we can refer to nested elements under a sample in a path
    // statement
    boolean asc = randomBoolean();
    SearchResponse response = client().prepareSearch("test").setTypes("book").setSearchType(SearchType.QUERY_THEN_FETCH).addAggregation(terms("genres").field("genre").order(Terms.Order.aggregation("sample>max_price.value", asc)).subAggregation(sampler("sample").shardSize(100).subAggregation(max("max_price").field("price")))).execute().actionGet();
    assertSearchResponse(response);
    Terms genres = response.getAggregations().get("genres");
    Collection<Bucket> genreBuckets = genres.getBuckets();
    // For this test to be useful we need >1 genre bucket to compare
    assertThat(genreBuckets.size(), greaterThan(1));
    double lastMaxPrice = asc ? Double.MIN_VALUE : Double.MAX_VALUE;
    for (Terms.Bucket genreBucket : genres.getBuckets()) {
        Sampler sample = genreBucket.getAggregations().get("sample");
        Max maxPriceInGenre = sample.getAggregations().get("max_price");
        double price = maxPriceInGenre.getValue();
        if (asc) {
            assertThat(price, greaterThanOrEqualTo(lastMaxPrice));
        } else {
            assertThat(price, lessThanOrEqualTo(lastMaxPrice));
        }
        lastMaxPrice = price;
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Max(org.elasticsearch.search.aggregations.metrics.max.Max) Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 8 with Sampler

use of org.elasticsearch.search.aggregations.bucket.sampler.Sampler in project elasticsearch by elastic.

the class SamplerIT method testPartiallyUnmappedChildAggNoDiversity.

public void testPartiallyUnmappedChildAggNoDiversity() throws Exception {
    SamplerAggregationBuilder sampleAgg = sampler("sample").shardSize(100);
    sampleAgg.subAggregation(terms("authors").field("author"));
    SearchResponse response = client().prepareSearch("idx_unmapped", "test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("genre", "fantasy")).setFrom(0).setSize(60).setExplain(true).addAggregation(sampleAgg).execute().actionGet();
    assertSearchResponse(response);
    Sampler sample = response.getAggregations().get("sample");
    assertThat(sample.getDocCount(), greaterThan(0L));
    Terms authors = sample.getAggregations().get("authors");
    assertThat(authors.getBuckets().size(), greaterThan(0));
}
Also used : Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SamplerAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 9 with Sampler

use of org.elasticsearch.search.aggregations.bucket.sampler.Sampler in project elasticsearch by elastic.

the class DiversifiedSamplerIT method testWhollyUnmappedDiversifyField.

public void testWhollyUnmappedDiversifyField() throws Exception {
    //All of the indices are missing the "author" field used for diversifying results
    int MAX_DOCS_PER_AUTHOR = 1;
    DiversifiedAggregationBuilder sampleAgg = new DiversifiedAggregationBuilder("sample").shardSize(100);
    sampleAgg.field("author").maxDocsPerValue(MAX_DOCS_PER_AUTHOR).executionHint(randomExecutionHint());
    sampleAgg.subAggregation(terms("authors").field("author"));
    SearchResponse response = client().prepareSearch("idx_unmapped", "idx_unmapped_author").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("genre", "fantasy")).setFrom(0).setSize(60).addAggregation(sampleAgg).execute().actionGet();
    assertSearchResponse(response);
    Sampler sample = response.getAggregations().get("sample");
    assertThat(sample.getDocCount(), equalTo(0L));
    Terms authors = sample.getAggregations().get("authors");
    assertNull(authors);
}
Also used : Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) DiversifiedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 10 with Sampler

use of org.elasticsearch.search.aggregations.bucket.sampler.Sampler in project elasticsearch by elastic.

the class DiversifiedSamplerIT method testIssue10719.

public void testIssue10719() throws Exception {
    // Tests that we can refer to nested elements under a sample in a path
    // statement
    boolean asc = randomBoolean();
    SearchResponse response = client().prepareSearch("test").setTypes("book").setSearchType(SearchType.QUERY_THEN_FETCH).addAggregation(terms("genres").field("genre").order(Terms.Order.aggregation("sample>max_price.value", asc)).subAggregation(sampler("sample").shardSize(100).subAggregation(max("max_price").field("price")))).execute().actionGet();
    assertSearchResponse(response);
    Terms genres = response.getAggregations().get("genres");
    Collection<Bucket> genreBuckets = genres.getBuckets();
    // For this test to be useful we need >1 genre bucket to compare
    assertThat(genreBuckets.size(), greaterThan(1));
    double lastMaxPrice = asc ? Double.MIN_VALUE : Double.MAX_VALUE;
    for (Terms.Bucket genreBucket : genres.getBuckets()) {
        Sampler sample = genreBucket.getAggregations().get("sample");
        Max maxPriceInGenre = sample.getAggregations().get("max_price");
        double price = maxPriceInGenre.getValue();
        if (asc) {
            assertThat(price, greaterThanOrEqualTo(lastMaxPrice));
        } else {
            assertThat(price, lessThanOrEqualTo(lastMaxPrice));
        }
        lastMaxPrice = price;
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Max(org.elasticsearch.search.aggregations.metrics.max.Max) Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) 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)10 Sampler (org.elasticsearch.search.aggregations.bucket.sampler.Sampler)10 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)10 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)10 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)6 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)6 DiversifiedAggregationBuilder (org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder)5 SamplerAggregationBuilder (org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder)3 Max (org.elasticsearch.search.aggregations.metrics.max.Max)2 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1