Search in sources :

Example 1 with BucketScriptPipelineAggregationBuilder

use of org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder in project OpenSearch by opensearch-project.

the class AggregatorFactoriesTests method testRewriteAggregation.

public void testRewriteAggregation() throws Exception {
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference bytesReference;
    try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) {
        builder.startObject();
        {
            builder.startObject("terms");
            {
                builder.array("title", "foo");
            }
            builder.endObject();
        }
        builder.endObject();
        bytesReference = BytesReference.bytes(builder);
    }
    FilterAggregationBuilder filterAggBuilder = new FilterAggregationBuilder("titles", new WrapperQueryBuilder(bytesReference));
    BucketScriptPipelineAggregationBuilder pipelineAgg = new BucketScriptPipelineAggregationBuilder("const", new Script("1"));
    AggregatorFactories.Builder builder = new AggregatorFactories.Builder().addAggregator(filterAggBuilder).addPipelineAggregator(pipelineAgg);
    AggregatorFactories.Builder rewritten = builder.rewrite(new QueryRewriteContext(xContentRegistry, null, null, () -> 0L));
    assertNotSame(builder, rewritten);
    Collection<AggregationBuilder> aggregatorFactories = rewritten.getAggregatorFactories();
    assertEquals(1, aggregatorFactories.size());
    assertThat(aggregatorFactories.iterator().next(), instanceOf(FilterAggregationBuilder.class));
    FilterAggregationBuilder rewrittenFilterAggBuilder = (FilterAggregationBuilder) aggregatorFactories.iterator().next();
    assertNotSame(filterAggBuilder, rewrittenFilterAggBuilder);
    assertNotEquals(filterAggBuilder, rewrittenFilterAggBuilder);
    // Check the filter was rewritten from a wrapper query to a terms query
    QueryBuilder rewrittenFilter = rewrittenFilterAggBuilder.getFilter();
    assertThat(rewrittenFilter, instanceOf(TermsQueryBuilder.class));
    // Check that a further rewrite returns the same aggregation factories builder
    AggregatorFactories.Builder secondRewritten = rewritten.rewrite(new QueryRewriteContext(xContentRegistry, null, null, () -> 0L));
    assertSame(rewritten, secondRewritten);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) Script(org.opensearch.script.Script) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) AbstractPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder) BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.AbstractPipelineAggregationBuilder) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) XContentType(org.opensearch.common.xcontent.XContentType) QueryRewriteContext(org.opensearch.index.query.QueryRewriteContext) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) WrapperQueryBuilder(org.opensearch.index.query.WrapperQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 2 with BucketScriptPipelineAggregationBuilder

use of org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder in project OpenSearch by opensearch-project.

the class NestedAggregatorTests method testNestedWithPipeline.

/**
 * This tests to make sure pipeline aggs embedded under a SingleBucket agg (like nested)
 * are properly reduced
 */
public void testNestedWithPipeline() throws IOException {
    int numRootDocs = randomIntBetween(1, 20);
    int expectedNestedDocs = 0;
    double expectedMaxValue = Double.NEGATIVE_INFINITY;
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
            for (int i = 0; i < numRootDocs; i++) {
                List<Document> documents = new ArrayList<>();
                expectedMaxValue = Math.max(expectedMaxValue, generateMaxDocs(documents, 1, i, NESTED_OBJECT, VALUE_FIELD_NAME));
                expectedNestedDocs += 1;
                Document document = new Document();
                document.add(new Field(IdFieldMapper.NAME, Uid.encodeId(Integer.toString(i)), IdFieldMapper.Defaults.FIELD_TYPE));
                document.add(sequenceIDFields.primaryTerm);
                documents.add(document);
                iw.addDocuments(documents);
            }
            iw.commit();
        }
        try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
            NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder(NESTED_AGG, NESTED_OBJECT).subAggregation(new TermsAggregationBuilder("terms").field(VALUE_FIELD_NAME).userValueTypeHint(ValueType.NUMERIC).subAggregation(new MaxAggregationBuilder(MAX_AGG_NAME).field(VALUE_FIELD_NAME)).subAggregation(new BucketScriptPipelineAggregationBuilder("bucketscript", Collections.singletonMap("_value", MAX_AGG_NAME), new Script(ScriptType.INLINE, MockScriptEngine.NAME, INVERSE_SCRIPT, Collections.emptyMap()))));
            MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG);
            InternalNested nested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType);
            assertEquals(expectedNestedDocs, nested.getDocCount());
            assertEquals(NESTED_AGG, nested.getName());
            assertEquals(expectedNestedDocs, nested.getDocCount());
            InternalTerms<?, LongTerms.Bucket> terms = (InternalTerms) nested.getProperty("terms");
            assertNotNull(terms);
            for (LongTerms.Bucket bucket : terms.getBuckets()) {
                InternalMax max = (InternalMax) bucket.getAggregations().asMap().get(MAX_AGG_NAME);
                InternalSimpleValue bucketScript = (InternalSimpleValue) bucket.getAggregations().asMap().get("bucketscript");
                assertNotNull(max);
                assertNotNull(bucketScript);
                assertEquals(max.getValue(), -bucketScript.getValue(), Double.MIN_VALUE);
            }
            assertTrue(AggregationInspectionHelper.hasValue(nested));
        }
    }
}
Also used : BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Directory(org.apache.lucene.store.Directory) Script(org.opensearch.script.Script) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) InternalSimpleValue(org.opensearch.search.aggregations.pipeline.InternalSimpleValue) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) IndexReader(org.apache.lucene.index.IndexReader) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 3 with BucketScriptPipelineAggregationBuilder

use of org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder in project OpenSearch by opensearch-project.

the class TermsAggregatorTests method testOrderByPipelineAggregation.

public void testOrderByPipelineAggregation() throws Exception {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
                IndexSearcher indexSearcher = newIndexSearcher(indexReader);
                BucketScriptPipelineAggregationBuilder bucketScriptAgg = bucketScript("script", new Script("2.718"));
                TermsAggregationBuilder termsAgg = terms("terms").field("field").userValueTypeHint(ValueType.STRING).order(BucketOrder.aggregation("script", true)).subAggregation(bucketScriptAgg);
                MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("field");
                AggregationExecutionException e = expectThrows(AggregationExecutionException.class, () -> createAggregator(termsAgg, indexSearcher, fieldType));
                assertEquals("Invalid aggregation order path [script]. The provided aggregation [script] " + "either does not exist, or is a pipeline aggregation and cannot be used to sort the buckets.", e.getMessage());
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) PipelineAggregatorBuilders.bucketScript(org.opensearch.search.aggregations.PipelineAggregatorBuilders.bucketScript) Script(org.opensearch.script.Script) BucketScriptPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

Script (org.opensearch.script.Script)3 BucketScriptPipelineAggregationBuilder (org.opensearch.search.aggregations.pipeline.BucketScriptPipelineAggregationBuilder)3 IndexReader (org.apache.lucene.index.IndexReader)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 Directory (org.apache.lucene.store.Directory)2 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)2 ArrayList (java.util.ArrayList)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 XContentType (org.opensearch.common.xcontent.XContentType)1 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)1 QueryBuilder (org.opensearch.index.query.QueryBuilder)1 QueryRewriteContext (org.opensearch.index.query.QueryRewriteContext)1