Terms aggs: Validate the aggregation order on unmapped terms too.

Close #8946
This commit is contained in:
Adrien Grand 2014-12-15 12:50:15 +01:00
parent b2ec19ab36
commit a50e3930c9
5 changed files with 187 additions and 163 deletions

View File

@ -31,8 +31,12 @@ import java.util.Map;
*/
public abstract class NonCollectingAggregator extends Aggregator {
protected NonCollectingAggregator(String name, AggregationContext context, Aggregator parent, AggregatorFactories subFactories, Map<String, Object> metaData) {
super(name, BucketAggregationMode.MULTI_BUCKETS, subFactories, 0, context, parent, metaData);
}
protected NonCollectingAggregator(String name, AggregationContext context, Aggregator parent, Map<String, Object> metaData) {
super(name, BucketAggregationMode.MULTI_BUCKETS, AggregatorFactories.EMPTY, 0, context, parent, metaData);
this(name, context, parent, AggregatorFactories.EMPTY, metaData);
}
private void fail() {

View File

@ -147,7 +147,11 @@ public class TermsAggregatorFactory extends ValuesSourceAggregatorFactory<Values
@Override
protected Aggregator createUnmapped(AggregationContext aggregationContext, Aggregator parent, Map<String, Object> metaData) {
final InternalAggregation aggregation = new UnmappedTerms(name, order, bucketCountThresholds.getRequiredSize(), bucketCountThresholds.getShardSize(), bucketCountThresholds.getMinDocCount(), metaData);
return new NonCollectingAggregator(name, aggregationContext, parent, metaData) {
return new NonCollectingAggregator(name, aggregationContext, parent, factories, metaData) {
{
// even in the case of an unmapped aggregator, validate the order
InternalOrder.validate(order, this);
}
@Override
public InternalAggregation buildEmptyAggregation() {
return aggregation;

View File

@ -39,6 +39,7 @@ import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -983,79 +984,83 @@ public class DoubleTermsTests extends AbstractTermsTests {
@Test
public void singleValuedField_OrderedByMissingSubAggregation() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("avg_i", true))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("avg_i", true))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation that doesn't exist");
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation that doesn't exist");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}
@Test
public void singleValuedField_OrderedByNonMetricsOrMultiBucketSubAggregation() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("num_tags", true))
.subAggregation(terms("num_tags").field("num_tags")
.collectMode(randomFrom(SubAggCollectionMode.values())))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("num_tags", true))
.subAggregation(terms("num_tags").field("num_tags")
.collectMode(randomFrom(SubAggCollectionMode.values())))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation which is not of a metrics type");
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation which is not of a metrics type");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}
@Test
public void singleValuedField_OrderedByMultiValuedSubAggregation_WithUknownMetric() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME + "2")
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats.foo", true))
.subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats.foo", true))
.subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"with an unknown specified metric to order by");
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"with an unknown specified metric to order by");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}
@Test
public void singleValuedField_OrderedByMultiValuedSubAggregation_WithoutMetric() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats", true))
.subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats", true))
.subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"where the metric name is not specified");
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"where the metric name is not specified");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}

View File

@ -38,6 +38,7 @@ import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -981,79 +982,83 @@ public class LongTermsTests extends AbstractTermsTests {
@Test
public void singleValuedField_OrderedByMissingSubAggregation() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("avg_i", true))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("avg_i", true))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation that doesn't exist");
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation that doesn't exist");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}
@Test
public void singleValuedField_OrderedByNonMetricsOrMultiBucketSubAggregation() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("num_tags", true))
.subAggregation(terms("num_tags").field("num_tags")
.collectMode(randomFrom(SubAggCollectionMode.values())))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("num_tags", true))
.subAggregation(terms("num_tags").field("num_tags")
.collectMode(randomFrom(SubAggCollectionMode.values())))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation which is not of a metrics type");
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation which is not of a metrics type");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}
@Test
public void singleValuedField_OrderedByMultiValuedSubAggregation_WithUknownMetric() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats.foo", true))
.subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats.foo", true))
.subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"with an unknown specified metric to order by");
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"with an unknown specified metric to order by");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}
@Test
public void singleValuedField_OrderedByMultiValuedSubAggregation_WithoutMetric() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats", true))
.subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats", true))
.subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"where the metric name is not specified");
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"where the metric name is not specified");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}

View File

@ -44,6 +44,7 @@ import org.junit.Test;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -1363,81 +1364,86 @@ public class StringTermsTests extends AbstractTermsTests {
@Test
public void singleValuedField_OrderedByMissingSubAggregation() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.executionHint(randomExecutionHint())
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("avg_i", true))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.executionHint(randomExecutionHint())
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("avg_i", true))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation that doesn't exist");
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation that doesn't exist");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}
@Test
public void singleValuedField_OrderedByNonMetricsOrMultiBucketSubAggregation() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.executionHint(randomExecutionHint())
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("values", true))
.subAggregation(terms("values").field("i")
.collectMode(randomFrom(SubAggCollectionMode.values())))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.executionHint(randomExecutionHint())
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("values", true))
.subAggregation(terms("values").field("i")
.collectMode(randomFrom(SubAggCollectionMode.values())))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation which is not of a metrics or single-bucket type");
fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation which is not of a metrics or single-bucket type");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}
@Test
public void singleValuedField_OrderedByMultiValuedSubAggregation_WithUknownMetric() throws Exception {
try {
SearchResponse response = client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.executionHint(randomExecutionHint())
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats.foo", true))
.subAggregation(stats("stats").field("i"))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"with an unknown specified metric to order by. response had " + response.getFailedShards() + " failed shards.");
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
SearchResponse response = client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.executionHint(randomExecutionHint())
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats.foo", true))
.subAggregation(stats("stats").field("i"))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"with an unknown specified metric to order by. response had " + response.getFailedShards() + " failed shards.");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}
@Test
public void singleValuedField_OrderedByMultiValuedSubAggregation_WithoutMetric() throws Exception {
try {
for (String index : Arrays.asList("idx", "idx_unmapped")) {
try {
client().prepareSearch(index).setTypes("type")
.addAggregation(terms("terms")
.executionHint(randomExecutionHint())
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats", true))
.subAggregation(stats("stats").field("i"))
).execute().actionGet();
client().prepareSearch("idx").setTypes("type")
.addAggregation(terms("terms")
.executionHint(randomExecutionHint())
.field(SINGLE_VALUED_FIELD_NAME)
.collectMode(randomFrom(SubAggCollectionMode.values()))
.order(Terms.Order.aggregation("stats", true))
.subAggregation(stats("stats").field("i"))
).execute().actionGet();
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"where the metric name is not specified");
fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
"where the metric name is not specified");
} catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) {
// expected
}
}
}