Search in sources :

Example 1 with TermQueryBuilder

use of org.elasticsearch.index.query.TermQueryBuilder 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 2 with TermQueryBuilder

use of org.elasticsearch.index.query.TermQueryBuilder 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 3 with TermQueryBuilder

use of org.elasticsearch.index.query.TermQueryBuilder in project elasticsearch by elastic.

the class SamplerIT method testUnmappedChildAggNoDiversity.

public void testUnmappedChildAggNoDiversity() throws Exception {
    SamplerAggregationBuilder sampleAgg = sampler("sample").shardSize(100);
    sampleAgg.subAggregation(terms("authors").field("author"));
    SearchResponse response = client().prepareSearch("idx_unmapped").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");
    assertThat(authors.getBuckets().size(), equalTo(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 4 with TermQueryBuilder

use of org.elasticsearch.index.query.TermQueryBuilder 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 5 with TermQueryBuilder

use of org.elasticsearch.index.query.TermQueryBuilder in project elasticsearch by elastic.

the class SignificantTermsIT method testIncludeExcludeExactValues.

public void testIncludeExcludeExactValues() throws Exception {
    String[] incExcTerms = { "weller", "nosuchterm" };
    SearchResponse response = client().prepareSearch("test").setQuery(new TermQueryBuilder("description", "weller")).addAggregation(significantTerms("mySignificantTerms").field("description").executionHint(randomExecutionHint()).includeExclude(new IncludeExclude(null, incExcTerms))).get();
    assertSearchResponse(response);
    SignificantTerms topTerms = response.getAggregations().get("mySignificantTerms");
    Set<String> terms = new HashSet<>();
    for (Bucket topTerm : topTerms) {
        terms.add(topTerm.getKeyAsString());
    }
    assertEquals(new HashSet<String>(Arrays.asList("jam", "council", "style", "paul", "of", "the")), terms);
    response = client().prepareSearch("test").setQuery(new TermQueryBuilder("description", "weller")).addAggregation(significantTerms("mySignificantTerms").field("description").executionHint(randomExecutionHint()).includeExclude(new IncludeExclude(incExcTerms, null))).get();
    assertSearchResponse(response);
    topTerms = response.getAggregations().get("mySignificantTerms");
    terms = new HashSet<>();
    for (Bucket topTerm : topTerms) {
        terms.add(topTerm.getKeyAsString());
    }
    assertThat(terms, hasSize(1));
    assertThat(terms.contains("weller"), is(true));
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) Bucket(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms.Bucket) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) Matchers.containsString(org.hamcrest.Matchers.containsString) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) HashSet(java.util.HashSet)

Aggregations

TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)38 SearchResponse (org.elasticsearch.action.search.SearchResponse)20 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 SignificantTerms (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms)14 BytesReference (org.elasticsearch.common.bytes.BytesReference)7 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)6 Sampler (org.elasticsearch.search.aggregations.bucket.sampler.Sampler)6 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 DirectoryReader (org.apache.lucene.index.DirectoryReader)5 IndexWriter (org.apache.lucene.index.IndexWriter)5 Directory (org.apache.lucene.store.Directory)5 ElasticsearchDirectoryReader (org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader)5 ShardRequestCache (org.elasticsearch.index.cache.request.ShardRequestCache)5 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)5 WrapperQueryBuilder (org.elasticsearch.index.query.WrapperQueryBuilder)5 ShardId (org.elasticsearch.index.shard.ShardId)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Term (org.apache.lucene.index.Term)4 Bucket (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms.Bucket)4