Search in sources :

Example 1 with ParsedAggregation

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

the class InternalAggregationTestCase method parseAndAssert.

@SuppressWarnings("unchecked")
protected <P extends ParsedAggregation> P parseAndAssert(final InternalAggregation aggregation, final boolean shuffled, final boolean addRandomFields) throws IOException {
    final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
    final XContentType xContentType = randomFrom(XContentType.values());
    final boolean humanReadable = randomBoolean();
    final BytesReference originalBytes;
    if (shuffled) {
        originalBytes = toShuffledXContent(aggregation, xContentType, params, humanReadable);
    } else {
        originalBytes = toXContent(aggregation, xContentType, params, humanReadable);
    }
    BytesReference mutated;
    if (addRandomFields) {
        /*
             * - we don't add to the root object because it should only contain
             * the named aggregation to test - we don't want to insert into the
             * "meta" object, because we pass on everything we find there
             *
             * - we don't want to directly insert anything random into "buckets"
             * objects, they are used with "keyed" aggregations and contain
             * named bucket objects. Any new named object on this level should
             * also be a bucket and be parsed as such.
             *
             * we also exclude top_hits that contain SearchHits, as all unknown fields
             * on a root level of SearchHit are interpreted as meta-fields and will be kept.
             */
        Predicate<String> basicExcludes = path -> path.isEmpty() || path.endsWith(Aggregation.CommonFields.META.getPreferredName()) || path.endsWith(Aggregation.CommonFields.BUCKETS.getPreferredName()) || path.contains("top_hits");
        Predicate<String> excludes = basicExcludes.or(excludePathsFromXContentInsertion());
        mutated = XContentTestUtils.insertRandomFields(xContentType, originalBytes, excludes, random());
    } else {
        mutated = originalBytes;
    }
    SetOnce<Aggregation> parsedAggregation = new SetOnce<>();
    try (XContentParser parser = createParser(xContentType.xContent(), mutated)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        XContentParserUtils.parseTypedKeysObject(parser, Aggregation.TYPED_KEYS_DELIMITER, Aggregation.class, parsedAggregation::set);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
        Aggregation agg = parsedAggregation.get();
        assertEquals(aggregation.getName(), agg.getName());
        assertEquals(aggregation.getMetadata(), agg.getMetadata());
        assertTrue(agg instanceof ParsedAggregation);
        assertEquals(aggregation.getType(), agg.getType());
        BytesReference parsedBytes = toXContent(agg, xContentType, params, humanReadable);
        OpenSearchAssertions.assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
        return (P) agg;
    }
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) BytesReference(org.opensearch.common.bytes.BytesReference) InternalSampler(org.opensearch.search.aggregations.bucket.sampler.InternalSampler) ParsedMax(org.opensearch.search.aggregations.metrics.ParsedMax) ParsedDateRange(org.opensearch.search.aggregations.bucket.range.ParsedDateRange) GeoDistanceAggregationBuilder(org.opensearch.search.aggregations.bucket.range.GeoDistanceAggregationBuilder) ParsedSignificantLongTerms(org.opensearch.search.aggregations.bucket.terms.ParsedSignificantLongTerms) ParsedBucketMetricValue(org.opensearch.search.aggregations.pipeline.ParsedBucketMetricValue) GeoBoundsAggregationBuilder(org.opensearch.search.aggregations.metrics.GeoBoundsAggregationBuilder) ParsedDateHistogram(org.opensearch.search.aggregations.bucket.histogram.ParsedDateHistogram) IpRangeAggregationBuilder(org.opensearch.search.aggregations.bucket.range.IpRangeAggregationBuilder) StringRareTerms(org.opensearch.search.aggregations.bucket.terms.StringRareTerms) ParsedGlobal(org.opensearch.search.aggregations.bucket.global.ParsedGlobal) MockBigArrays(org.opensearch.common.util.MockBigArrays) ParsedMin(org.opensearch.search.aggregations.metrics.ParsedMin) ParsedStringRareTerms(org.opensearch.search.aggregations.bucket.terms.ParsedStringRareTerms) Map(java.util.Map) ParsedSum(org.opensearch.search.aggregations.metrics.ParsedSum) InternalHDRPercentiles(org.opensearch.search.aggregations.metrics.InternalHDRPercentiles) FilterAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FilterAggregationBuilder) ScriptService(org.opensearch.script.ScriptService) LongRareTerms(org.opensearch.search.aggregations.bucket.terms.LongRareTerms) ParsedSimpleValue(org.opensearch.search.aggregations.pipeline.ParsedSimpleValue) ParsedHDRPercentiles(org.opensearch.search.aggregations.metrics.ParsedHDRPercentiles) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) VariableWidthHistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.VariableWidthHistogramAggregationBuilder) DerivativePipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.DerivativePipelineAggregationBuilder) Settings(org.opensearch.common.settings.Settings) InternalTDigestPercentileRanks(org.opensearch.search.aggregations.metrics.InternalTDigestPercentileRanks) ParsedValueCount(org.opensearch.search.aggregations.metrics.ParsedValueCount) CardinalityAggregationBuilder(org.opensearch.search.aggregations.metrics.CardinalityAggregationBuilder) GeoHashGridAggregationBuilder(org.opensearch.search.aggregations.bucket.geogrid.GeoHashGridAggregationBuilder) ParsedGeoTileGrid(org.opensearch.search.aggregations.bucket.geogrid.ParsedGeoTileGrid) SumBucketPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.SumBucketPipelineAggregationBuilder) ValueCountAggregationBuilder(org.opensearch.search.aggregations.metrics.ValueCountAggregationBuilder) XContentParserUtils(org.opensearch.common.xcontent.XContentParserUtils) XContentType(org.opensearch.common.xcontent.XContentType) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) ParsedNested(org.opensearch.search.aggregations.bucket.nested.ParsedNested) ParsedAdjacencyMatrix(org.opensearch.search.aggregations.bucket.adjacency.ParsedAdjacencyMatrix) ParsedDerivative(org.opensearch.search.aggregations.pipeline.ParsedDerivative) ReverseNestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.ReverseNestedAggregationBuilder) MissingAggregationBuilder(org.opensearch.search.aggregations.bucket.missing.MissingAggregationBuilder) InternalMultiBucketAggregation.countInnerBucket(org.opensearch.search.aggregations.InternalMultiBucketAggregation.countInnerBucket) ParsedTDigestPercentiles(org.opensearch.search.aggregations.metrics.ParsedTDigestPercentiles) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) ReduceContext(org.opensearch.search.aggregations.InternalAggregation.ReduceContext) DateHistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder) MaxBucketPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.MaxBucketPipelineAggregationBuilder) ParseField(org.opensearch.common.ParseField) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) RestSearchAction(org.opensearch.rest.action.search.RestSearchAction) XContentHelper.toXContent(org.opensearch.common.xcontent.XContentHelper.toXContent) ParsedAggregation(org.opensearch.search.aggregations.ParsedAggregation) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ParsedWeightedAvg(org.opensearch.search.aggregations.metrics.ParsedWeightedAvg) SetOnce(org.apache.lucene.util.SetOnce) ParsedReverseNested(org.opensearch.search.aggregations.bucket.nested.ParsedReverseNested) SearchPlugin(org.opensearch.plugins.SearchPlugin) IOException(java.io.IOException) ParsedExtendedStats(org.opensearch.search.aggregations.metrics.ParsedExtendedStats) Plugin(org.opensearch.plugins.Plugin) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) ParsedStats(org.opensearch.search.aggregations.metrics.ParsedStats) StatsAggregationBuilder(org.opensearch.search.aggregations.metrics.StatsAggregationBuilder) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ParsedScriptedMetric(org.opensearch.search.aggregations.metrics.ParsedScriptedMetric) ParsedStatsBucket(org.opensearch.search.aggregations.pipeline.ParsedStatsBucket) SumAggregationBuilder(org.opensearch.search.aggregations.metrics.SumAggregationBuilder) SearchModule(org.opensearch.search.SearchModule) Aggregation(org.opensearch.search.aggregations.Aggregation) RangeAggregationBuilder(org.opensearch.search.aggregations.bucket.range.RangeAggregationBuilder) ToXContent(org.opensearch.common.xcontent.ToXContent) ParsedStringTerms(org.opensearch.search.aggregations.bucket.terms.ParsedStringTerms) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) ParsedGeoCentroid(org.opensearch.search.aggregations.metrics.ParsedGeoCentroid) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) XContentParser(org.opensearch.common.xcontent.XContentParser) ExtendedStatsAggregationBuilder(org.opensearch.search.aggregations.metrics.ExtendedStatsAggregationBuilder) ScriptedMetricAggregationBuilder(org.opensearch.search.aggregations.metrics.ScriptedMetricAggregationBuilder) ParsedTDigestPercentileRanks(org.opensearch.search.aggregations.metrics.ParsedTDigestPercentileRanks) ParsedAvg(org.opensearch.search.aggregations.metrics.ParsedAvg) AvgBucketPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.AvgBucketPipelineAggregationBuilder) DateRangeAggregationBuilder(org.opensearch.search.aggregations.bucket.range.DateRangeAggregationBuilder) MedianAbsoluteDeviationAggregationBuilder(org.opensearch.search.aggregations.metrics.MedianAbsoluteDeviationAggregationBuilder) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) ParsedFilter(org.opensearch.search.aggregations.bucket.filter.ParsedFilter) InternalTDigestPercentiles(org.opensearch.search.aggregations.metrics.InternalTDigestPercentiles) ParsedFilters(org.opensearch.search.aggregations.bucket.filter.ParsedFilters) ParsedSampler(org.opensearch.search.aggregations.bucket.sampler.ParsedSampler) Predicate(java.util.function.Predicate) Collections.emptyList(java.util.Collections.emptyList) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) ParsedComposite(org.opensearch.search.aggregations.bucket.composite.ParsedComposite) ContextParser(org.opensearch.common.xcontent.ContextParser) ParsedVariableWidthHistogram(org.opensearch.search.aggregations.bucket.histogram.ParsedVariableWidthHistogram) ParsedMultiTerms(org.opensearch.search.aggregations.bucket.terms.ParsedMultiTerms) WeightedAvgAggregationBuilder(org.opensearch.search.aggregations.metrics.WeightedAvgAggregationBuilder) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) InternalHDRPercentileRanks(org.opensearch.search.aggregations.metrics.InternalHDRPercentileRanks) Collectors(java.util.stream.Collectors) OpenSearchAssertions(org.opensearch.test.hamcrest.OpenSearchAssertions) MultiBucketConsumer(org.opensearch.search.aggregations.MultiBucketConsumerService.MultiBucketConsumer) TopHitsAggregationBuilder(org.opensearch.search.aggregations.metrics.TopHitsAggregationBuilder) List(java.util.List) ParsedBinaryRange(org.opensearch.search.aggregations.bucket.range.ParsedBinaryRange) PipelineAggregator(org.opensearch.search.aggregations.pipeline.PipelineAggregator) Matchers.equalTo(org.hamcrest.Matchers.equalTo) PercentilesBucketPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.PercentilesBucketPipelineAggregationBuilder) BigArrays(org.opensearch.common.util.BigArrays) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) MultiBucketConsumerService(org.opensearch.search.aggregations.MultiBucketConsumerService) ParsedTopHits(org.opensearch.search.aggregations.metrics.ParsedTopHits) DoubleTerms(org.opensearch.search.aggregations.bucket.terms.DoubleTerms) ParsedRange(org.opensearch.search.aggregations.bucket.range.ParsedRange) InternalSimpleValue(org.opensearch.search.aggregations.pipeline.InternalSimpleValue) BytesReference(org.opensearch.common.bytes.BytesReference) ParsedPercentilesBucket(org.opensearch.search.aggregations.pipeline.ParsedPercentilesBucket) HistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder) ParsedCardinality(org.opensearch.search.aggregations.metrics.ParsedCardinality) DocValueFormat(org.opensearch.search.DocValueFormat) AvgAggregationBuilder(org.opensearch.search.aggregations.metrics.AvgAggregationBuilder) ParsedAutoDateHistogram(org.opensearch.search.aggregations.bucket.histogram.ParsedAutoDateHistogram) HashMap(java.util.HashMap) AutoDateHistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.AutoDateHistogramAggregationBuilder) ParsedHistogram(org.opensearch.search.aggregations.bucket.histogram.ParsedHistogram) ParsedGeoDistance(org.opensearch.search.aggregations.bucket.range.ParsedGeoDistance) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) CompositeAggregationBuilder(org.opensearch.search.aggregations.bucket.composite.CompositeAggregationBuilder) PipelineTree(org.opensearch.search.aggregations.pipeline.PipelineAggregator.PipelineTree) GeoCentroidAggregationBuilder(org.opensearch.search.aggregations.metrics.GeoCentroidAggregationBuilder) ParsedSignificantStringTerms(org.opensearch.search.aggregations.bucket.terms.ParsedSignificantStringTerms) ParsedExtendedStatsBucket(org.opensearch.search.aggregations.pipeline.ParsedExtendedStatsBucket) ParsedGeoBounds(org.opensearch.search.aggregations.metrics.ParsedGeoBounds) Collections.singletonMap(java.util.Collections.singletonMap) ParsedLongRareTerms(org.opensearch.search.aggregations.bucket.terms.ParsedLongRareTerms) ParsedMedianAbsoluteDeviation(org.opensearch.search.aggregations.metrics.ParsedMedianAbsoluteDeviation) SignificantStringTerms(org.opensearch.search.aggregations.bucket.terms.SignificantStringTerms) ParsedLongTerms(org.opensearch.search.aggregations.bucket.terms.ParsedLongTerms) ParsedDoubleTerms(org.opensearch.search.aggregations.bucket.terms.ParsedDoubleTerms) GeoTileGridAggregationBuilder(org.opensearch.search.aggregations.bucket.geogrid.GeoTileGridAggregationBuilder) Collections.emptyMap(java.util.Collections.emptyMap) AdjacencyMatrixAggregationBuilder(org.opensearch.search.aggregations.bucket.adjacency.AdjacencyMatrixAggregationBuilder) ParsedHDRPercentileRanks(org.opensearch.search.aggregations.metrics.ParsedHDRPercentileRanks) MultiTermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.MultiTermsAggregationBuilder) SignificantLongTerms(org.opensearch.search.aggregations.bucket.terms.SignificantLongTerms) ParsedGeoHashGrid(org.opensearch.search.aggregations.bucket.geogrid.ParsedGeoHashGrid) StatsBucketPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.StatsBucketPipelineAggregationBuilder) InternalBucketMetricValue(org.opensearch.search.aggregations.pipeline.InternalBucketMetricValue) NestedAggregationBuilder(org.opensearch.search.aggregations.bucket.nested.NestedAggregationBuilder) Comparator(java.util.Comparator) ParsedMissing(org.opensearch.search.aggregations.bucket.missing.ParsedMissing) Collections(java.util.Collections) ExtendedStatsBucketPipelineAggregationBuilder(org.opensearch.search.aggregations.pipeline.ExtendedStatsBucketPipelineAggregationBuilder) SetOnce(org.apache.lucene.util.SetOnce) ParsedAggregation(org.opensearch.search.aggregations.ParsedAggregation) ParsedAggregation(org.opensearch.search.aggregations.ParsedAggregation) Aggregation(org.opensearch.search.aggregations.Aggregation) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with ParsedAggregation

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

the class InternalAggregationTestCase method testFromXContent.

public final void testFromXContent() throws IOException {
    final T aggregation = createTestInstanceForXContent();
    final ParsedAggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), false);
    assertFromXContent(aggregation, parsedAggregation);
}
Also used : ParsedAggregation(org.opensearch.search.aggregations.ParsedAggregation)

Example 3 with ParsedAggregation

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

the class InternalAggregationTestCase method testFromXContentWithRandomFields.

public final void testFromXContentWithRandomFields() throws IOException {
    final T aggregation = createTestInstanceForXContent();
    final ParsedAggregation parsedAggregation = parseAndAssert(aggregation, randomBoolean(), true);
    assertFromXContent(aggregation, parsedAggregation);
}
Also used : ParsedAggregation(org.opensearch.search.aggregations.ParsedAggregation)

Aggregations

ParsedAggregation (org.opensearch.search.aggregations.ParsedAggregation)3 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Predicate (java.util.function.Predicate)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 SetOnce (org.apache.lucene.util.SetOnce)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 Matchers.lessThanOrEqualTo (org.hamcrest.Matchers.lessThanOrEqualTo)1 ParseField (org.opensearch.common.ParseField)1 CircuitBreaker (org.opensearch.common.breaker.CircuitBreaker)1