Search in sources :

Example 1 with DiversifiedAggregationBuilder

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

the class DiversifiedSamplerIT method testNestedSamples.

public void testNestedSamples() throws Exception {
    // Test samples nested under samples
    int MAX_DOCS_PER_AUTHOR = 1;
    int MAX_DOCS_PER_GENRE = 2;
    DiversifiedAggregationBuilder rootSample = new DiversifiedAggregationBuilder("genreSample").shardSize(100).field("genre").maxDocsPerValue(MAX_DOCS_PER_GENRE);
    DiversifiedAggregationBuilder sampleAgg = new DiversifiedAggregationBuilder("sample").shardSize(100);
    sampleAgg.field("author").maxDocsPerValue(MAX_DOCS_PER_AUTHOR).executionHint(randomExecutionHint());
    sampleAgg.subAggregation(terms("authors").field("author"));
    sampleAgg.subAggregation(terms("genres").field("genre"));
    rootSample.subAggregation(sampleAgg);
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).addAggregation(rootSample).execute().actionGet();
    assertSearchResponse(response);
    Sampler genreSample = response.getAggregations().get("genreSample");
    Sampler sample = genreSample.getAggregations().get("sample");
    Terms genres = sample.getAggregations().get("genres");
    Collection<Bucket> testBuckets = genres.getBuckets();
    for (Terms.Bucket testBucket : testBuckets) {
        assertThat(testBucket.getDocCount(), lessThanOrEqualTo((long) NUM_SHARDS * MAX_DOCS_PER_GENRE));
    }
    Terms authors = sample.getAggregations().get("authors");
    testBuckets = authors.getBuckets();
    for (Terms.Bucket testBucket : testBuckets) {
        assertThat(testBucket.getDocCount(), lessThanOrEqualTo((long) NUM_SHARDS * MAX_DOCS_PER_AUTHOR));
    }
}
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) DiversifiedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with DiversifiedAggregationBuilder

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

the class DiversifiedSamplerIT method testSimpleDiversity.

public void testSimpleDiversity() throws Exception {
    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("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();
    for (Terms.Bucket testBucket : testBuckets) {
        assertThat(testBucket.getDocCount(), lessThanOrEqualTo((long) NUM_SHARDS * MAX_DOCS_PER_AUTHOR));
    }
}
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) 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 3 with DiversifiedAggregationBuilder

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

the class DiversifiedSamplerIT method testNestedDiversity.

public void testNestedDiversity() throws Exception {
    // Test multiple samples gathered under buckets made by a parent agg
    int MAX_DOCS_PER_AUTHOR = 1;
    TermsAggregationBuilder rootTerms = terms("genres").field("genre");
    DiversifiedAggregationBuilder sampleAgg = new DiversifiedAggregationBuilder("sample").shardSize(100);
    sampleAgg.field("author").maxDocsPerValue(MAX_DOCS_PER_AUTHOR).executionHint(randomExecutionHint());
    sampleAgg.subAggregation(terms("authors").field("author"));
    rootTerms.subAggregation(sampleAgg);
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).addAggregation(rootTerms).execute().actionGet();
    assertSearchResponse(response);
    Terms genres = response.getAggregations().get("genres");
    Collection<Bucket> genreBuckets = genres.getBuckets();
    for (Terms.Bucket genreBucket : genreBuckets) {
        Sampler sample = genreBucket.getAggregations().get("sample");
        Terms authors = sample.getAggregations().get("authors");
        Collection<Bucket> testBuckets = authors.getBuckets();
        for (Terms.Bucket testBucket : testBuckets) {
            assertThat(testBucket.getDocCount(), lessThanOrEqualTo((long) NUM_SHARDS * MAX_DOCS_PER_AUTHOR));
        }
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) 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) DiversifiedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with DiversifiedAggregationBuilder

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

the class DiversifiedSamplerIT method testPartiallyUnmappedDiversifyField.

public void testPartiallyUnmappedDiversifyField() throws Exception {
    // One of the indexes is missing the "author" field used for
    // diversifying results
    DiversifiedAggregationBuilder sampleAgg = new DiversifiedAggregationBuilder("sample").shardSize(100).field("author").maxDocsPerValue(1);
    sampleAgg.subAggregation(terms("authors").field("author"));
    SearchResponse response = client().prepareSearch("idx_unmapped_author", "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");
    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) 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 5 with DiversifiedAggregationBuilder

use of org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder 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)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)5 DiversifiedAggregationBuilder (org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder)5 Sampler (org.elasticsearch.search.aggregations.bucket.sampler.Sampler)5 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)5 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)3 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)3 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1