This PR migrates most tests from AvgIT integration test to AvgAggregatorTests, as described in #42893
This commit is contained in:
parent
2171b6b47f
commit
c091b6c004
|
@ -19,11 +19,13 @@
|
|||
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.document.IntPoint;
|
||||
import org.apache.lucene.document.NumericDocValuesField;
|
||||
import org.apache.lucene.document.SortedNumericDocValuesField;
|
||||
import org.apache.lucene.index.DirectoryReader;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.MultiReader;
|
||||
import org.apache.lucene.index.RandomIndexWriter;
|
||||
import org.apache.lucene.search.DocValuesFieldExistsQuery;
|
||||
import org.apache.lucene.search.IndexSearcher;
|
||||
|
@ -32,19 +34,95 @@ import org.apache.lucene.search.Query;
|
|||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.NumericUtils;
|
||||
import org.elasticsearch.common.CheckedConsumer;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.index.mapper.MappedFieldType;
|
||||
import org.elasticsearch.index.mapper.NumberFieldMapper;
|
||||
import org.elasticsearch.script.MockScriptEngine;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptEngine;
|
||||
import org.elasticsearch.script.ScriptModule;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.script.ScriptType;
|
||||
import org.elasticsearch.search.aggregations.AggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||
import org.elasticsearch.search.aggregations.AggregatorTestCase;
|
||||
import org.elasticsearch.search.aggregations.BucketOrder;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator;
|
||||
import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper;
|
||||
import org.elasticsearch.search.aggregations.support.ValueType;
|
||||
import org.elasticsearch.search.lookup.LeafDocLookup;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static java.util.Collections.singleton;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
|
||||
public class AvgAggregatorTests extends AggregatorTestCase {
|
||||
|
||||
/** Script to take a field name in params and sum the values of the field. */
|
||||
public static final String SUM_FIELD_PARAMS_SCRIPT = "sum_field_params";
|
||||
|
||||
/** Script to sum the values of a field named {@code values}. */
|
||||
public static final String SUM_VALUES_FIELD_SCRIPT = "sum_values_field";
|
||||
|
||||
/** Script to return the value of a field named {@code value}. */
|
||||
public static final String VALUE_FIELD_SCRIPT = "value_field";
|
||||
|
||||
/** Script to return the {@code _value} provided by aggs framework. */
|
||||
public static final String VALUE_SCRIPT = "_value";
|
||||
|
||||
@Override
|
||||
protected ScriptService getMockScriptService() {
|
||||
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
|
||||
Function<Map<String, Object>, Integer> getInc = vars -> {
|
||||
if (vars == null || vars.containsKey("inc") == false) {
|
||||
return 0;
|
||||
} else {
|
||||
return ((Number) vars.get("inc")).intValue();
|
||||
}
|
||||
};
|
||||
|
||||
BiFunction<Map<String, Object>, String, Object> sum = (vars, fieldname) -> {
|
||||
int inc = getInc.apply(vars);
|
||||
LeafDocLookup docLookup = (LeafDocLookup) vars.get("doc");
|
||||
List<Long> values = new ArrayList<>();
|
||||
for (Object v : docLookup.get(fieldname)) {
|
||||
values.add(((Number) v).longValue() + inc);
|
||||
}
|
||||
return values;
|
||||
};
|
||||
|
||||
scripts.put(SUM_FIELD_PARAMS_SCRIPT, vars -> {
|
||||
String fieldname = (String) vars.get("field");
|
||||
return sum.apply(vars, fieldname);
|
||||
});
|
||||
scripts.put(SUM_VALUES_FIELD_SCRIPT, vars -> sum.apply(vars, "values"));
|
||||
scripts.put(VALUE_FIELD_SCRIPT, vars -> sum.apply(vars, "value"));
|
||||
scripts.put(VALUE_SCRIPT, vars -> {
|
||||
int inc = getInc.apply(vars);
|
||||
return ((Number) vars.get("_value")).doubleValue() + inc;
|
||||
});
|
||||
|
||||
MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME,
|
||||
scripts,
|
||||
Collections.emptyMap());
|
||||
Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine);
|
||||
|
||||
return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
|
||||
}
|
||||
|
||||
public void testNoDocs() throws IOException {
|
||||
testCase(new MatchAllDocsQuery(), iw -> {
|
||||
// Intentionally not writing any docs
|
||||
|
@ -139,28 +217,361 @@ public class AvgAggregatorTests extends AggregatorTestCase {
|
|||
verifyAvgOfDoubles(largeValues, Double.NEGATIVE_INFINITY, 0d);
|
||||
}
|
||||
|
||||
public void testUnmappedField() throws IOException {
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
|
||||
testCase(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> {
|
||||
iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
|
||||
iw.addDocument(singleton(new NumericDocValuesField("number", 1)));
|
||||
}, avg -> {
|
||||
assertEquals(Double.NaN, avg.getValue(), 0);
|
||||
assertFalse(AggregationInspectionHelper.hasValue(avg));
|
||||
}, null);
|
||||
}
|
||||
|
||||
public void testUnmappedWithMissingField() throws IOException {
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number").missing(0L);
|
||||
testCase(aggregationBuilder, new DocValuesFieldExistsQuery("number"), iw -> {
|
||||
iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
|
||||
iw.addDocument(singleton(new NumericDocValuesField("number", 1)));
|
||||
}, avg -> {
|
||||
assertEquals(0.0, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
}, null);
|
||||
}
|
||||
|
||||
private void verifyAvgOfDoubles(double[] values, double expected, double delta) throws IOException {
|
||||
testCase(new MatchAllDocsQuery(),
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
|
||||
fieldType.setName("number");
|
||||
testCase(aggregationBuilder, new MatchAllDocsQuery(),
|
||||
iw -> {
|
||||
for (double value : values) {
|
||||
iw.addDocument(singleton(new NumericDocValuesField("number", NumericUtils.doubleToSortableLong(value))));
|
||||
}
|
||||
},
|
||||
avg -> assertEquals(expected, avg.getValue(), delta),
|
||||
NumberFieldMapper.NumberType.DOUBLE
|
||||
fieldType
|
||||
);
|
||||
}
|
||||
|
||||
public void testSingleValuedFieldPartiallyUnmapped() throws IOException {
|
||||
Directory directory = newDirectory();
|
||||
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
|
||||
indexWriter.addDocument(singleton(new NumericDocValuesField("number", 7)));
|
||||
indexWriter.addDocument(singleton(new NumericDocValuesField("number", 2)));
|
||||
indexWriter.addDocument(singleton(new NumericDocValuesField("number", 3)));
|
||||
indexWriter.close();
|
||||
|
||||
Directory unmappedDirectory = newDirectory();
|
||||
RandomIndexWriter unmappedIndexWriter = new RandomIndexWriter(random(), unmappedDirectory);
|
||||
unmappedIndexWriter.close();
|
||||
|
||||
IndexReader indexReader = DirectoryReader.open(directory);
|
||||
IndexReader unamappedIndexReader = DirectoryReader.open(unmappedDirectory);
|
||||
MultiReader multiReader = new MultiReader(indexReader, unamappedIndexReader);
|
||||
IndexSearcher indexSearcher = newSearcher(multiReader, true, true);
|
||||
|
||||
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("number");
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
|
||||
|
||||
AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
|
||||
aggregator.preCollection();
|
||||
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
|
||||
aggregator.postCollection();
|
||||
|
||||
InternalAvg avg = (InternalAvg) aggregator.buildAggregation(0L);
|
||||
|
||||
assertEquals(4, avg.getValue(), 0);
|
||||
assertEquals(3, avg.getCount(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
|
||||
multiReader.close();
|
||||
directory.close();
|
||||
unmappedDirectory.close();
|
||||
}
|
||||
|
||||
public void testSingleValuedField() throws IOException {
|
||||
testCase(new MatchAllDocsQuery(), iw -> {
|
||||
iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
|
||||
iw.addDocument(singleton(new NumericDocValuesField("number", 2)));
|
||||
iw.addDocument(singleton(new NumericDocValuesField("number", 3)));
|
||||
}, avg -> {
|
||||
assertEquals(4, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
assertEquals(4.0, avg.getProperty("value"));
|
||||
});
|
||||
}
|
||||
|
||||
public void testSingleValuedField_WithFormatter() throws IOException {
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("value");
|
||||
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
|
||||
.format("#")
|
||||
.field("value")
|
||||
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));
|
||||
|
||||
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
|
||||
}
|
||||
}, avg -> {
|
||||
assertEquals((double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10, avg.getValue(),0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
assertEquals("6", avg.getValueAsString());
|
||||
}, fieldType);
|
||||
}
|
||||
|
||||
public void testSingleValuedFieldWithValueScript() throws IOException {
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("value");
|
||||
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
|
||||
.field("value")
|
||||
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));
|
||||
|
||||
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
|
||||
}
|
||||
}, avg -> {
|
||||
assertEquals((double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
}, fieldType);
|
||||
}
|
||||
|
||||
public void testScriptSingleValued() throws IOException {
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("value");
|
||||
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
|
||||
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_FIELD_SCRIPT, Collections.emptyMap()));
|
||||
|
||||
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
|
||||
}
|
||||
}, avg -> {
|
||||
assertEquals((double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
}, fieldType);
|
||||
}
|
||||
|
||||
public void testScriptSingleValuedWithParams() throws IOException {
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("value");
|
||||
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("inc", 1);
|
||||
params.put("field", "value");
|
||||
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
|
||||
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params));
|
||||
|
||||
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
|
||||
}
|
||||
}, avg -> {
|
||||
assertEquals((double) (2+3+4+5+6+7+8+9+10+11) / 10, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
}, fieldType);
|
||||
}
|
||||
|
||||
public void testMultiValuedField() throws IOException {
|
||||
testCase(new MatchAllDocsQuery(), iw -> {
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
Document document = new Document();
|
||||
document.add(new SortedNumericDocValuesField("number", i + 2));
|
||||
document.add(new SortedNumericDocValuesField("number", i + 3));
|
||||
iw.addDocument(document);
|
||||
}
|
||||
}, avg -> {
|
||||
assertEquals((2+3+3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12) / 20, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
});
|
||||
}
|
||||
|
||||
public void testScriptMultiValued() throws IOException {
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("values");
|
||||
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
|
||||
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_VALUES_FIELD_SCRIPT, Collections.emptyMap()));
|
||||
|
||||
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
Document document = new Document();
|
||||
document.add(new SortedNumericDocValuesField("values", i + 2));
|
||||
document.add(new SortedNumericDocValuesField("values", i + 3));
|
||||
iw.addDocument(document);
|
||||
}
|
||||
}, avg -> {
|
||||
assertEquals((double) (2+3+3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12) / 20, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
}, fieldType);
|
||||
}
|
||||
|
||||
public void testScriptMultiValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("inc", 1);
|
||||
params.put("field", "values");
|
||||
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("values");
|
||||
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
|
||||
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params));
|
||||
|
||||
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
Document document = new Document();
|
||||
document.add(new SortedNumericDocValuesField("values", i + 2));
|
||||
document.add(new SortedNumericDocValuesField("values", i + 3));
|
||||
iw.addDocument(document);
|
||||
}
|
||||
}, avg -> {
|
||||
assertEquals((double) (3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12+12+13) / 20, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
}, fieldType);
|
||||
}
|
||||
|
||||
public void testSingleValuedFieldWithValueScriptWithParams() throws IOException {
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("value");
|
||||
|
||||
Map<String, Object> params = Collections.singletonMap("inc", 1);
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
|
||||
.field("value")
|
||||
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, params));
|
||||
|
||||
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
iw.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
|
||||
}
|
||||
}, avg -> {
|
||||
assertEquals((double) (2+3+4+5+6+7+8+9+10+11) / 10, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
}, fieldType);
|
||||
}
|
||||
|
||||
public void testMultiValuedFieldWithValueScriptWithParams() throws IOException {
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("values");
|
||||
|
||||
Map<String, Object> params = Collections.singletonMap("inc", 1);
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
|
||||
.field("values")
|
||||
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, params));
|
||||
|
||||
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
Document document = new Document();
|
||||
document.add(new SortedNumericDocValuesField("values", i + 2));
|
||||
document.add(new SortedNumericDocValuesField("values", i + 3));
|
||||
iw.addDocument(document); }
|
||||
}, avg -> {
|
||||
assertEquals((double) (3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12+12+13) / 20, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
}, fieldType);
|
||||
}
|
||||
|
||||
public void testMultiValuedFieldWithValueScript() throws IOException {
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("values");
|
||||
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name")
|
||||
.field("values")
|
||||
.script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap()));
|
||||
|
||||
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> {
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
Document document = new Document();
|
||||
document.add(new SortedNumericDocValuesField("values", i + 2));
|
||||
document.add(new SortedNumericDocValuesField("values", i + 3));
|
||||
iw.addDocument(document); }
|
||||
}, avg -> {
|
||||
assertEquals((double) (2+3+3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12) / 20, avg.getValue(), 0);
|
||||
assertTrue(AggregationInspectionHelper.hasValue(avg));
|
||||
}, fieldType);
|
||||
}
|
||||
|
||||
public void testOrderByEmptyAggregation() throws IOException {
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("value");
|
||||
fieldType.setHasDocValues(true);
|
||||
|
||||
AggregationBuilder aggregationBuilder = new TermsAggregationBuilder("terms", ValueType.NUMERIC)
|
||||
.field("value")
|
||||
.order(BucketOrder.compound(BucketOrder.aggregation("filter>avg", true)))
|
||||
.subAggregation(AggregationBuilders.filter("filter", termQuery("value", 100))
|
||||
.subAggregation(AggregationBuilders.avg("avg").field("value")));
|
||||
|
||||
Directory directory = newDirectory();
|
||||
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
|
||||
final int numDocs = 10;
|
||||
for (int i = 0; i < numDocs; i++) {
|
||||
indexWriter.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
|
||||
}
|
||||
indexWriter.close();
|
||||
|
||||
IndexReader indexReader = DirectoryReader.open(directory);
|
||||
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
|
||||
|
||||
TermsAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
|
||||
aggregator.preCollection();
|
||||
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
|
||||
aggregator.postCollection();
|
||||
|
||||
Terms terms = (Terms) aggregator.buildAggregation(0L);
|
||||
assertNotNull(terms);
|
||||
List<? extends Terms.Bucket> buckets = terms.getBuckets();
|
||||
assertNotNull(buckets);
|
||||
assertEquals(10, buckets.size());
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Terms.Bucket bucket = buckets.get(i);
|
||||
assertNotNull(bucket);
|
||||
assertEquals((long) i + 1, bucket.getKeyAsNumber());
|
||||
assertEquals(1L, bucket.getDocCount());
|
||||
|
||||
Filter filter = bucket.getAggregations().get("filter");
|
||||
assertNotNull(filter);
|
||||
assertEquals(0L, filter.getDocCount());
|
||||
|
||||
Avg avg = filter.getAggregations().get("avg");
|
||||
assertNotNull(avg);
|
||||
assertEquals(Double.NaN, avg.getValue(), 0);
|
||||
}
|
||||
|
||||
indexReader.close();
|
||||
directory.close();
|
||||
}
|
||||
|
||||
private void testCase(Query query,
|
||||
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
|
||||
Consumer<InternalAvg> verify) throws IOException {
|
||||
testCase(query, buildIndex, verify, NumberFieldMapper.NumberType.LONG);
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER);
|
||||
fieldType.setName("number");
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
|
||||
testCase(aggregationBuilder, query, buildIndex, verify, fieldType);
|
||||
}
|
||||
|
||||
private void testCase(Query query,
|
||||
private void testCase(AvgAggregationBuilder aggregationBuilder, Query query,
|
||||
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
|
||||
Consumer<InternalAvg> verify,
|
||||
NumberFieldMapper.NumberType fieldNumberType) throws IOException {
|
||||
Consumer<InternalAvg> verify, MappedFieldType fieldType) throws IOException {
|
||||
Directory directory = newDirectory();
|
||||
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
|
||||
buildIndex.accept(indexWriter);
|
||||
|
@ -169,10 +580,6 @@ public class AvgAggregatorTests extends AggregatorTestCase {
|
|||
IndexReader indexReader = DirectoryReader.open(directory);
|
||||
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
|
||||
|
||||
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
|
||||
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(fieldNumberType);
|
||||
fieldType.setName("number");
|
||||
|
||||
AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
|
||||
aggregator.preCollection();
|
||||
indexSearcher.search(query, aggregator);
|
||||
|
|
|
@ -18,334 +18,30 @@
|
|||
*/
|
||||
package org.elasticsearch.search.aggregations.metrics;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptType;
|
||||
import org.elasticsearch.search.aggregations.BucketOrder;
|
||||
import org.elasticsearch.search.aggregations.InternalAggregation;
|
||||
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
|
||||
import org.elasticsearch.search.aggregations.bucket.global.Global;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.avg;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.filter;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.global;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.histogram;
|
||||
import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
|
||||
import static org.elasticsearch.search.aggregations.metrics.MetricAggScriptPlugin.METRIC_SCRIPT_ENGINE;
|
||||
import static org.elasticsearch.search.aggregations.metrics.MetricAggScriptPlugin.VALUE_FIELD_SCRIPT;
|
||||
import static org.elasticsearch.search.aggregations.metrics.MetricAggScriptPlugin.SUM_FIELD_PARAMS_SCRIPT;
|
||||
import static org.elasticsearch.search.aggregations.metrics.MetricAggScriptPlugin.SUM_VALUES_FIELD_SCRIPT;
|
||||
import static org.elasticsearch.search.aggregations.metrics.MetricAggScriptPlugin.VALUE_SCRIPT;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
||||
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
public class AvgIT extends AbstractNumericTestCase {
|
||||
public class AvgIT extends ESIntegTestCase {
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> nodePlugins() {
|
||||
return Collections.singleton(MetricAggScriptPlugin.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testEmptyAggregation() throws Exception {
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0).subAggregation(avg("avg").field("value")))
|
||||
.get();
|
||||
|
||||
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L));
|
||||
Histogram histo = searchResponse.getAggregations().get("histo");
|
||||
assertThat(histo, notNullValue());
|
||||
Histogram.Bucket bucket = histo.getBuckets().get(1);
|
||||
assertThat(bucket, notNullValue());
|
||||
|
||||
Avg avg = bucket.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(Double.isNaN(avg.getValue()), is(true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testUnmapped() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx_unmapped")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg").field("value"))
|
||||
.get();
|
||||
|
||||
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L));
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo(Double.NaN));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testSingleValuedField() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg").field("value"))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (1+2+3+4+5+6+7+8+9+10) / 10));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testSingleValuedFieldGetProperty() throws Exception {
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery())
|
||||
.addAggregation(global("global").subAggregation(avg("avg").field("value"))).get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Global global = searchResponse.getAggregations().get("global");
|
||||
assertThat(global, notNullValue());
|
||||
assertThat(global.getName(), equalTo("global"));
|
||||
assertThat(global.getDocCount(), equalTo(10L));
|
||||
assertThat(global.getAggregations(), notNullValue());
|
||||
assertThat(global.getAggregations().asMap().size(), equalTo(1));
|
||||
|
||||
Avg avg = global.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
double expectedAvgValue = (double) (1+2+3+4+5+6+7+8+9+10) / 10;
|
||||
assertThat(avg.getValue(), equalTo(expectedAvgValue));
|
||||
assertThat((Avg) ((InternalAggregation)global).getProperty("avg"), equalTo(avg));
|
||||
assertThat((double) ((InternalAggregation)global).getProperty("avg.value"), equalTo(expectedAvgValue));
|
||||
assertThat((double) ((InternalAggregation)avg).getProperty("value"), equalTo(expectedAvgValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testSingleValuedFieldPartiallyUnmapped() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx", "idx_unmapped")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg").field("value"))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (1+2+3+4+5+6+7+8+9+10) / 10));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testSingleValuedFieldWithValueScript() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg").field("value")
|
||||
.script(new Script(ScriptType.INLINE, METRIC_SCRIPT_ENGINE, VALUE_SCRIPT, Collections.emptyMap())))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (1+2+3+4+5+6+7+8+9+10) / 10));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testSingleValuedFieldWithValueScriptWithParams() throws Exception {
|
||||
Map<String, Object> params = Collections.singletonMap("inc", 1);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg").field("value")
|
||||
.script(new Script(ScriptType.INLINE, METRIC_SCRIPT_ENGINE, VALUE_SCRIPT, params)))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (2+3+4+5+6+7+8+9+10+11) / 10));
|
||||
}
|
||||
|
||||
public void testSingleValuedField_WithFormatter() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg").format("#").field("value")).get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10));
|
||||
assertThat(avg.getValueAsString(), equalTo("6"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testMultiValuedField() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg").field("values"))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (2+3+3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12) / 20));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testMultiValuedFieldWithValueScript() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg").field("values")
|
||||
.script(new Script(ScriptType.INLINE, METRIC_SCRIPT_ENGINE, VALUE_SCRIPT, Collections.emptyMap())))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (2+3+3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12) / 20));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testMultiValuedFieldWithValueScriptWithParams() throws Exception {
|
||||
Map<String, Object> params = Collections.singletonMap("inc", 1);
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg").field("values")
|
||||
.script(new Script(ScriptType.INLINE, METRIC_SCRIPT_ENGINE, VALUE_SCRIPT, params)))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12+12+13) / 20));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testScriptSingleValued() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg")
|
||||
.script(new Script(ScriptType.INLINE, METRIC_SCRIPT_ENGINE, VALUE_FIELD_SCRIPT, Collections.emptyMap())))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (1+2+3+4+5+6+7+8+9+10) / 10));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testScriptSingleValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("inc", 1);
|
||||
params.put("field", "value");
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg")
|
||||
.script(new Script(ScriptType.INLINE, METRIC_SCRIPT_ENGINE, SUM_FIELD_PARAMS_SCRIPT, params)))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (2+3+4+5+6+7+8+9+10+11) / 10));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testScriptMultiValued() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg")
|
||||
.script(new Script(ScriptType.INLINE, METRIC_SCRIPT_ENGINE, SUM_VALUES_FIELD_SCRIPT, Collections.emptyMap())))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (2+3+3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12) / 20));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testScriptMultiValuedWithParams() throws Exception {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("inc", 1);
|
||||
params.put("field", "values");
|
||||
SearchResponse searchResponse = client().prepareSearch("idx")
|
||||
.setQuery(matchAllQuery())
|
||||
.addAggregation(avg("avg")
|
||||
.script(new Script(ScriptType.INLINE, METRIC_SCRIPT_ENGINE, SUM_FIELD_PARAMS_SCRIPT, params)))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Avg avg = searchResponse.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.getName(), equalTo("avg"));
|
||||
assertThat(avg.getValue(), equalTo((double) (3+4+4+5+5+6+6+7+7+8+8+9+9+10+10+11+11+12+12+13) / 20));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testOrderByEmptyAggregation() throws Exception {
|
||||
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery())
|
||||
.addAggregation(terms("terms").field("value").order(BucketOrder.compound(BucketOrder.aggregation("filter>avg", true)))
|
||||
.subAggregation(filter("filter", termQuery("value", 100)).subAggregation(avg("avg").field("value"))))
|
||||
.get();
|
||||
|
||||
assertHitCount(searchResponse, 10);
|
||||
|
||||
Terms terms = searchResponse.getAggregations().get("terms");
|
||||
assertThat(terms, notNullValue());
|
||||
List<? extends Terms.Bucket> buckets = terms.getBuckets();
|
||||
assertThat(buckets, notNullValue());
|
||||
assertThat(buckets.size(), equalTo(10));
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Terms.Bucket bucket = buckets.get(i);
|
||||
assertThat(bucket, notNullValue());
|
||||
assertThat(bucket.getKeyAsNumber(), equalTo((long) i + 1));
|
||||
assertThat(bucket.getDocCount(), equalTo(1L));
|
||||
Filter filter = bucket.getAggregations().get("filter");
|
||||
assertThat(filter, notNullValue());
|
||||
assertThat(filter.getDocCount(), equalTo(0L));
|
||||
Avg avg = filter.getAggregations().get("avg");
|
||||
assertThat(avg, notNullValue());
|
||||
assertThat(avg.value(), equalTo(Double.NaN));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that a request using a script does not get cached and a request
|
||||
* not using a script does get cached.
|
||||
|
|
Loading…
Reference in New Issue