Aggregations: if maxOrd is 0 then use noop collector

Before the OrdinalsCollector was used and this leads to a ArrayIndexOutOfBoundsException

Closes #6413
This commit is contained in:
Martijn van Groningen 2014-06-05 16:57:14 +02:00
parent e15d2e2514
commit 38be1e0dde
2 changed files with 29 additions and 0 deletions

View File

@ -93,6 +93,10 @@ public class CardinalityAggregator extends NumericMetricsAggregator.SingleValue
if (bytesValues instanceof BytesValues.WithOrdinals) {
BytesValues.WithOrdinals values = (BytesValues.WithOrdinals) bytesValues;
final long maxOrd = values.ordinals().getMaxOrd();
if (maxOrd == 0) {
return new EmptyCollector();
}
final long ordinalsMemoryUsage = OrdinalsCollector.memoryOverhead(maxOrd);
final long countsMemoryUsage = HyperLogLogPlusPlus.memoryUsage(precision);
// only use ordinals if they don't increase memory usage by more than 25%
@ -166,6 +170,24 @@ public class CardinalityAggregator extends NumericMetricsAggregator.SingleValue
}
private static class EmptyCollector implements Collector {
@Override
public void collect(int doc, long bucketOrd) {
// no-op
}
@Override
public void postCollect() {
// no-op
}
@Override
public void close() throws ElasticsearchException {
// no-op
}
}
private static class DirectCollector implements Collector {
private final LongValues hashes;

View File

@ -123,6 +123,13 @@ public class CardinalityTests extends ElasticsearchIntegrationTest {
}
indexRandom(true, builders);
createIndex("idx_unmapped");
IndexRequestBuilder[] dummyDocsBuilder = new IndexRequestBuilder[10];
for (int i = 0; i < dummyDocsBuilder.length; i++) {
dummyDocsBuilder[i] = client().prepareIndex("idx", "type").setSource("a_field", "1");
}
indexRandom(true, dummyDocsBuilder);
ensureSearchable();
}