use of org.opensearch.search.aggregations.metrics.AvgAggregationBuilder in project fess by codelibs.
the class EsAbstractConditionAggregation method regAvgA.
// ===================================================================================
// Register
// ========
protected AvgAggregationBuilder regAvgA(String name, String field) {
AvgAggregationBuilder builder = AggregationBuilders.avg(name).field(field);
regA(builder);
return builder;
}
use of org.opensearch.search.aggregations.metrics.AvgAggregationBuilder in project fess by codelibs.
the class EsAbstractConditionAggregation method regAvgA.
// ===================================================================================
// Register
// ========
protected AvgAggregationBuilder regAvgA(String name, String field) {
AvgAggregationBuilder builder = AggregationBuilders.avg(name).field(field);
regA(builder);
return builder;
}
use of org.opensearch.search.aggregations.metrics.AvgAggregationBuilder in project OpenSearch by opensearch-project.
the class AvgBucketAggregatorTests method testSameAggNames.
/**
* Test for issue #30608. Under the following circumstances:
*
* A. Multi-bucket agg in the first entry of our internal list
* B. Regular agg as the immediate child of the multi-bucket in A
* C. Regular agg with the same name as B at the top level, listed as the second entry in our internal list
* D. Finally, a pipeline agg with the path down to B
*
* BucketMetrics reduction would throw a class cast exception due to bad subpathing. This test ensures
* it is fixed.
*
* Note: we have this test inside of the `avg_bucket` package so that we can get access to the package-private
* `reduce()` needed for testing this
*/
public void testSameAggNames() throws IOException {
Query query = new MatchAllDocsQuery();
AvgAggregationBuilder avgBuilder = new AvgAggregationBuilder("foo").field(VALUE_FIELD);
DateHistogramAggregationBuilder histo = new DateHistogramAggregationBuilder("histo").calendarInterval(DateHistogramInterval.YEAR).field(DATE_FIELD).subAggregation(new AvgAggregationBuilder("foo").field(VALUE_FIELD));
AvgBucketPipelineAggregationBuilder avgBucketBuilder = new AvgBucketPipelineAggregationBuilder("the_avg_bucket", "histo>foo");
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
Document document = new Document();
for (String date : dataset) {
if (frequently()) {
indexWriter.commit();
}
document.add(new SortedNumericDocValuesField(DATE_FIELD, asLong(date)));
document.add(new SortedNumericDocValuesField(VALUE_FIELD, randomInt()));
indexWriter.addDocument(document);
document.clear();
}
}
InternalAvg avgResult;
InternalDateHistogram histogramResult;
try (IndexReader indexReader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(DATE_FIELD);
MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD, NumberFieldMapper.NumberType.LONG);
avgResult = searchAndReduce(indexSearcher, query, avgBuilder, 10000, new MappedFieldType[] { fieldType, valueFieldType });
histogramResult = searchAndReduce(indexSearcher, query, histo, 10000, new MappedFieldType[] { fieldType, valueFieldType });
}
// Finally, reduce the pipeline agg
PipelineAggregator avgBucketAgg = avgBucketBuilder.createInternal(Collections.emptyMap());
List<Aggregation> reducedAggs = new ArrayList<>(2);
// Histo has to go first to exercise the bug
reducedAggs.add(histogramResult);
reducedAggs.add(avgResult);
Aggregations aggregations = new Aggregations(reducedAggs);
InternalAggregation pipelineResult = ((AvgBucketPipelineAggregator) avgBucketAgg).doReduce(aggregations, null);
assertNotNull(pipelineResult);
}
}
use of org.opensearch.search.aggregations.metrics.AvgAggregationBuilder in project OpenSearch by opensearch-project.
the class DerivativeAggregatorTests method testAvgMovavgDerivNPE.
public void testAvgMovavgDerivNPE() throws IOException {
try (Directory directory = newDirectory()) {
Query query = new MatchAllDocsQuery();
HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("histo").field("tick").interval(1);
aggBuilder.subAggregation(new AvgAggregationBuilder("avg").field("value"));
aggBuilder.subAggregation(new MovAvgPipelineAggregationBuilder("movavg", "avg").modelBuilder(new SimpleModel.SimpleModelBuilder()).window(3));
aggBuilder.subAggregation(new DerivativePipelineAggregationBuilder("deriv", "movavg"));
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
Document document = new Document();
for (int i = 0; i < 10; i++) {
if (frequently()) {
indexWriter.commit();
}
if (i != 1 || i != 3) {
document.add(new NumericDocValuesField("value", i));
}
document.add(new NumericDocValuesField("tick", i));
indexWriter.addDocument(document);
document.clear();
}
indexWriter.commit();
}
try (IndexReader indexReader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
searchAndReduce(indexSearcher, query, aggBuilder);
}
}
}
use of org.opensearch.search.aggregations.metrics.AvgAggregationBuilder in project OpenSearch by opensearch-project.
the class BucketScriptAggregatorTests method testScript.
public void testScript() throws IOException {
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number_field", NumberFieldMapper.NumberType.INTEGER);
MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("the_field");
FiltersAggregationBuilder filters = new FiltersAggregationBuilder("placeholder", new MatchAllQueryBuilder()).subAggregation(new TermsAggregationBuilder("the_terms").userValueTypeHint(ValueType.STRING).field("the_field").subAggregation(new AvgAggregationBuilder("the_avg").field("number_field"))).subAggregation(new BucketScriptPipelineAggregationBuilder("bucket_script", Collections.singletonMap("the_avg", "the_terms['test1']>the_avg.value"), new Script(ScriptType.INLINE, MockScriptEngine.NAME, SCRIPT_NAME, Collections.emptyMap())));
testCase(filters, new MatchAllDocsQuery(), iw -> {
Document doc = new Document();
doc.add(new SortedSetDocValuesField("the_field", new BytesRef("test1")));
doc.add(new SortedNumericDocValuesField("number_field", 19));
iw.addDocument(doc);
doc = new Document();
doc.add(new SortedSetDocValuesField("the_field", new BytesRef("test2")));
doc.add(new SortedNumericDocValuesField("number_field", 55));
iw.addDocument(doc);
}, f -> {
assertThat(((InternalSimpleValue) (f.getBuckets().get(0).getAggregations().get("bucket_script"))).value, equalTo(19.0));
}, fieldType, fieldType1);
}
Aggregations