Search in sources :

Example 1 with AvgAggregationBuilder

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;
}
Also used : AvgAggregationBuilder(org.opensearch.search.aggregations.metrics.AvgAggregationBuilder)

Example 2 with AvgAggregationBuilder

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;
}
Also used : AvgAggregationBuilder(org.opensearch.search.aggregations.metrics.AvgAggregationBuilder)

Example 3 with AvgAggregationBuilder

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);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Aggregations(org.opensearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) Aggregation(org.opensearch.search.aggregations.Aggregation) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) InternalAvg(org.opensearch.search.aggregations.metrics.InternalAvg) InternalDateHistogram(org.opensearch.search.aggregations.bucket.histogram.InternalDateHistogram) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) AvgAggregationBuilder(org.opensearch.search.aggregations.metrics.AvgAggregationBuilder) Directory(org.apache.lucene.store.Directory) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) DateHistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 4 with AvgAggregationBuilder

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);
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) HistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Document(org.apache.lucene.document.Document) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) AvgAggregationBuilder(org.opensearch.search.aggregations.metrics.AvgAggregationBuilder) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 5 with AvgAggregationBuilder

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);
}
Also used : Script(org.opensearch.script.Script) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Document(org.apache.lucene.document.Document) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) AvgAggregationBuilder(org.opensearch.search.aggregations.metrics.AvgAggregationBuilder) BytesRef(org.apache.lucene.util.BytesRef) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Aggregations

AvgAggregationBuilder (org.opensearch.search.aggregations.metrics.AvgAggregationBuilder)10 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 Query (org.apache.lucene.search.Query)5 DateHistogramAggregationBuilder (org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder)5 Document (org.apache.lucene.document.Document)4 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)3 IndexReader (org.apache.lucene.index.IndexReader)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 Directory (org.apache.lucene.store.Directory)3 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)3 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 DateFieldMapper (org.opensearch.index.mapper.DateFieldMapper)2 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)2 Script (org.opensearch.script.Script)2 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)2 InternalDateHistogram (org.opensearch.search.aggregations.bucket.histogram.InternalDateHistogram)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1