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 { 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) { 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() { private void fail() {

View File

@ -147,7 +147,11 @@ public class TermsAggregatorFactory extends ValuesSourceAggregatorFactory<Values
@Override @Override
protected Aggregator createUnmapped(AggregationContext aggregationContext, Aggregator parent, Map<String, Object> metaData) { 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); 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 @Override
public InternalAggregation buildEmptyAggregation() { public InternalAggregation buildEmptyAggregation() {
return aggregation; return aggregation;

View File

@ -39,6 +39,7 @@ import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -870,11 +871,11 @@ public class DoubleTermsTests extends AbstractTermsTests {
assertThat(bucket, notNullValue()); assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (double)i)); assertThat(key(bucket), equalTo("" + (double)i));
assertThat(bucket.getDocCount(), equalTo(1l)); assertThat(bucket.getDocCount(), equalTo(1l));
Avg avg = bucket.getAggregations().get("avg_i"); Avg avg = bucket.getAggregations().get("avg_i");
assertThat(avg, notNullValue()); assertThat(avg, notNullValue());
assertThat(avg.getValue(), equalTo((double) i)); assertThat(avg.getValue(), equalTo((double) i));
Terms subTermsAgg = bucket.getAggregations().get("subTerms"); Terms subTermsAgg = bucket.getAggregations().get("subTerms");
assertThat(subTermsAgg, notNullValue()); assertThat(subTermsAgg, notNullValue());
assertThat(subTermsAgg.getBuckets().size(), equalTo(2)); assertThat(subTermsAgg.getBuckets().size(), equalTo(2));
@ -983,79 +984,83 @@ public class DoubleTermsTests extends AbstractTermsTests {
@Test @Test
public void singleValuedField_OrderedByMissingSubAggregation() throws Exception { 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") fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation that doesn't exist");
.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"); } catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) { }
// expected
} }
} }
@Test @Test
public void singleValuedField_OrderedByNonMetricsOrMultiBucketSubAggregation() throws Exception { 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") fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation which is not of a metrics 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"); } catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) { }
// expected
} }
} }
@Test @Test
public void singleValuedField_OrderedByMultiValuedSubAggregation_WithUknownMetric() throws Exception { 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") fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
.addAggregation(terms("terms") "with an unknown specified metric to order by");
.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 " + } catch (ElasticsearchException e) {
"with an unknown specified metric to order by"); // expected
}
} catch (ElasticsearchException e) {
// expected
} }
} }
@Test @Test
public void singleValuedField_OrderedByMultiValuedSubAggregation_WithoutMetric() throws Exception { 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") fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
.addAggregation(terms("terms") "where the metric name is not specified");
.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 " + } catch (ElasticsearchException e) {
"where the metric name is not specified"); // expected
}
} catch (ElasticsearchException e) {
// expected
} }
} }

View File

@ -38,6 +38,7 @@ import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -868,11 +869,11 @@ public class LongTermsTests extends AbstractTermsTests {
assertThat(bucket, notNullValue()); assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + i)); assertThat(key(bucket), equalTo("" + i));
assertThat(bucket.getDocCount(), equalTo(1l)); assertThat(bucket.getDocCount(), equalTo(1l));
Avg avg = bucket.getAggregations().get("avg_i"); Avg avg = bucket.getAggregations().get("avg_i");
assertThat(avg, notNullValue()); assertThat(avg, notNullValue());
assertThat(avg.getValue(), equalTo((double) i)); assertThat(avg.getValue(), equalTo((double) i));
Terms subTermsAgg = bucket.getAggregations().get("subTerms"); Terms subTermsAgg = bucket.getAggregations().get("subTerms");
assertThat(subTermsAgg, notNullValue()); assertThat(subTermsAgg, notNullValue());
assertThat(subTermsAgg.getBuckets().size(), equalTo(2)); assertThat(subTermsAgg.getBuckets().size(), equalTo(2));
@ -981,79 +982,83 @@ public class LongTermsTests extends AbstractTermsTests {
@Test @Test
public void singleValuedField_OrderedByMissingSubAggregation() throws Exception { 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") fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation that doesn't exist");
.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"); } catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) { }
// expected
} }
} }
@Test @Test
public void singleValuedField_OrderedByNonMetricsOrMultiBucketSubAggregation() throws Exception { 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") fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation which is not of a metrics 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"); } catch (ElasticsearchException e) {
// expected
} catch (ElasticsearchException e) { }
// expected
} }
} }
@Test @Test
public void singleValuedField_OrderedByMultiValuedSubAggregation_WithUknownMetric() throws Exception { 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") fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
.addAggregation(terms("terms") "with an unknown specified metric to order by");
.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 " + } catch (ElasticsearchException e) {
"with an unknown specified metric to order by"); // expected
}
} catch (ElasticsearchException e) {
// expected
} }
} }
@Test @Test
public void singleValuedField_OrderedByMultiValuedSubAggregation_WithoutMetric() throws Exception { 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") fail("Expected search to fail when trying to sort terms aggregation by multi-valued sug-aggregation " +
.addAggregation(terms("terms") "where the metric name is not specified");
.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 " + } catch (ElasticsearchException e) {
"where the metric name is not specified"); // expected
}
} catch (ElasticsearchException e) {
// expected
} }
} }

View File

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