From 11bafa18e1b1c6d9a95da57295fc9e2271f12ba7 Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Thu, 14 Jan 2016 10:52:28 +0000 Subject: Removes Aggregation Builders in place of AggregatorFactory implementations --- .../terms/GlobalOrdinalsStringTermsAggregator.java | 2 +- .../aggregations/bucket/terms/InternalOrder.java | 19 +- .../aggregations/bucket/terms/InternalTerms.java | 2 +- .../bucket/terms/TermsAggregatorFactory.java | 91 ++++--- .../aggregations/bucket/terms/TermsBuilder.java | 276 --------------------- .../aggregations/bucket/terms/TermsParser.java | 8 +- .../bucket/terms/support/IncludeExclude.java | 50 ++++ 7 files changed, 135 insertions(+), 313 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsBuilder.java (limited to 'core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms') diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java index 1e7a0047ea..91e949e190 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java @@ -347,7 +347,7 @@ public class GlobalOrdinalsStringTermsAggregator extends AbstractStringTermsAggr Map metaData) throws IOException { super(name, factories, valuesSource, order, bucketCountThresholds, null, aggregationContext, parent, collectionMode, showTermDocCountError, pipelineAggregators, metaData); - assert factories == null || factories.count() == 0; + assert factories == null || factories.countAggregators() == 0; this.segmentDocCounts = context.bigArrays().newIntArray(1, true); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalOrder.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalOrder.java index b5e1e81479..f3f87c09dc 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalOrder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalOrder.java @@ -96,7 +96,7 @@ class InternalOrder extends Terms.Order { public static boolean isCountDesc(Terms.Order order) { if (order == COUNT_DESC) { return true; - }else if (order instanceof CompoundOrder) { + } else if (order instanceof CompoundOrder) { // check if its a compound order with count desc and the tie breaker (term asc) CompoundOrder compoundOrder = (CompoundOrder) order; if (compoundOrder.orderElements.size() == 2 && compoundOrder.orderElements.get(0) == COUNT_DESC && compoundOrder.orderElements.get(1) == TERM_ASC) { @@ -106,6 +106,23 @@ class InternalOrder extends Terms.Order { return false; } + public static boolean isTermOrder(Terms.Order order) { + if (order == TERM_ASC) { + return true; + } else if (order == TERM_DESC) { + return true; + } else if (order instanceof CompoundOrder) { + // check if its a compound order with only a single element ordering + // by term + CompoundOrder compoundOrder = (CompoundOrder) order; + if (compoundOrder.orderElements.size() == 1 && compoundOrder.orderElements.get(0) == TERM_ASC + || compoundOrder.orderElements.get(0) == TERM_DESC) { + return true; + } + } + return false; + } + final byte id; final String key; final boolean asc; diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java index 31285d20f8..6e5aaac2e8 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java @@ -188,7 +188,7 @@ public abstract class InternalTerms pipelineAggregators, Map metaData) throws IOException { - if (includeExclude != null || factories.count() > 0 + if (includeExclude != null || factories.countAggregators() > 0 // we need the FieldData impl to be able to extract the // segment to global ord mapping || valuesSource.getClass() != ValuesSource.Bytes.FieldData.class) { @@ -182,7 +180,7 @@ public class TermsAggregatorFactory extends ValuesSourceAggregatorFactory orders = Collections.singletonList(Terms.Order.count(false)); + private Terms.Order order = Terms.Order.compound(Terms.Order.count(false), Terms.Order.term(true)); private IncludeExclude includeExclude = null; private String executionHint = null; private SubAggCollectionMode collectMode = SubAggCollectionMode.DEPTH_FIRST; @@ -190,8 +188,8 @@ public class TermsAggregatorFactory extends ValuesSourceAggregatorFactory order) { - this.orders = order; + public TermsAggregatorFactory order(Terms.Order order) { + this.order = order; + return this; + } + + /** + * Sets the order in which the buckets will be returned. + */ + public TermsAggregatorFactory order(List orders) { + order(Terms.Order.compound(orders)); return this; } /** * Gets the order in which the buckets will be returned. */ - public List order() { - return orders; + public Terms.Order order() { + return order; } /** @@ -281,7 +325,6 @@ public class TermsAggregatorFactory extends ValuesSourceAggregatorFactory pipelineAggregators, Map metaData) throws IOException { - Terms.Order order = resolveOrder(orders); final InternalAggregation aggregation = new UnmappedTerms(name, order, bucketCountThresholds.getRequiredSize(), bucketCountThresholds.getShardSize(), bucketCountThresholds.getMinDocCount(), pipelineAggregators, metaData); return new NonCollectingAggregator(name, aggregationContext, parent, factories, pipelineAggregators, metaData) { @@ -315,7 +358,6 @@ public class TermsAggregatorFactory extends ValuesSourceAggregatorFactory pipelineAggregators, Map metaData) throws IOException { - Terms.Order order = resolveOrder(orders); if (collectsFromSingleBucket == false) { return asMultiBucketAggregator(this, aggregationContext, parent); } @@ -415,11 +457,8 @@ public class TermsAggregatorFactory extends ValuesSourceAggregatorFactory orders = new ArrayList<>(numOrders); - for (int i = 0; i < numOrders; i++) { - orders.add(InternalOrder.Streams.readOrder(in)); - } - factory.orders = orders; + factory.order = InternalOrder.Streams.readOrder(in); factory.showTermDocCountError = in.readBoolean(); return factory; } @@ -457,16 +491,13 @@ public class TermsAggregatorFactory extends ValuesSourceAggregatorFactory { - - private TermsAggregator.BucketCountThresholds bucketCountThresholds = new TermsAggregator.BucketCountThresholds(-1, -1, -1, -1); - - private Terms.ValueType valueType; - private Terms.Order order; - private String includePattern; - private String excludePattern; - private String executionHint; - private SubAggCollectionMode collectionMode; - private Boolean showTermDocCountError; - private String[] includeTerms = null; - private String[] excludeTerms = null; - - /** - * Sole constructor. - */ - public TermsBuilder(String name) { - super(name, "terms"); - } - - /** - * Sets the size - indicating how many term buckets should be returned (defaults to 10) - */ - public TermsBuilder size(int size) { - bucketCountThresholds.setRequiredSize(size); - return this; - } - - /** - * Sets the shard_size - indicating the number of term buckets each shard will return to the coordinating node (the - * node that coordinates the search execution). The higher the shard size is, the more accurate the results are. - */ - public TermsBuilder shardSize(int shardSize) { - bucketCountThresholds.setShardSize(shardSize); - return this; - } - - /** - * Set the minimum document count terms should have in order to appear in the response. - */ - public TermsBuilder minDocCount(long minDocCount) { - bucketCountThresholds.setMinDocCount(minDocCount); - return this; - } - - /** - * Set the minimum document count terms should have on the shard in order to appear in the response. - */ - public TermsBuilder shardMinDocCount(long shardMinDocCount) { - bucketCountThresholds.setShardMinDocCount(shardMinDocCount); - return this; - } - - /** - * Define a regular expression that will determine what terms should be aggregated. The regular expression is based - * on the {@link RegExp} class. - * - * @see RegExp#RegExp(String) - */ - public TermsBuilder include(String regex) { - if (includeTerms != null) { - throw new IllegalArgumentException("exclude clause must be an array of strings or a regex, not both"); - } - this.includePattern = regex; - return this; - } - - /** - * Define a set of terms that should be aggregated. - */ - public TermsBuilder include(String [] terms) { - if (includePattern != null) { - throw new IllegalArgumentException("include clause must be an array of exact values or a regex, not both"); - } - this.includeTerms = terms; - return this; - } - - /** - * Define a set of terms that should be aggregated. - */ - public TermsBuilder include(long [] terms) { - if (includePattern != null) { - throw new IllegalArgumentException("include clause must be an array of exact values or a regex, not both"); - } - this.includeTerms = longsArrToStringArr(terms); - return this; - } - - private String[] longsArrToStringArr(long[] terms) { - String[] termsAsString = new String[terms.length]; - for (int i = 0; i < terms.length; i++) { - termsAsString[i] = Long.toString(terms[i]); - } - return termsAsString; - } - - - /** - * Define a set of terms that should be aggregated. - */ - public TermsBuilder include(double [] terms) { - if (includePattern != null) { - throw new IllegalArgumentException("include clause must be an array of exact values or a regex, not both"); - } - this.includeTerms = doubleArrToStringArr(terms); - return this; - } - - private String[] doubleArrToStringArr(double[] terms) { - String[] termsAsString = new String[terms.length]; - for (int i = 0; i < terms.length; i++) { - termsAsString[i] = Double.toString(terms[i]); - } - return termsAsString; - } - - /** - * Define a regular expression that will filter out terms that should be excluded from the aggregation. The regular - * expression is based on the {@link RegExp} class. - * - * @see RegExp#RegExp(String) - */ - public TermsBuilder exclude(String regex) { - if (excludeTerms != null) { - throw new IllegalArgumentException("exclude clause must be an array of exact values or a regex, not both"); - } - this.excludePattern = regex; - return this; - } - - /** - * Define a set of terms that should not be aggregated. - */ - public TermsBuilder exclude(String [] terms) { - if (excludePattern != null) { - throw new IllegalArgumentException("exclude clause must be an array of exact values or a regex, not both"); - } - this.excludeTerms = terms; - return this; - } - - - /** - * Define a set of terms that should not be aggregated. - */ - public TermsBuilder exclude(long [] terms) { - if (excludePattern != null) { - throw new IllegalArgumentException("exclude clause must be an array of exact values or a regex, not both"); - } - this.excludeTerms = longsArrToStringArr(terms); - return this; - } - - /** - * Define a set of terms that should not be aggregated. - */ - public TermsBuilder exclude(double [] terms) { - if (excludePattern != null) { - throw new IllegalArgumentException("exclude clause must be an array of exact values or a regex, not both"); - } - this.excludeTerms = doubleArrToStringArr(terms); - return this; - } - - - - /** - * When using scripts, the value type indicates the types of the values the script is generating. - */ - public TermsBuilder valueType(Terms.ValueType valueType) { - this.valueType = valueType; - return this; - } - - /** - * Defines the order in which the buckets will be returned. - */ - public TermsBuilder order(Terms.Order order) { - this.order = order; - return this; - } - - /** - * Expert: provide an execution hint to the aggregation. - */ - public TermsBuilder executionHint(String executionHint) { - this.executionHint = executionHint; - return this; - } - - /** - * Expert: set the collection mode. - */ - public TermsBuilder collectMode(SubAggCollectionMode mode) { - this.collectionMode = mode; - return this; - } - - /** - * Expert: return document count errors per term in the response. - */ - public TermsBuilder showTermDocCountError(boolean showTermDocCountError) { - this.showTermDocCountError = showTermDocCountError; - return this; - } - - @Override - protected XContentBuilder doInternalXContent(XContentBuilder builder, Params params) throws IOException { - - bucketCountThresholds.toXContent(builder, params); - - if (showTermDocCountError != null) { - builder.field(TermsAggregatorFactory.SHOW_TERM_DOC_COUNT_ERROR.getPreferredName(), showTermDocCountError); - } - if (executionHint != null) { - builder.field(TermsAggregatorFactory.EXECUTION_HINT_FIELD_NAME.getPreferredName(), executionHint); - } - if (valueType != null) { - builder.field("value_type", valueType.name().toLowerCase(Locale.ROOT)); - } - if (order != null) { - builder.field("order"); - order.toXContent(builder, params); - } - if (collectionMode != null) { - builder.field(SubAggCollectionMode.KEY.getPreferredName(), collectionMode.parseField().getPreferredName()); - } - if (includeTerms != null) { - builder.array("include", includeTerms); - } - if (includePattern != null) { - builder.field("include", includePattern); - } - if (excludeTerms != null) { - builder.array("exclude", excludeTerms); - } - if (excludePattern != null) { - builder.field("exclude", excludePattern); - } - return builder; - } -} diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsParser.java index 5b8368a218..7e300af763 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/TermsParser.java @@ -52,7 +52,7 @@ public class TermsParser extends AbstractTermsParser { protected TermsAggregatorFactory doCreateFactory(String aggregationName, ValuesSourceType valuesSourceType, ValueType targetValueType, BucketCountThresholds bucketCountThresholds, SubAggCollectionMode collectMode, String executionHint, IncludeExclude incExc, Map otherOptions) { - TermsAggregatorFactory factory = new TermsAggregatorFactory(aggregationName, valuesSourceType, targetValueType); + TermsAggregatorFactory factory = new TermsAggregatorFactory(aggregationName, targetValueType); List orderElements = (List) otherOptions.get(TermsAggregatorFactory.ORDER_FIELD); if (orderElements != null) { List orders = new ArrayList<>(orderElements.size()); @@ -97,7 +97,7 @@ public class TermsParser extends AbstractTermsParser { orderElements.add(orderParam); } else { throw new ParsingException(parser.getTokenLocation(), - "Order elements must be of type object in [" + aggregationName + "]."); + "Order elements must be of type object in [" + aggregationName + "] found token of type [" + token + "]."); } } otherOptions.put(TermsAggregatorFactory.ORDER_FIELD, orderElements); @@ -179,8 +179,8 @@ public class TermsParser extends AbstractTermsParser { } @Override - public AggregatorFactory[] getFactoryPrototypes() { - return new AggregatorFactory[] { new TermsAggregatorFactory(null, null, null) }; + public AggregatorFactory getFactoryPrototypes() { + return new TermsAggregatorFactory(null, null); } } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/support/IncludeExclude.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/support/IncludeExclude.java index f6df150a4c..eee9d4cbf9 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/support/IncludeExclude.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/support/IncludeExclude.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.aggregations.bucket.terms.support; import com.carrotsearch.hppc.LongHashSet; import com.carrotsearch.hppc.LongSet; + import org.apache.lucene.index.RandomAccessOrds; import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.index.Terms; @@ -226,6 +227,10 @@ public class IncludeExclude implements Writeable, ToXContent { this.excludeValues = null; } + public IncludeExclude(String include, String exclude) { + this(include == null ? null : new RegExp(include), exclude == null ? null : new RegExp(exclude)); + } + /** * @param includeValues The terms to be included * @param excludeValues The terms to be excluded @@ -240,6 +245,51 @@ public class IncludeExclude implements Writeable, ToXContent { this.excludeValues = excludeValues; } + public IncludeExclude(String[] includeValues, String[] excludeValues) { + this(convertToBytesRefSet(includeValues), convertToBytesRefSet(excludeValues)); + } + + public IncludeExclude(double[] includeValues, double[] excludeValues) { + this(convertToBytesRefSet(includeValues), convertToBytesRefSet(excludeValues)); + } + + public IncludeExclude(long[] includeValues, long[] excludeValues) { + this(convertToBytesRefSet(includeValues), convertToBytesRefSet(excludeValues)); + } + + private static SortedSet convertToBytesRefSet(String[] values) { + SortedSet returnSet = null; + if (values != null) { + returnSet = new TreeSet<>(); + for (String value : values) { + returnSet.add(new BytesRef(value)); + } + } + return returnSet; + } + + private static SortedSet convertToBytesRefSet(double[] values) { + SortedSet returnSet = null; + if (values != null) { + returnSet = new TreeSet<>(); + for (double value : values) { + returnSet.add(new BytesRef(String.valueOf(value))); + } + } + return returnSet; + } + + private static SortedSet convertToBytesRefSet(long[] values) { + SortedSet returnSet = null; + if (values != null) { + returnSet = new TreeSet<>(); + for (long value : values) { + returnSet.add(new BytesRef(String.valueOf(value))); + } + } + return returnSet; + } + /** * Terms adapter around doc values. */ -- cgit v1.2.3