Skip to content

Commit 3043eb5

Browse files
committed
Aggregations Refactor: Refactor Value Count Aggregation
1 parent db0d20e commit 3043eb5

File tree

3 files changed

+109
-26
lines changed

3 files changed

+109
-26
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountAggregator.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
package org.elasticsearch.search.aggregations.metrics.valuecount;
2020

2121
import org.apache.lucene.index.LeafReaderContext;
22+
import org.elasticsearch.common.io.stream.StreamInput;
23+
import org.elasticsearch.common.io.stream.StreamOutput;
2224
import org.elasticsearch.common.lease.Releasables;
2325
import org.elasticsearch.common.util.BigArrays;
2426
import org.elasticsearch.common.util.LongArray;
27+
import org.elasticsearch.common.xcontent.XContentBuilder;
2528
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
2629
import org.elasticsearch.search.aggregations.Aggregator;
2730
import org.elasticsearch.search.aggregations.InternalAggregation;
@@ -30,9 +33,10 @@
3033
import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator;
3134
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
3235
import org.elasticsearch.search.aggregations.support.AggregationContext;
36+
import org.elasticsearch.search.aggregations.support.ValueType;
3337
import org.elasticsearch.search.aggregations.support.ValuesSource;
3438
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
35-
import org.elasticsearch.search.aggregations.support.ValuesSourceParser;
39+
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
3640
import org.elasticsearch.search.aggregations.support.format.ValueFormatter;
3741

3842
import java.io.IOException;
@@ -110,8 +114,8 @@ public void doClose() {
110114

111115
public static class Factory<VS extends ValuesSource> extends ValuesSourceAggregatorFactory.LeafOnly<VS> {
112116

113-
public Factory(String name, ValuesSourceParser.Input<VS> input) {
114-
super(name, InternalValueCount.TYPE.name(), input);
117+
public Factory(String name, ValuesSourceType valuesSourceType, ValueType valueType) {
118+
super(name, InternalValueCount.TYPE.name(), valuesSourceType, valueType);
115119
}
116120

117121
@Override
@@ -128,6 +132,32 @@ protected Aggregator doCreateInternal(VS valuesSource, AggregationContext aggreg
128132
metaData);
129133
}
130134

135+
@Override
136+
protected ValuesSourceAggregatorFactory<VS> innerReadFrom(String name, ValuesSourceType valuesSourceType,
137+
ValueType targetValueType, StreamInput in) {
138+
return new ValueCountAggregator.Factory<VS>(name, valuesSourceType, targetValueType);
139+
}
140+
141+
@Override
142+
protected void innerWriteTo(StreamOutput out) {
143+
// Do nothing, no extra state to write to stream
144+
}
145+
146+
@Override
147+
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
148+
return builder;
149+
}
150+
151+
@Override
152+
protected int innerHashCode() {
153+
return 0;
154+
}
155+
156+
@Override
157+
protected boolean innerEquals(Object obj) {
158+
return true;
159+
}
160+
131161
}
132162

133163
}

core/src/main/java/org/elasticsearch/search/aggregations/metrics/valuecount/ValueCountParser.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,47 @@
1818
*/
1919
package org.elasticsearch.search.aggregations.metrics.valuecount;
2020

21+
import org.elasticsearch.common.ParseField;
22+
import org.elasticsearch.common.ParseFieldMatcher;
2123
import org.elasticsearch.common.xcontent.XContentParser;
22-
import org.elasticsearch.search.SearchParseException;
23-
import org.elasticsearch.search.aggregations.Aggregator;
2424
import org.elasticsearch.search.aggregations.AggregatorFactory;
25-
import org.elasticsearch.search.aggregations.support.ValuesSourceParser;
26-
import org.elasticsearch.search.internal.SearchContext;
25+
import org.elasticsearch.search.aggregations.support.AbstractValuesSourceParser.AnyValuesSourceParser;
26+
import org.elasticsearch.search.aggregations.support.ValueType;
27+
import org.elasticsearch.search.aggregations.support.ValuesSource;
28+
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
29+
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
2730

2831
import java.io.IOException;
32+
import java.util.Map;
2933

3034
/**
3135
*
3236
*/
33-
public class ValueCountParser implements Aggregator.Parser {
37+
public class ValueCountParser extends AnyValuesSourceParser {
38+
39+
public ValueCountParser() {
40+
super(true, true);
41+
}
3442

3543
@Override
3644
public String type() {
3745
return InternalValueCount.TYPE.name();
3846
}
3947

4048
@Override
41-
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
42-
43-
ValuesSourceParser vsParser = ValuesSourceParser.any(aggregationName, InternalValueCount.TYPE, context)
44-
.build();
45-
46-
XContentParser.Token token;
47-
String currentFieldName = null;
48-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
49-
if (token == XContentParser.Token.FIELD_NAME) {
50-
currentFieldName = parser.currentName();
51-
} else if (!vsParser.token(currentFieldName, token, parser)) {
52-
throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
53-
parser.getTokenLocation());
54-
}
55-
}
49+
protected boolean token(String aggregationName, String currentFieldName, XContentParser.Token token, XContentParser parser,
50+
ParseFieldMatcher parseFieldMatcher, Map<ParseField, Object> otherOptions) throws IOException {
51+
return false;
52+
}
5653

57-
return new ValueCountAggregator.Factory(aggregationName, vsParser.input());
54+
@Override
55+
protected ValuesSourceAggregatorFactory<ValuesSource> createFactory(String aggregationName, ValuesSourceType valuesSourceType,
56+
ValueType targetValueType, Map<ParseField, Object> otherOptions) {
57+
return new ValueCountAggregator.Factory<ValuesSource>(aggregationName, valuesSourceType, targetValueType);
5858
}
5959

60-
// NORELEASE implement this method when refactoring this aggregation
6160
@Override
6261
public AggregatorFactory getFactoryPrototype() {
63-
return null;
62+
return new ValueCountAggregator.Factory<ValuesSource>(null, null, null);
6463
}
6564
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.search.aggregations.metrics;
21+
22+
import org.elasticsearch.script.Script;
23+
import org.elasticsearch.search.aggregations.BaseAggregationTestCase;
24+
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregator;
25+
import org.elasticsearch.search.aggregations.support.ValuesSource;
26+
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
27+
28+
public class ValueCountTests extends BaseAggregationTestCase<ValueCountAggregator.Factory<? extends ValuesSource>> {
29+
30+
@Override
31+
protected final ValueCountAggregator.Factory<? extends ValuesSource> createTestAggregatorFactory() {
32+
ValueCountAggregator.Factory<ValuesSource> factory = new ValueCountAggregator.Factory<ValuesSource>("foo", ValuesSourceType.ANY,
33+
null);
34+
String field = randomNumericField();
35+
int randomFieldBranch = randomInt(3);
36+
switch (randomFieldBranch) {
37+
case 0:
38+
factory.field(field);
39+
break;
40+
case 1:
41+
factory.field(field);
42+
factory.script(new Script("_value + 1"));
43+
break;
44+
case 2:
45+
factory.script(new Script("doc[" + field + "] + 1"));
46+
break;
47+
}
48+
if (randomBoolean()) {
49+
factory.missing("MISSING");
50+
}
51+
return factory;
52+
}
53+
54+
}

0 commit comments

Comments
 (0)