summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/search/aggregations/AggregatorFactoriesTests.java
diff options
context:
space:
mode:
authorColin Goodheart-Smithe <colings86@users.noreply.github.com>2017-07-04 16:47:48 +0100
committerGitHub <noreply@github.com>2017-07-04 16:47:48 +0100
commit41abccf6c5716b08a42a8b40961f57b679694f1a (patch)
tree68ef7438d9cb9351ecc9ec0f18105889fcf1d881 /core/src/test/java/org/elasticsearch/search/aggregations/AggregatorFactoriesTests.java
parent1c4ef0d214f6e55e06f8c813aa0434e8436c47af (diff)
Adds rewrite phase to aggregations (#25495)
* Adds rewrite phase to aggregations This change adds aggregations to the rewrite performed by the `SearchSourceBuilder`. This means that `AggregationBuilder`s are able to implement a `rewrite()` method where they can return a new `AggregationBuilder` which is functionally the same but in a more primitive form. This is exactly analogous to the rewrite done by the `QueryBuilder`s. The first aggregation to implement the rewrite are the filter and filters aggregations so they can rewrite the filters they contain. Closes #17676 * Removes rewrite from PipelineAggregationBuilder Rewrite is based on shard level information. Since pipeline aggregation are run in the reduce phase it doesn’t make sense to rewrite them on the shards. In fact eventually we shouldn’t be transporting them to the shards at all and should be retaining them on the coordinating node for execution in the reduce phase * Addresses review comments * addresses more review comments * Fixed imports
Diffstat (limited to 'core/src/test/java/org/elasticsearch/search/aggregations/AggregatorFactoriesTests.java')
-rw-r--r--core/src/test/java/org/elasticsearch/search/aggregations/AggregatorFactoriesTests.java49
1 files changed, 49 insertions, 0 deletions
diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/AggregatorFactoriesTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/AggregatorFactoriesTests.java
index 4d5af51ec1..f113801965 100644
--- a/core/src/test/java/org/elasticsearch/search/aggregations/AggregatorFactoriesTests.java
+++ b/core/src/test/java/org/elasticsearch/search/aggregations/AggregatorFactoriesTests.java
@@ -19,15 +19,25 @@
package org.elasticsearch.search.aggregations;
import org.elasticsearch.common.ParsingException;
+import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.env.Environment;
+import org.elasticsearch.index.query.QueryBuilder;
+import org.elasticsearch.index.query.QueryRewriteContext;
+import org.elasticsearch.index.query.TermsQueryBuilder;
+import org.elasticsearch.index.query.WrapperQueryBuilder;
+import org.elasticsearch.script.Script;
import org.elasticsearch.search.SearchModule;
+import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders;
+import org.elasticsearch.search.aggregations.pipeline.bucketscript.BucketScriptPipelineAggregationBuilder;
import org.elasticsearch.test.AbstractQueryTestCase;
import org.elasticsearch.test.ESTestCase;
@@ -39,6 +49,7 @@ import java.util.regex.Pattern;
import static java.util.Collections.emptyList;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.instanceOf;
public class AggregatorFactoriesTests extends ESTestCase {
private String[] currentTypes;
@@ -236,6 +247,44 @@ public class AggregatorFactoriesTests extends ESTestCase {
assertThat(e.toString(), containsString("Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [tag_count]"));
}
+ public void testRewrite() throws Exception {
+ XContentType xContentType = randomFrom(XContentType.values());
+ BytesReference bytesReference;
+ try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) {
+ builder.startObject();
+ {
+ builder.startObject("terms");
+ {
+ builder.array("title", "foo");
+ }
+ builder.endObject();
+ }
+ builder.endObject();
+ bytesReference = builder.bytes();
+ }
+ FilterAggregationBuilder filterAggBuilder = new FilterAggregationBuilder("titles", new WrapperQueryBuilder(bytesReference));
+ BucketScriptPipelineAggregationBuilder pipelineAgg = new BucketScriptPipelineAggregationBuilder("const", new Script("1"));
+ AggregatorFactories.Builder builder = new AggregatorFactories.Builder().addAggregator(filterAggBuilder)
+ .addPipelineAggregator(pipelineAgg);
+ AggregatorFactories.Builder rewritten = builder
+ .rewrite(new QueryRewriteContext(null, null, null, xContentRegistry, null, null, () -> 0L));
+ assertNotSame(builder, rewritten);
+ List<AggregationBuilder> aggregatorFactories = rewritten.getAggregatorFactories();
+ assertEquals(1, aggregatorFactories.size());
+ assertThat(aggregatorFactories.get(0), instanceOf(FilterAggregationBuilder.class));
+ FilterAggregationBuilder rewrittenFilterAggBuilder = (FilterAggregationBuilder) aggregatorFactories.get(0);
+ assertNotSame(filterAggBuilder, rewrittenFilterAggBuilder);
+ assertNotEquals(filterAggBuilder, rewrittenFilterAggBuilder);
+ // Check the filter was rewritten from a wrapper query to a terms query
+ QueryBuilder rewrittenFilter = rewrittenFilterAggBuilder.getFilter();
+ assertThat(rewrittenFilter, instanceOf(TermsQueryBuilder.class));
+
+ // Check that a further rewrite returns the same aggregation factories builder
+ AggregatorFactories.Builder secondRewritten = rewritten
+ .rewrite(new QueryRewriteContext(null, null, null, xContentRegistry, null, null, () -> 0L));
+ assertSame(rewritten, secondRewritten);
+ }
+
@Override
protected NamedXContentRegistry xContentRegistry() {
return xContentRegistry;