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);
}
}
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));
}
}
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));
}
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));
}
}
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());
}
Aggregations