Search in sources :

Example 21 with IncludeExclude

use of org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude in project elasticsearch by elastic.

the class MinDocCountIT method testMinDocCountOnTerms.

private void testMinDocCountOnTerms(String field, Script script, Terms.Order order, String include, boolean retry) throws Exception {
    // all terms
    final SearchResponse allTermsResponse = client().prepareSearch("idx").setTypes("type").setSize(0).setQuery(QUERY).addAggregation(script.apply(terms("terms"), field).collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint()).order(order).size(cardinality + randomInt(10)).minDocCount(0)).execute().actionGet();
    assertAllSuccessful(allTermsResponse);
    final Terms allTerms = allTermsResponse.getAggregations().get("terms");
    assertEquals(cardinality, allTerms.getBuckets().size());
    for (long minDocCount = 0; minDocCount < 20; ++minDocCount) {
        final int size = randomIntBetween(1, cardinality + 2);
        final SearchRequest request = client().prepareSearch("idx").setTypes("type").setSize(0).setQuery(QUERY).addAggregation(script.apply(terms("terms"), field).collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint()).order(order).size(size).includeExclude(include == null ? null : new IncludeExclude(include, null)).shardSize(cardinality + randomInt(10)).minDocCount(minDocCount)).request();
        final SearchResponse response = client().search(request).get();
        assertAllSuccessful(response);
        assertSubset(allTerms, (Terms) response.getAggregations().get("terms"), minDocCount, size, include);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 22 with IncludeExclude

use of org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude in project elasticsearch by elastic.

the class LongTermsIT method testIncludeExcludeResults.

private void testIncludeExcludeResults(int minDocCount, long[] includes, long[] excludes, long[] expectedWithCounts, long[] expectedZeroCounts) {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(SINGLE_VALUED_FIELD_NAME).includeExclude(new IncludeExclude(includes, excludes)).collectMode(randomFrom(SubAggCollectionMode.values())).minDocCount(minDocCount)).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(expectedWithCounts.length + expectedZeroCounts.length));
    for (int i = 0; i < expectedWithCounts.length; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("" + expectedWithCounts[i]);
        assertThat(bucket, notNullValue());
        assertThat(bucket.getDocCount(), equalTo(1L));
    }
    for (int i = 0; i < expectedZeroCounts.length; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("" + expectedZeroCounts[i]);
        assertThat(bucket, notNullValue());
        assertThat(bucket.getDocCount(), equalTo(0L));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 23 with IncludeExclude

use of org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude in project elasticsearch by elastic.

the class SignificantTermsIT method testIncludeExclude.

public void testIncludeExclude() throws Exception {
    SearchResponse response = client().prepareSearch("test").setQuery(new TermQueryBuilder("description", "weller")).addAggregation(significantTerms("mySignificantTerms").field("description").executionHint(randomExecutionHint()).includeExclude(new IncludeExclude(null, "weller"))).get();
    assertSearchResponse(response);
    SignificantTerms topTerms = response.getAggregations().get("mySignificantTerms");
    Set<String> terms = new HashSet<>();
    for (Bucket topTerm : topTerms) {
        terms.add(topTerm.getKeyAsString());
    }
    assertThat(terms, hasSize(6));
    assertThat(terms.contains("jam"), is(true));
    assertThat(terms.contains("council"), is(true));
    assertThat(terms.contains("style"), is(true));
    assertThat(terms.contains("paul"), is(true));
    assertThat(terms.contains("of"), is(true));
    assertThat(terms.contains("the"), is(true));
    response = client().prepareSearch("test").setQuery(new TermQueryBuilder("description", "weller")).addAggregation(significantTerms("mySignificantTerms").field("description").executionHint(randomExecutionHint()).includeExclude(new IncludeExclude("weller", 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) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) HashSet(java.util.HashSet)

Example 24 with IncludeExclude

use of org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude in project elasticsearch by elastic.

the class StringTermsIT method testSingleValueFieldWithExactTermFiltering.

public void testSingleValueFieldWithExactTermFiltering() throws Exception {
    // include without exclude
    String[] incVals = { "val000", "val001", "val002", "val003", "val004", "val005", "val006", "val007", "val008", "val009" };
    SearchResponse response = client().prepareSearch("idx").setTypes("high_card_type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).includeExclude(new IncludeExclude(incVals, null))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(incVals.length));
    for (String incVal : incVals) {
        Terms.Bucket bucket = terms.getBucketByKey(incVal);
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo(incVal));
        assertThat(bucket.getDocCount(), equalTo(1L));
    }
    // include and exclude
    // Slightly illogical example with exact terms below as include and exclude sets
    // are made to overlap but the exclude set should have priority over matches.
    // we should be left with: val002, val003, val004, val005, val006, val007, val008, val009
    String[] excVals = { "val000", "val001" };
    response = client().prepareSearch("idx").setTypes("high_card_type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).includeExclude(new IncludeExclude(incVals, excVals))).execute().actionGet();
    assertSearchResponse(response);
    terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(8));
    for (int i = 2; i < 10; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("val00" + i);
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val00" + i));
        assertThat(bucket.getDocCount(), equalTo(1L));
    }
    // Check case with only exact term exclude clauses
    response = client().prepareSearch("idx").setTypes("high_card_type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).includeExclude(new IncludeExclude(null, excVals))).execute().actionGet();
    assertSearchResponse(response);
    terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(10));
    for (String key : excVals) {
        Terms.Bucket bucket = terms.getBucketByKey(key);
        assertThat(bucket, nullValue());
    }
    NumberFormat nf = NumberFormat.getIntegerInstance(Locale.ENGLISH);
    nf.setMinimumIntegerDigits(3);
    for (int i = 2; i < 12; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("val" + nf.format(i));
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + nf.format(i)));
        assertThat(bucket.getDocCount(), equalTo(1L));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) NumberFormat(java.text.NumberFormat)

Example 25 with IncludeExclude

use of org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude in project elasticsearch by elastic.

the class IncludeExcludeTests method testRegexExclude.

public void testRegexExclude() throws IOException {
    String excRegex = "foo.*";
    String differentRegex = "bar.*";
    IncludeExclude serialized = serialize(new IncludeExclude(null, excRegex), IncludeExclude.EXCLUDE_FIELD);
    assertFalse(serialized.isPartitionBased());
    assertTrue(serialized.isRegexBased());
    IncludeExclude same = new IncludeExclude(null, excRegex);
    assertEquals(serialized, same);
    assertEquals(serialized.hashCode(), same.hashCode());
    IncludeExclude different = new IncludeExclude(null, differentRegex);
    assertFalse(serialized.equals(different));
    assertTrue(serialized.hashCode() != different.hashCode());
}
Also used : IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)

Aggregations

IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)33 SearchResponse (org.elasticsearch.action.search.SearchResponse)19 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)18 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)16 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)7 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)6 HashSet (java.util.HashSet)5 TreeSet (java.util.TreeSet)4 BytesRef (org.apache.lucene.util.BytesRef)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 RegExp (org.apache.lucene.util.automaton.RegExp)3 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)3 SignificantTerms (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms)3 LongBitSet (org.apache.lucene.util.LongBitSet)2 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)2 Script (org.elasticsearch.script.Script)2 DocValueFormat (org.elasticsearch.search.DocValueFormat)2 AggregationExecutionException (org.elasticsearch.search.aggregations.AggregationExecutionException)2 Bucket (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms.Bucket)2 BucketCountThresholds (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator.BucketCountThresholds)2