Revert "Remove support for sorting terms aggregation by ascending count"
This is delayed after alpha4 since Kibana relies on it.
This commit is contained in:
parent
bdb6dcea3a
commit
fb2a48d0f0
|
@ -46,8 +46,9 @@ import java.util.Objects;
|
||||||
class InternalOrder extends Terms.Order {
|
class InternalOrder extends Terms.Order {
|
||||||
|
|
||||||
private static final byte COUNT_DESC_ID = 1;
|
private static final byte COUNT_DESC_ID = 1;
|
||||||
private static final byte TERM_DESC_ID = 2;
|
private static final byte COUNT_ASC_ID = 2;
|
||||||
private static final byte TERM_ASC_ID = 3;
|
private static final byte TERM_DESC_ID = 3;
|
||||||
|
private static final byte TERM_ASC_ID = 4;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Order by the (higher) count of each term.
|
* 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<Terms.Bucket>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compare(Terms.Bucket o1, Terms.Bucket o2) {
|
||||||
|
return Long.compare(o1.getDocCount(), o2.getDocCount());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Order by the terms.
|
* 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) {
|
if (order == COUNT_DESC) {
|
||||||
return true;
|
return true;
|
||||||
} else if (order instanceof CompoundOrder) {
|
} else if (order instanceof CompoundOrder) {
|
||||||
|
@ -336,6 +348,7 @@ class InternalOrder extends Terms.Order {
|
||||||
byte id = in.readByte();
|
byte id = in.readByte();
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case COUNT_DESC_ID: return absoluteOrder ? new CompoundOrder(Collections.singletonList((Terms.Order) InternalOrder.COUNT_DESC)) : InternalOrder.COUNT_DESC;
|
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_DESC_ID: return InternalOrder.TERM_DESC;
|
||||||
case TERM_ASC_ID: return InternalOrder.TERM_ASC;
|
case TERM_ASC_ID: return InternalOrder.TERM_ASC;
|
||||||
case Aggregation.ID:
|
case Aggregation.ID:
|
||||||
|
|
|
@ -192,7 +192,7 @@ public abstract class InternalTerms<A extends InternalTerms, B extends InternalT
|
||||||
final long thisAggDocCountError;
|
final long thisAggDocCountError;
|
||||||
if (terms.buckets.size() < this.shardSize || InternalOrder.isTermOrder(order)) {
|
if (terms.buckets.size() < this.shardSize || InternalOrder.isTermOrder(order)) {
|
||||||
thisAggDocCountError = 0;
|
thisAggDocCountError = 0;
|
||||||
} else if (InternalOrder.isCountOrder(this.order)) {
|
} else if (InternalOrder.isCountDesc(this.order)) {
|
||||||
thisAggDocCountError = terms.buckets.get(terms.buckets.size() - 1).docCount;
|
thisAggDocCountError = terms.buckets.get(terms.buckets.size() - 1).docCount;
|
||||||
} else {
|
} else {
|
||||||
thisAggDocCountError = -1;
|
thisAggDocCountError = -1;
|
||||||
|
|
|
@ -100,10 +100,10 @@ public interface Terms extends MultiBucketsAggregation {
|
||||||
static abstract class Order implements ToXContent {
|
static abstract class Order implements ToXContent {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return a bucket ordering strategy that sorts buckets by their document counts (descending)
|
* @return a bucket ordering strategy that sorts buckets by their document counts (ascending or descending)
|
||||||
*/
|
*/
|
||||||
public static Order count() {
|
public static Order count(boolean asc) {
|
||||||
return InternalOrder.COUNT_DESC;
|
return asc ? InternalOrder.COUNT_ASC : InternalOrder.COUNT_DESC;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -53,7 +53,7 @@ public class TermsAggregationBuilder extends ValuesSourceAggregationBuilder<Valu
|
||||||
public static final ParseField SHOW_TERM_DOC_COUNT_ERROR = new ParseField("show_term_doc_count_error");
|
public static final ParseField SHOW_TERM_DOC_COUNT_ERROR = new ParseField("show_term_doc_count_error");
|
||||||
public static final ParseField ORDER_FIELD = new ParseField("order");
|
public static final ParseField ORDER_FIELD = new ParseField("order");
|
||||||
|
|
||||||
private Terms.Order order = Terms.Order.compound(Terms.Order.count(), Terms.Order.term(true));
|
private Terms.Order order = Terms.Order.compound(Terms.Order.count(false), Terms.Order.term(true));
|
||||||
private IncludeExclude includeExclude = null;
|
private IncludeExclude includeExclude = null;
|
||||||
private String executionHint = null;
|
private String executionHint = null;
|
||||||
private SubAggCollectionMode collectMode = null;
|
private SubAggCollectionMode collectMode = null;
|
||||||
|
|
|
@ -77,19 +77,9 @@ public class TermsParser extends AbstractTermsParser {
|
||||||
@Override
|
@Override
|
||||||
public boolean parseSpecial(String aggregationName, XContentParser parser, ParseFieldMatcher parseFieldMatcher, Token token,
|
public boolean parseSpecial(String aggregationName, XContentParser parser, ParseFieldMatcher parseFieldMatcher, Token token,
|
||||||
String currentFieldName, Map<ParseField, Object> otherOptions) throws IOException {
|
String currentFieldName, Map<ParseField, Object> otherOptions) throws IOException {
|
||||||
if (token == Token.VALUE_STRING) {
|
if (token == XContentParser.Token.START_OBJECT) {
|
||||||
if (parseFieldMatcher.match(currentFieldName, TermsAggregationBuilder.ORDER_FIELD)) {
|
if (parseFieldMatcher.match(currentFieldName, TermsAggregationBuilder.ORDER_FIELD)) {
|
||||||
if ("_count".equals(parser.text())) {
|
otherOptions.put(TermsAggregationBuilder.ORDER_FIELD, Collections.singletonList(parseOrderParam(aggregationName, parser)));
|
||||||
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)));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||||
|
@ -127,10 +117,6 @@ public class TermsParser extends AbstractTermsParser {
|
||||||
} else if (token == XContentParser.Token.VALUE_STRING) {
|
} else if (token == XContentParser.Token.VALUE_STRING) {
|
||||||
String dir = parser.text();
|
String dir = parser.text();
|
||||||
if ("asc".equalsIgnoreCase(dir)) {
|
if ("asc".equalsIgnoreCase(dir)) {
|
||||||
if ("_count".equals(orderKey)) {
|
|
||||||
throw new ParsingException(parser.getTokenLocation(),
|
|
||||||
"Sort by ascending _count is not supported in [" + aggregationName + "].");
|
|
||||||
}
|
|
||||||
orderAsc = true;
|
orderAsc = true;
|
||||||
} else if ("desc".equalsIgnoreCase(dir)) {
|
} else if ("desc".equalsIgnoreCase(dir)) {
|
||||||
orderAsc = false;
|
orderAsc = false;
|
||||||
|
@ -181,7 +167,7 @@ public class TermsParser extends AbstractTermsParser {
|
||||||
return Order.term(asc);
|
return Order.term(asc);
|
||||||
}
|
}
|
||||||
if ("_count".equals(key)) {
|
if ("_count".equals(key)) {
|
||||||
return Order.count();
|
return Order.count(asc);
|
||||||
}
|
}
|
||||||
return Order.aggregation(key, asc);
|
return Order.aggregation(key, asc);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -62,7 +62,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3).shardSize(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -86,7 +86,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -110,7 +110,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type").setRouting(routing1)
|
SearchResponse response = client().prepareSearch("idx").setTypes("type").setRouting(routing1)
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -156,7 +156,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -179,7 +179,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3).shardSize(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -202,7 +202,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -226,7 +226,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type").setRouting(routing1)
|
SearchResponse response = client().prepareSearch("idx").setTypes("type").setRouting(routing1)
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -272,7 +272,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -295,7 +295,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3).shardSize(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -318,7 +318,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
SearchResponse response = client().prepareSearch("idx").setTypes("type")
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
@ -341,7 +341,7 @@ public class ShardSizeTermsIT extends ShardSizeTestCase {
|
||||||
SearchResponse response = client().prepareSearch("idx").setTypes("type").setRouting(routing1)
|
SearchResponse response = client().prepareSearch("idx").setTypes("type").setRouting(routing1)
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addAggregation(terms("keys").field("key").size(3)
|
.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();
|
.execute().actionGet();
|
||||||
|
|
||||||
Terms terms = response.getAggregations().get("keys");
|
Terms terms = response.getAggregations().get("keys");
|
||||||
|
|
|
@ -271,6 +271,37 @@ public class TermsDocCountErrorIT extends ESIntegTestCase {
|
||||||
assertNoDocCountErrorSingleResponse(size, testResponse);
|
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 {
|
public void testStringValueFieldTermSortAsc() throws Exception {
|
||||||
int size = randomIntBetween(1, 20);
|
int size = randomIntBetween(1, 20);
|
||||||
int shardSize = randomIntBetween(size, size * 2);
|
int shardSize = randomIntBetween(size, size * 2);
|
||||||
|
@ -476,6 +507,37 @@ public class TermsDocCountErrorIT extends ESIntegTestCase {
|
||||||
assertNoDocCountErrorSingleResponse(size, testResponse);
|
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 {
|
public void testLongValueFieldTermSortAsc() throws Exception {
|
||||||
int size = randomIntBetween(1, 20);
|
int size = randomIntBetween(1, 20);
|
||||||
int shardSize = randomIntBetween(size, size * 2);
|
int shardSize = randomIntBetween(size, size * 2);
|
||||||
|
@ -681,6 +743,37 @@ public class TermsDocCountErrorIT extends ESIntegTestCase {
|
||||||
assertNoDocCountErrorSingleResponse(size, testResponse);
|
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 {
|
public void testDoubleValueFieldTermSortAsc() throws Exception {
|
||||||
int size = randomIntBetween(1, 20);
|
int size = randomIntBetween(1, 20);
|
||||||
int shardSize = randomIntBetween(size, size * 2);
|
int shardSize = randomIntBetween(size, size * 2);
|
||||||
|
|
|
@ -180,7 +180,7 @@ public class TermsTests extends BaseAggregationTestCase<TermsAggregationBuilder>
|
||||||
orders.add(Terms.Order.term(randomBoolean()));
|
orders.add(Terms.Order.term(randomBoolean()));
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
orders.add(Terms.Order.count());
|
orders.add(Terms.Order.count(randomBoolean()));
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
orders.add(Terms.Order.aggregation(randomAsciiOfLengthBetween(3, 20), randomBoolean()));
|
orders.add(Terms.Order.aggregation(randomAsciiOfLengthBetween(3, 20), randomBoolean()));
|
||||||
|
|
|
@ -239,7 +239,9 @@ determined and is given a value of -1 to indicate this.
|
||||||
==== Order
|
==== Order
|
||||||
|
|
||||||
The order of the buckets can be customized by setting the `order` parameter. By default, the buckets are ordered by
|
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]
|
[source,js]
|
||||||
--------------------------------------------------
|
--------------------------------------------------
|
||||||
|
@ -248,15 +250,12 @@ their `doc_count` descending:
|
||||||
"genders" : {
|
"genders" : {
|
||||||
"terms" : {
|
"terms" : {
|
||||||
"field" : "gender",
|
"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:
|
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 ascending `_count` or by sub aggregation is discouraged as it increases the
|
||||||
|
|
||||||
WARNING: Sorting by sub aggregation is discouraged as it increases the
|
|
||||||
<<search-aggregations-bucket-terms-aggregation-approximate-counts,error>> on document counts.
|
<<search-aggregations-bucket-terms-aggregation-approximate-counts,error>> on document counts.
|
||||||
It is fine when a single shard is queried, or when the field that is being aggregated was used
|
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
|
as a routing key at index time: in these cases results will be accurate since shards have disjoint
|
||||||
|
|
|
@ -26,7 +26,3 @@ for `from` and `to` anymore.
|
||||||
`size: 0` is no longer valid for the terms, significant terms and geohash grid
|
`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
|
aggregations. Instead a size should be explicitly specified with a number greater
|
||||||
than zero.
|
than zero.
|
||||||
|
|
||||||
==== `order: {"count": "asc"}` on Terms
|
|
||||||
|
|
||||||
The ability to sort the terms aggregation by ascending count has been removed.
|
|
||||||
|
|
|
@ -1048,6 +1048,11 @@ public class DoubleTermsTests extends AbstractTermsTestCase {
|
||||||
assertMultiSortResponse(expectedKeys, Terms.Order.aggregation("avg_l", false), Terms.Order.term(true));
|
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 {
|
public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscSingleValueSubAggregationAsc() throws Exception {
|
||||||
double[] expectedKeys = new double[] { 6, 7, 3, 5, 4, 1, 2 };
|
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));
|
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 {
|
public void testSingleValuedFieldOrderedByThreeCriteria() throws Exception {
|
||||||
double[] expectedKeys = new double[] { 2, 1, 4, 5, 3, 6, 7 };
|
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 {
|
public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscAsCompound() throws Exception {
|
||||||
|
|
|
@ -1025,6 +1025,11 @@ public class LongTermsTests extends AbstractTermsTestCase {
|
||||||
assertMultiSortResponse(expectedKeys, Terms.Order.aggregation("avg_l", false), Terms.Order.term(true));
|
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 {
|
public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscSingleValueSubAggregationAsc() throws Exception {
|
||||||
long[] expectedKeys = new long[] { 6, 7, 3, 5, 4, 1, 2 };
|
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));
|
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 {
|
public void testSingleValuedFieldOrderedByThreeCriteria() throws Exception {
|
||||||
long[] expectedKeys = new long[] { 2, 1, 4, 5, 3, 6, 7 };
|
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 {
|
public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscAsCompound() throws Exception {
|
||||||
|
|
|
@ -173,20 +173,36 @@ public class MinDocCountTests extends AbstractTermsTestCase {
|
||||||
testMinDocCountOnTerms("s", Script.YES, Terms.Order.term(false));
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
public void testLongTermAsc() throws Exception {
|
||||||
|
@ -205,12 +221,20 @@ public class MinDocCountTests extends AbstractTermsTestCase {
|
||||||
testMinDocCountOnTerms("l", Script.YES, Terms.Order.term(false));
|
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 {
|
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 {
|
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 {
|
public void testDoubleTermAsc() throws Exception {
|
||||||
|
@ -229,12 +253,20 @@ public class MinDocCountTests extends AbstractTermsTestCase {
|
||||||
testMinDocCountOnTerms("d", Script.YES, Terms.Order.term(false));
|
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 {
|
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 {
|
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 {
|
private void testMinDocCountOnTerms(String field, Script script, Terms.Order order) throws Exception {
|
||||||
|
|
|
@ -1375,6 +1375,11 @@ public class StringTermsTests extends AbstractTermsTestCase {
|
||||||
assertMultiSortResponse(expectedKeys, Terms.Order.aggregation("avg_l", false), Terms.Order.term(true));
|
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 {
|
public void testSingleValuedFieldOrderedBySingleValueSubAggregationAscSingleValueSubAggregationAsc() throws Exception {
|
||||||
String[] expectedKeys = new String[] { "val6", "val7", "val3", "val5", "val4", "val1", "val2" };
|
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));
|
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 {
|
public void testSingleValuedFieldOrderedByThreeCriteria() throws Exception {
|
||||||
String[] expectedKeys = new String[] { "val2", "val1", "val4", "val5", "val3", "val6", "val7" };
|
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));
|
Terms.Order.aggregation("avg_l", false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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.key_as_string: "2014-09-01T00:00:00.000Z" }
|
||||||
|
|
||||||
- match: { aggregations.date_terms.buckets.0.doc_count: 1 }
|
- 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" } } } }
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue