From fb2a48d0f0fbdb58196557df24f148bb3316eba1 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Fri, 17 Jun 2016 17:14:01 +0200 Subject: [PATCH] Revert "Remove support for sorting terms aggregation by ascending count" This is delayed after alpha4 since Kibana relies on it. --- .../bucket/terms/InternalOrder.java | 19 +++- .../bucket/terms/InternalTerms.java | 2 +- .../aggregations/bucket/terms/Terms.java | 6 +- .../bucket/terms/TermsAggregationBuilder.java | 2 +- .../bucket/terms/TermsParser.java | 20 +--- .../aggregations/bucket/ShardSizeTermsIT.java | 24 ++--- .../bucket/TermsDocCountErrorIT.java | 93 +++++++++++++++++++ .../aggregations/bucket/TermsTests.java | 2 +- .../bucket/terms-aggregation.asciidoc | 13 +-- .../migrate_5_0/aggregations.asciidoc | 4 - .../messy/tests/DoubleTermsTests.java | 7 +- .../messy/tests/LongTermsTests.java | 7 +- .../messy/tests/MinDocCountTests.java | 48 ++++++++-- .../messy/tests/StringTermsTests.java | 7 +- .../test/search.aggregation/20_terms.yaml | 15 +-- 15 files changed, 194 insertions(+), 75 deletions(-) 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 5ebeb6ee7fb..f3f87c09dca 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 @@ -46,8 +46,9 @@ import java.util.Objects; class InternalOrder extends Terms.Order { private static final byte COUNT_DESC_ID = 1; - private static final byte TERM_DESC_ID = 2; - private static final byte TERM_ASC_ID = 3; + private static final byte COUNT_ASC_ID = 2; + private static final byte TERM_DESC_ID = 3; + private static final byte TERM_ASC_ID = 4; /** * Order by the (higher) count of each term. @@ -59,6 +60,17 @@ class InternalOrder extends Terms.Order { } }); + /** + * Order by the (lower) count of each term. + */ + public static final InternalOrder COUNT_ASC = new InternalOrder(COUNT_ASC_ID, "_count", true, new Comparator() { + + @Override + public int compare(Terms.Bucket o1, Terms.Bucket o2) { + return Long.compare(o1.getDocCount(), o2.getDocCount()); + } + }); + /** * Order by the terms. */ @@ -81,7 +93,7 @@ class InternalOrder extends Terms.Order { } }); - public static boolean isCountOrder(Terms.Order order) { + public static boolean isCountDesc(Terms.Order order) { if (order == COUNT_DESC) { return true; } else if (order instanceof CompoundOrder) { @@ -336,6 +348,7 @@ class InternalOrder extends Terms.Order { byte id = in.readByte(); switch (id) { case COUNT_DESC_ID: return absoluteOrder ? new CompoundOrder(Collections.singletonList((Terms.Order) InternalOrder.COUNT_DESC)) : InternalOrder.COUNT_DESC; + case COUNT_ASC_ID: return absoluteOrder ? new CompoundOrder(Collections.singletonList((Terms.Order) InternalOrder.COUNT_ASC)) : InternalOrder.COUNT_ASC; case TERM_DESC_ID: return InternalOrder.TERM_DESC; case TERM_ASC_ID: return InternalOrder.TERM_ASC; case Aggregation.ID: 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 5784066f85c..fcbd66dced6 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 @@ -192,7 +192,7 @@ public abstract class InternalTerms otherOptions) throws IOException { - if (token == Token.VALUE_STRING) { + if (token == XContentParser.Token.START_OBJECT) { if (parseFieldMatcher.match(currentFieldName, TermsAggregationBuilder.ORDER_FIELD)) { - if ("_count".equals(parser.text())) { - otherOptions.put(TermsAggregationBuilder.ORDER_FIELD, - Collections.singletonList(new OrderElement(parser.text(), false))); - return true; - } - } - return false; - } else if (token == XContentParser.Token.START_OBJECT) { - if (parseFieldMatcher.match(currentFieldName, TermsAggregationBuilder.ORDER_FIELD)) { - otherOptions.put(TermsAggregationBuilder.ORDER_FIELD, - Collections.singletonList(parseOrderParam(aggregationName, parser))); + otherOptions.put(TermsAggregationBuilder.ORDER_FIELD, Collections.singletonList(parseOrderParam(aggregationName, parser))); return true; } } else if (token == XContentParser.Token.START_ARRAY) { @@ -127,10 +117,6 @@ public class TermsParser extends AbstractTermsParser { } else if (token == XContentParser.Token.VALUE_STRING) { String dir = parser.text(); if ("asc".equalsIgnoreCase(dir)) { - if ("_count".equals(orderKey)) { - throw new ParsingException(parser.getTokenLocation(), - "Sort by ascending _count is not supported in [" + aggregationName + "]."); - } orderAsc = true; } else if ("desc".equalsIgnoreCase(dir)) { orderAsc = false; @@ -181,7 +167,7 @@ public class TermsParser extends AbstractTermsParser { return Order.term(asc); } if ("_count".equals(key)) { - return Order.count(); + return Order.count(asc); } return Order.aggregation(key, asc); } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/ShardSizeTermsIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/ShardSizeTermsIT.java index 82b60289c75..ecc16b85f13 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/ShardSizeTermsIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/ShardSizeTermsIT.java @@ -39,7 +39,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type") .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -62,7 +62,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type") .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3).shardSize(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -86,7 +86,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type") .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -110,7 +110,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type").setRouting(routing1) .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -156,7 +156,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type") .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -179,7 +179,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type") .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3).shardSize(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -202,7 +202,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type") .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -226,7 +226,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type").setRouting(routing1) .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -272,7 +272,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type") .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -295,7 +295,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type") .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3).shardSize(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -318,7 +318,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type") .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); @@ -341,7 +341,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase { SearchResponse response = client().prepareSearch("idx").setTypes("type").setRouting(routing1) .setQuery(matchAllQuery()) .addAggregation(terms("keys").field("key").size(3) - .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count())) + .collectMode(randomFrom(SubAggCollectionMode.values())).shardSize(5).order(Terms.Order.count(false))) .execute().actionGet(); Terms terms = response.getAggregations().get("keys"); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TermsDocCountErrorIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TermsDocCountErrorIT.java index 00c73b42246..6bbe1dc22b2 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TermsDocCountErrorIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TermsDocCountErrorIT.java @@ -271,6 +271,37 @@ public class TermsDocCountErrorIT extends ESIntegTestCase { assertNoDocCountErrorSingleResponse(size, testResponse); } + public void testStringValueFieldDocCountAsc() throws Exception { + int size = randomIntBetween(1, 20); + int shardSize = randomIntBetween(size, size * 2); + SearchResponse accurateResponse = client().prepareSearch("idx_single_shard").setTypes("type") + .addAggregation(terms("terms") + .executionHint(randomExecutionHint()) + .field(STRING_FIELD_NAME) + .showTermDocCountError(true) + .size(10000).shardSize(10000) + .order(Order.count(true)) + .collectMode(randomFrom(SubAggCollectionMode.values()))) + .execute().actionGet(); + + assertSearchResponse(accurateResponse); + + SearchResponse testResponse = client().prepareSearch("idx_single_shard").setTypes("type") + .addAggregation(terms("terms") + .executionHint(randomExecutionHint()) + .field(STRING_FIELD_NAME) + .showTermDocCountError(true) + .size(size) + .shardSize(shardSize) + .order(Order.count(true)) + .collectMode(randomFrom(SubAggCollectionMode.values()))) + .execute().actionGet(); + + assertSearchResponse(testResponse); + + assertUnboundedDocCountError(size, accurateResponse, testResponse); + } + public void testStringValueFieldTermSortAsc() throws Exception { int size = randomIntBetween(1, 20); int shardSize = randomIntBetween(size, size * 2); @@ -476,6 +507,37 @@ public class TermsDocCountErrorIT extends ESIntegTestCase { assertNoDocCountErrorSingleResponse(size, testResponse); } + public void testLongValueFieldDocCountAsc() throws Exception { + int size = randomIntBetween(1, 20); + int shardSize = randomIntBetween(size, size * 2); + SearchResponse accurateResponse = client().prepareSearch("idx_single_shard").setTypes("type") + .addAggregation(terms("terms") + .executionHint(randomExecutionHint()) + .field(LONG_FIELD_NAME) + .showTermDocCountError(true) + .size(10000).shardSize(10000) + .order(Order.count(true)) + .collectMode(randomFrom(SubAggCollectionMode.values()))) + .execute().actionGet(); + + assertSearchResponse(accurateResponse); + + SearchResponse testResponse = client().prepareSearch("idx_single_shard").setTypes("type") + .addAggregation(terms("terms") + .executionHint(randomExecutionHint()) + .field(LONG_FIELD_NAME) + .showTermDocCountError(true) + .size(size) + .shardSize(shardSize) + .order(Order.count(true)) + .collectMode(randomFrom(SubAggCollectionMode.values()))) + .execute().actionGet(); + + assertSearchResponse(testResponse); + + assertUnboundedDocCountError(size, accurateResponse, testResponse); + } + public void testLongValueFieldTermSortAsc() throws Exception { int size = randomIntBetween(1, 20); int shardSize = randomIntBetween(size, size * 2); @@ -681,6 +743,37 @@ public class TermsDocCountErrorIT extends ESIntegTestCase { assertNoDocCountErrorSingleResponse(size, testResponse); } + public void testDoubleValueFieldDocCountAsc() throws Exception { + int size = randomIntBetween(1, 20); + int shardSize = randomIntBetween(size, size * 2); + SearchResponse accurateResponse = client().prepareSearch("idx_single_shard").setTypes("type") + .addAggregation(terms("terms") + .executionHint(randomExecutionHint()) + .field(DOUBLE_FIELD_NAME) + .showTermDocCountError(true) + .size(10000).shardSize(10000) + .order(Order.count(true)) + .collectMode(randomFrom(SubAggCollectionMode.values()))) + .execute().actionGet(); + + assertSearchResponse(accurateResponse); + + SearchResponse testResponse = client().prepareSearch("idx_single_shard").setTypes("type") + .addAggregation(terms("terms") + .executionHint(randomExecutionHint()) + .field(DOUBLE_FIELD_NAME) + .showTermDocCountError(true) + .size(size) + .shardSize(shardSize) + .order(Order.count(true)) + .collectMode(randomFrom(SubAggCollectionMode.values()))) + .execute().actionGet(); + + assertSearchResponse(testResponse); + + assertUnboundedDocCountError(size, accurateResponse, testResponse); + } + public void testDoubleValueFieldTermSortAsc() throws Exception { int size = randomIntBetween(1, 20); int shardSize = randomIntBetween(size, size * 2); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TermsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TermsTests.java index 055258bb4cc..c8a14e82bf4 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TermsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/TermsTests.java @@ -180,7 +180,7 @@ public class TermsTests extends BaseAggregationTestCase orders.add(Terms.Order.term(randomBoolean())); break; case 1: - orders.add(Terms.Order.count()); + orders.add(Terms.Order.count(randomBoolean())); break; case 2: orders.add(Terms.Order.aggregation(randomAsciiOfLengthBetween(3, 20), randomBoolean())); diff --git a/docs/reference/aggregations/bucket/terms-aggregation.asciidoc b/docs/reference/aggregations/bucket/terms-aggregation.asciidoc index 30e3d649179..3c1f4ae860a 100644 --- a/docs/reference/aggregations/bucket/terms-aggregation.asciidoc +++ b/docs/reference/aggregations/bucket/terms-aggregation.asciidoc @@ -239,7 +239,9 @@ determined and is given a value of -1 to indicate this. ==== Order The order of the buckets can be customized by setting the `order` parameter. By default, the buckets are ordered by -their `doc_count` descending: +their `doc_count` descending. It is also possible to change this behaviour as follows: + +Ordering the buckets by their `doc_count` in an ascending manner: [source,js] -------------------------------------------------- @@ -248,15 +250,12 @@ their `doc_count` descending: "genders" : { "terms" : { "field" : "gender", - "order" : "_count" <1> + "order" : { "_count" : "asc" } } } } } -------------------------------------------------- -<1> This is the default if the order is missing. - -It is also possible to change this behaviour as follows: Ordering the buckets alphabetically by their terms in an ascending manner: @@ -313,9 +312,7 @@ Ordering the buckets by multi value metrics sub-aggregation (identified by the a } -------------------------------------------------- -Ordering the buckets by their `doc_count` in an ascending manner is not supported. - -WARNING: Sorting by sub aggregation is discouraged as it increases the +WARNING: Sorting by ascending `_count` or by sub aggregation is discouraged as it increases the <> on document counts. It is fine when a single shard is queried, or when the field that is being aggregated was used as a routing key at index time: in these cases results will be accurate since shards have disjoint diff --git a/docs/reference/migration/migrate_5_0/aggregations.asciidoc b/docs/reference/migration/migrate_5_0/aggregations.asciidoc index 57afd44719b..d9227e91385 100644 --- a/docs/reference/migration/migrate_5_0/aggregations.asciidoc +++ b/docs/reference/migration/migrate_5_0/aggregations.asciidoc @@ -26,7 +26,3 @@ for `from` and `to` anymore. `size: 0` is no longer valid for the terms, significant terms and geohash grid aggregations. Instead a size should be explicitly specified with a number greater than zero. - -==== `order: {"count": "asc"}` on Terms - -The ability to sort the terms aggregation by ascending count has been removed. diff --git a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/DoubleTermsTests.java b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/DoubleTermsTests.java index 5e1dec0402f..6ed77fffcc8 100644 --- a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/DoubleTermsTests.java +++ b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/DoubleTermsTests.java @@ -1048,6 +1048,11 @@ public class DoubleTermsTests extends AbstractTermsTestCase { assertMultiSortResponse(expectedKeys, Terms.Order.aggregation("avg_l", false), Terms.Order.term(true)); } + public void testSingleValuedFieldOrderedByCountAscAndSingleValueSubAggregationAsc() throws Exception { + double[] expectedKeys = new double[] { 6, 7, 3, 4, 5, 1, 2 }; + assertMultiSortResponse(expectedKeys, Terms.Order.count(true), Terms.Order.aggregation("avg_l", true)); + } + public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscSingleValueSubAggregationAsc() throws Exception { double[] expectedKeys = new double[] { 6, 7, 3, 5, 4, 1, 2 }; assertMultiSortResponse(expectedKeys, Terms.Order.aggregation("sum_d", true), Terms.Order.aggregation("avg_l", true)); @@ -1055,7 +1060,7 @@ public class DoubleTermsTests extends AbstractTermsTestCase { public void testSingleValuedFieldOrderedByThreeCriteria() throws Exception { double[] expectedKeys = new double[] { 2, 1, 4, 5, 3, 6, 7 }; - assertMultiSortResponse(expectedKeys, Terms.Order.count(), Terms.Order.aggregation("sum_d", false), Terms.Order.aggregation("avg_l", false)); + assertMultiSortResponse(expectedKeys, Terms.Order.count(false), Terms.Order.aggregation("sum_d", false), Terms.Order.aggregation("avg_l", false)); } public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscAsCompound() throws Exception { diff --git a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/LongTermsTests.java b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/LongTermsTests.java index efc9833a33d..fa049429a3a 100644 --- a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/LongTermsTests.java +++ b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/LongTermsTests.java @@ -1025,6 +1025,11 @@ public class LongTermsTests extends AbstractTermsTestCase { assertMultiSortResponse(expectedKeys, Terms.Order.aggregation("avg_l", false), Terms.Order.term(true)); } + public void testSingleValuedFieldOrderedByCountAscAndSingleValueSubAggregationAsc() throws Exception { + long[] expectedKeys = new long[] { 6, 7, 3, 4, 5, 1, 2 }; + assertMultiSortResponse(expectedKeys, Terms.Order.count(true), Terms.Order.aggregation("avg_l", true)); + } + public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscSingleValueSubAggregationAsc() throws Exception { long[] expectedKeys = new long[] { 6, 7, 3, 5, 4, 1, 2 }; assertMultiSortResponse(expectedKeys, Terms.Order.aggregation("sum_d", true), Terms.Order.aggregation("avg_l", true)); @@ -1032,7 +1037,7 @@ public class LongTermsTests extends AbstractTermsTestCase { public void testSingleValuedFieldOrderedByThreeCriteria() throws Exception { long[] expectedKeys = new long[] { 2, 1, 4, 5, 3, 6, 7 }; - assertMultiSortResponse(expectedKeys, Terms.Order.count(), Terms.Order.aggregation("sum_d", false), Terms.Order.aggregation("avg_l", false)); + assertMultiSortResponse(expectedKeys, Terms.Order.count(false), Terms.Order.aggregation("sum_d", false), Terms.Order.aggregation("avg_l", false)); } public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscAsCompound() throws Exception { diff --git a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/MinDocCountTests.java b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/MinDocCountTests.java index c24551ed588..662d4d2f30c 100644 --- a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/MinDocCountTests.java +++ b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/MinDocCountTests.java @@ -173,20 +173,36 @@ public class MinDocCountTests extends AbstractTermsTestCase { testMinDocCountOnTerms("s", Script.YES, Terms.Order.term(false)); } + public void testStringCountAsc() throws Exception { + testMinDocCountOnTerms("s", Script.NO, Terms.Order.count(true)); + } + + public void testStringScriptCountAsc() throws Exception { + testMinDocCountOnTerms("s", Script.YES, Terms.Order.count(true)); + } + public void testStringCountDesc() throws Exception { - testMinDocCountOnTerms("s", Script.NO, Terms.Order.count()); + testMinDocCountOnTerms("s", Script.NO, Terms.Order.count(false)); } public void testStringScriptCountDesc() throws Exception { - testMinDocCountOnTerms("s", Script.YES, Terms.Order.count()); + testMinDocCountOnTerms("s", Script.YES, Terms.Order.count(false)); + } + + public void testStringCountAscWithInclude() throws Exception { + testMinDocCountOnTerms("s", Script.NO, Terms.Order.count(true), ".*a.*", true); + } + + public void testStringScriptCountAscWithInclude() throws Exception { + testMinDocCountOnTerms("s", Script.YES, Terms.Order.count(true), ".*a.*", true); } public void testStringCountDescWithInclude() throws Exception { - testMinDocCountOnTerms("s", Script.NO, Terms.Order.count(), ".*a.*", true); + testMinDocCountOnTerms("s", Script.NO, Terms.Order.count(false), ".*a.*", true); } public void testStringScriptCountDescWithInclude() throws Exception { - testMinDocCountOnTerms("s", Script.YES, Terms.Order.count(), ".*a.*", true); + testMinDocCountOnTerms("s", Script.YES, Terms.Order.count(false), ".*a.*", true); } public void testLongTermAsc() throws Exception { @@ -205,12 +221,20 @@ public class MinDocCountTests extends AbstractTermsTestCase { testMinDocCountOnTerms("l", Script.YES, Terms.Order.term(false)); } + public void testLongCountAsc() throws Exception { + testMinDocCountOnTerms("l", Script.NO, Terms.Order.count(true)); + } + + public void testLongScriptCountAsc() throws Exception { + testMinDocCountOnTerms("l", Script.YES, Terms.Order.count(true)); + } + public void testLongCountDesc() throws Exception { - testMinDocCountOnTerms("l", Script.NO, Terms.Order.count()); + testMinDocCountOnTerms("l", Script.NO, Terms.Order.count(false)); } public void testLongScriptCountDesc() throws Exception { - testMinDocCountOnTerms("l", Script.YES, Terms.Order.count()); + testMinDocCountOnTerms("l", Script.YES, Terms.Order.count(false)); } public void testDoubleTermAsc() throws Exception { @@ -229,12 +253,20 @@ public class MinDocCountTests extends AbstractTermsTestCase { testMinDocCountOnTerms("d", Script.YES, Terms.Order.term(false)); } + public void testDoubleCountAsc() throws Exception { + testMinDocCountOnTerms("d", Script.NO, Terms.Order.count(true)); + } + + public void testDoubleScriptCountAsc() throws Exception { + testMinDocCountOnTerms("d", Script.YES, Terms.Order.count(true)); + } + public void testDoubleCountDesc() throws Exception { - testMinDocCountOnTerms("d", Script.NO, Terms.Order.count()); + testMinDocCountOnTerms("d", Script.NO, Terms.Order.count(false)); } public void testDoubleScriptCountDesc() throws Exception { - testMinDocCountOnTerms("d", Script.YES, Terms.Order.count()); + testMinDocCountOnTerms("d", Script.YES, Terms.Order.count(false)); } private void testMinDocCountOnTerms(String field, Script script, Terms.Order order) throws Exception { diff --git a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/StringTermsTests.java b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/StringTermsTests.java index a5825d23172..b098db154da 100644 --- a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/StringTermsTests.java +++ b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/StringTermsTests.java @@ -1375,6 +1375,11 @@ public class StringTermsTests extends AbstractTermsTestCase { assertMultiSortResponse(expectedKeys, Terms.Order.aggregation("avg_l", false), Terms.Order.term(true)); } + public void testSingleValuedFieldOrderedByCountAscAndSingleValueSubAggregationAsc() throws Exception { + String[] expectedKeys = new String[] { "val6", "val7", "val3", "val4", "val5", "val1", "val2" }; + assertMultiSortResponse(expectedKeys, Terms.Order.count(true), Terms.Order.aggregation("avg_l", true)); + } + public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscSingleValueSubAggregationAsc() throws Exception { String[] expectedKeys = new String[] { "val6", "val7", "val3", "val5", "val4", "val1", "val2" }; assertMultiSortResponse(expectedKeys, Terms.Order.aggregation("sum_d", true), Terms.Order.aggregation("avg_l", true)); @@ -1382,7 +1387,7 @@ public class StringTermsTests extends AbstractTermsTestCase { public void testSingleValuedFieldOrderedByThreeCriteria() throws Exception { String[] expectedKeys = new String[] { "val2", "val1", "val4", "val5", "val3", "val6", "val7" }; - assertMultiSortResponse(expectedKeys, Terms.Order.count(), Terms.Order.aggregation("sum_d", false), + assertMultiSortResponse(expectedKeys, Terms.Order.count(false), Terms.Order.aggregation("sum_d", false), Terms.Order.aggregation("avg_l", false)); } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/20_terms.yaml b/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/20_terms.yaml index c079e6b6f7e..c35e79e6cfe 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/20_terms.yaml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/20_terms.yaml @@ -350,17 +350,4 @@ setup: - match: { aggregations.date_terms.buckets.0.key_as_string: "2014-09-01T00:00:00.000Z" } - - match: { aggregations.date_terms.buckets.0.doc_count: 1 } - ---- -"Terms count order test": - - do: - catch: /Sort by ascending _count is not supported in/ - search: - body: { "size" : 0, "aggs" : { "count_order" : { "terms" : { "field" : "f1", "order": {"_count": "asc"} } } } } - - - do: - search: - body: { "size" : 0, "aggs" : { "count_order" : { "terms" : { "field" : "f1", "order": "_count" } } } } - - + - match: { aggregations.date_terms.buckets.0.doc_count: 1 }