Search in sources :

Example 26 with IncludeExclude

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

the class IncludeExcludeTests method testExactExcludeValuesEquals.

public void testExactExcludeValuesEquals() throws IOException {
    String[] excValues = { "a", "b" };
    String[] differentExcValues = { "a", "c" };
    IncludeExclude serialized = serialize(new IncludeExclude(null, excValues), IncludeExclude.EXCLUDE_FIELD);
    assertFalse(serialized.isPartitionBased());
    assertFalse(serialized.isRegexBased());
    IncludeExclude same = new IncludeExclude(null, excValues);
    assertEquals(serialized, same);
    assertEquals(serialized.hashCode(), same.hashCode());
    IncludeExclude different = new IncludeExclude(null, differentExcValues);
    assertFalse(serialized.equals(different));
    assertTrue(serialized.hashCode() != different.hashCode());
}
Also used : IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)

Example 27 with IncludeExclude

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

the class IncludeExcludeTests method serializeMixedRegex.

// Serializes/deserializes the IncludeExclude statement with include AND
// exclude clauses
private IncludeExclude serializeMixedRegex(IncludeExclude incExc) throws IOException {
    XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
    if (randomBoolean()) {
        builder.prettyPrint();
    }
    builder.startObject();
    incExc.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    XContentParser parser = createParser(builder);
    QueryParseContext parseContext = new QueryParseContext(parser);
    XContentParser.Token token = parser.nextToken();
    assertEquals(token, XContentParser.Token.START_OBJECT);
    IncludeExclude inc = null;
    IncludeExclude exc = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        assertEquals(XContentParser.Token.FIELD_NAME, token);
        if (IncludeExclude.INCLUDE_FIELD.match(parser.currentName())) {
            token = parser.nextToken();
            inc = IncludeExclude.parseInclude(parser, parseContext);
        } else if (IncludeExclude.EXCLUDE_FIELD.match(parser.currentName())) {
            token = parser.nextToken();
            exc = IncludeExclude.parseExclude(parser, parseContext);
        } else {
            throw new IllegalArgumentException("Unexpected field name serialized in test: " + parser.currentName());
        }
    }
    assertNotNull(inc);
    assertNotNull(exc);
    // Include and Exclude clauses are parsed independently and then merged
    return IncludeExclude.merge(inc, exc);
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 28 with IncludeExclude

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

the class IncludeExcludeTests method testSingleTermWithOrds.

public void testSingleTermWithOrds() throws IOException {
    RandomAccessOrds ords = new RandomAccessOrds() {

        boolean consumed = true;

        @Override
        public void setDocument(int docID) {
            consumed = false;
        }

        @Override
        public long nextOrd() {
            if (consumed) {
                return SortedSetDocValues.NO_MORE_ORDS;
            } else {
                consumed = true;
                return 0;
            }
        }

        @Override
        public BytesRef lookupOrd(long ord) {
            assertEquals(0, ord);
            return new BytesRef("foo");
        }

        @Override
        public long getValueCount() {
            return 1;
        }

        @Override
        public long ordAt(int index) {
            return 0;
        }

        @Override
        public int cardinality() {
            return 1;
        }
    };
    IncludeExclude inexcl = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef("foo"))), null);
    OrdinalsFilter filter = inexcl.convertToOrdinalsFilter(DocValueFormat.RAW);
    LongBitSet acceptedOrds = filter.acceptedGlobalOrdinals(ords);
    assertEquals(1, acceptedOrds.length());
    assertTrue(acceptedOrds.get(0));
    inexcl = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef("bar"))), null);
    filter = inexcl.convertToOrdinalsFilter(DocValueFormat.RAW);
    acceptedOrds = filter.acceptedGlobalOrdinals(ords);
    assertEquals(1, acceptedOrds.length());
    assertFalse(acceptedOrds.get(0));
    inexcl = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef("foo"))), new TreeSet<>(Collections.singleton(new BytesRef("foo"))));
    filter = inexcl.convertToOrdinalsFilter(DocValueFormat.RAW);
    acceptedOrds = filter.acceptedGlobalOrdinals(ords);
    assertEquals(1, acceptedOrds.length());
    assertFalse(acceptedOrds.get(0));
    inexcl = new IncludeExclude(// means everything included
    null, new TreeSet<>(Collections.singleton(new BytesRef("foo"))));
    filter = inexcl.convertToOrdinalsFilter(DocValueFormat.RAW);
    acceptedOrds = filter.acceptedGlobalOrdinals(ords);
    assertEquals(1, acceptedOrds.length());
    assertFalse(acceptedOrds.get(0));
}
Also used : RandomAccessOrds(org.apache.lucene.index.RandomAccessOrds) TreeSet(java.util.TreeSet) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) OrdinalsFilter(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude.OrdinalsFilter) LongBitSet(org.apache.lucene.util.LongBitSet) BytesRef(org.apache.lucene.util.BytesRef)

Example 29 with IncludeExclude

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

the class IncludeExcludeTests method testRegexIncludeAndExclude.

public void testRegexIncludeAndExclude() throws IOException {
    String incRegex = "foo.*";
    String excRegex = "football";
    String differentExcRegex = "foosball";
    IncludeExclude serialized = serializeMixedRegex(new IncludeExclude(incRegex, excRegex));
    assertFalse(serialized.isPartitionBased());
    assertTrue(serialized.isRegexBased());
    IncludeExclude same = new IncludeExclude(incRegex, excRegex);
    assertEquals(serialized, same);
    assertEquals(serialized.hashCode(), same.hashCode());
    IncludeExclude different = new IncludeExclude(incRegex, differentExcRegex);
    assertFalse(serialized.equals(different));
    assertTrue(serialized.hashCode() != different.hashCode());
}
Also used : IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)

Example 30 with IncludeExclude

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

the class IncludeExcludeTests method testExactIncludeValuesEquals.

public void testExactIncludeValuesEquals() throws IOException {
    String[] incValues = { "a", "b" };
    String[] differentIncValues = { "a", "c" };
    IncludeExclude serialized = serialize(new IncludeExclude(incValues, null), IncludeExclude.INCLUDE_FIELD);
    assertFalse(serialized.isPartitionBased());
    assertFalse(serialized.isRegexBased());
    IncludeExclude same = new IncludeExclude(incValues, null);
    assertEquals(serialized, same);
    assertEquals(serialized.hashCode(), same.hashCode());
    IncludeExclude different = new IncludeExclude(differentIncValues, null);
    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