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 9d0a5a4 commit adc50e0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ public Type type() {
return TYPE;
}

@Override
public double getValue(String name) {
return value(name);
}

@Override
public long getDocCount(String name) {
return (countsMap.get(name) != null) ? countsMap.get(name) : 0;
}
Expand Down Expand Up @@ -215,7 +213,7 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th


public final static AggregationStreams.Stream STREAM = new AggregationStreams.Stream() {
@Override

public InternalMultipleMetric readResult(StreamInput in) throws IOException {
InternalMultipleMetric result = new InternalMultipleMetric();
result.readFrom(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBuc
if (valuesSourceMap == null) {
return LeafBucketCollector.NO_OP_COLLECTOR;
}

final BigArrays bigArrays = context.bigArrays();

final Map<String, SortedNumericDoubleValues> doubleValuesMap = new HashMap<String, SortedNumericDoubleValues>();
Expand Down Expand Up @@ -204,9 +204,20 @@ 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);

return new InternalMultipleMetric(name, metricParamsMap, scriptParamsMap, countsMap, pipelineAggregators(), metaData());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ public class MultipleMetricParser implements Aggregator.Parser {

public static final String SUM_OPERATOR = "sum";
public static final String COUNT_OPERATOR = "count";

@Override

public String type() {
return InternalMultipleMetric.TYPE.name();
}

public static boolean isValidOperator(String operator) {
return (operator.equals(SUM_OPERATOR) || operator.equals(COUNT_OPERATOR));
}

@Override

public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

XContentParser.Token token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import fr.v3d.elasticsearch.search.aggregations.metrics.multiplemetric.SumBuilder;

import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -69,6 +70,45 @@ public void assertMultipleMetricAggregation() {
assertEquals(metrics.getDocCount("value2"), 10);
}


@Test
public void assertMultipleMetricAggregationWithEmptyFilter() {
String indexName = "test1";
int size = 1;

Map<String, Integer> termsFactor = new HashMap<String, Integer>();
termsFactor.put("foo", 1);
termsFactor.put("bar", 1);

buildTestDataset(1, indexName, "type1", size, termsFactor);

TermsBuilder termsBuilder = new TermsBuilder("group_by")
.field("field0")
.subAggregation(new MultipleMetricBuilder("metrics")
.field(new SumBuilder("value1")
.field("value1")
.filter(new RangeQueryBuilder("value1").lt(0))
)
);

SearchResponse searchResponse = client().prepareSearch(indexName)
.setQuery(matchAllQuery()) //termQuery("field0", "foo")
.addAggregation(termsBuilder)
.execute().actionGet();

Terms terms = searchResponse.getAggregations().get("group_by");
for (Map.Entry<String, Integer> entry: termsFactor.entrySet()) {
String term = entry.getKey();
assertNotNull(terms.getBucketByKey(term));
assertNotNull(terms.getBucketByKey(term).getAggregations());
assertNotNull(terms.getBucketByKey(term).getAggregations().get("metrics"));

MultipleMetric metrics = terms.getBucketByKey(term).getAggregations().get("metrics");
assertEquals(metrics.getValue("value1"), 0.0, 0.0);
assertEquals(metrics.getDocCount("value1"), 0);
}
}

@Test
public void assertMultipleMetricAggregationWithScriptError() {
String indexName = "test1";
Expand Down

0 comments on commit adc50e0

Please sign in to comment.