Skip to content

Commit

Permalink
Fix bug on empty bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
Elie Prudhomme committed Aug 18, 2016
1 parent adc50e0 commit 668721c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBuc
final Map<String, SortedNumericDoubleValues> doubleValuesMap = new HashMap<String, SortedNumericDoubleValues>();
final Map<String, SortedBinaryDocValues> docValuesMap = new HashMap<String, SortedBinaryDocValues>();
final Map<String, Bits> bitsMap = new HashMap<String, Bits>();

for (Entry<String, ValuesSource> entry: valuesSourceMap.entrySet()) {
String key = entry.getKey();
logger.error("{} {}", key, entry.getValue());
Bits bits = Lucene.asSequentialAccessBits(ctx.reader().maxDoc(), weightMap.get(key).scorer(ctx));
bitsMap.put(key, bits);

Expand Down Expand Up @@ -164,8 +165,12 @@ private HashMap<String, Double> getScriptParamsMap(long owningBucketOrdinal) {

private Map<String, Long> getCountsMap(long owningBucketOrdinal) {
HashMap<String, Long> countsMap = new HashMap<String, Long>();
for (Map.Entry<String, LongArray> entry: metricCountsMap.entrySet())
countsMap.put(entry.getKey(), entry.getValue().get(owningBucketOrdinal));
for (Map.Entry<String, LongArray> entry: metricCountsMap.entrySet()) {
long count = (owningBucketOrdinal >= entry.getValue().size())
? 0L
: entry.getValue().get(owningBucketOrdinal);
countsMap.put(entry.getKey(), count);
}

return countsMap;
}
Expand Down Expand Up @@ -204,16 +209,6 @@ public double metric(String name, long owningBucketOrdinal) {

@Override
public InternalAggregation buildAggregation(long owningBucketOrdinal) {
long maxSize = -1L;
for (Entry<String, LongArray> e: metricCountsMap.entrySet()) {
LongArray counts = e.getValue();
if (counts.size() > maxSize)
maxSize = counts.size();
}

if (owningBucketOrdinal >= maxSize) {
return buildEmptyAggregation();
}

HashMap<String, Double> scriptParamsMap = getScriptParamsMap(owningBucketOrdinal);
Map<String, Long> countsMap = getCountsMap(owningBucketOrdinal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public void assertMultipleMetricAggregationWithEmptyFilter() {
.field("value1")
.filter(new RangeQueryBuilder("value1").lt(0))
)
.field(new CountBuilder("value1c")
.field("value1")
)
);

SearchResponse searchResponse = client().prepareSearch(indexName)
Expand All @@ -106,6 +109,8 @@ public void assertMultipleMetricAggregationWithEmptyFilter() {
MultipleMetric metrics = terms.getBucketByKey(term).getAggregations().get("metrics");
assertEquals(metrics.getValue("value1"), 0.0, 0.0);
assertEquals(metrics.getDocCount("value1"), 0);
assertEquals(metrics.getValue("value1c"), 10.0, 0.0);
assertEquals(metrics.getDocCount("value1c"), 10);
}
}

Expand Down

0 comments on commit 668721c

Please sign in to comment.