Search in sources :

Example 1 with AggregationPath

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

the class InternalMultiBucketAggregationTests method testResolveToAgg.

public void testResolveToAgg() {
    AggregationPath path = AggregationPath.parse("the_avg");
    List<LongTerms.Bucket> buckets = new ArrayList<>();
    InternalAggregation agg = new InternalAvg("the_avg", 2, 1, DocValueFormat.RAW, Collections.emptyMap());
    InternalAggregations internalAggregations = InternalAggregations.from(Collections.singletonList(agg));
    LongTerms.Bucket bucket = new LongTerms.Bucket(1, 1, internalAggregations, false, 0, DocValueFormat.RAW);
    buckets.add(bucket);
    Object[] value = (Object[]) resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms");
    assertThat(value[0], equalTo(agg));
}
Also used : InternalAvg(org.opensearch.search.aggregations.metrics.InternalAvg) ArrayList(java.util.ArrayList) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) AggregationPath(org.opensearch.search.aggregations.support.AggregationPath)

Example 2 with AggregationPath

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

the class InternalMultiBucketAggregationTests method testResolveToCount.

public void testResolveToCount() {
    AggregationPath path = AggregationPath.parse("_count");
    List<LongTerms.Bucket> buckets = new ArrayList<>();
    InternalAggregation agg = new InternalAvg("the_avg", 2, 1, DocValueFormat.RAW, Collections.emptyMap());
    InternalAggregations internalAggregations = InternalAggregations.from(Collections.singletonList(agg));
    LongTerms.Bucket bucket = new LongTerms.Bucket(1, 1, internalAggregations, false, 0, DocValueFormat.RAW);
    buckets.add(bucket);
    Object[] value = (Object[]) resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms");
    assertThat(value[0], equalTo(1L));
}
Also used : InternalAvg(org.opensearch.search.aggregations.metrics.InternalAvg) ArrayList(java.util.ArrayList) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) AggregationPath(org.opensearch.search.aggregations.support.AggregationPath)

Example 3 with AggregationPath

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

the class InternalMultiBucketAggregationTests method testResolveToSpecificBucket.

public void testResolveToSpecificBucket() {
    AggregationPath path = AggregationPath.parse("string_terms['foo']>the_avg.value");
    List<LongTerms.Bucket> buckets = new ArrayList<>();
    InternalAggregation agg = new InternalAvg("the_avg", 2, 1, DocValueFormat.RAW, Collections.emptyMap());
    InternalAggregations internalStringAggs = InternalAggregations.from(Collections.singletonList(agg));
    List<StringTerms.Bucket> stringBuckets = Collections.singletonList(new StringTerms.Bucket(new BytesRef("foo".getBytes(StandardCharsets.UTF_8), 0, "foo".getBytes(StandardCharsets.UTF_8).length), 1, internalStringAggs, false, 0, DocValueFormat.RAW));
    InternalTerms termsAgg = new StringTerms("string_terms", BucketOrder.count(false), BucketOrder.count(false), 1, 0, Collections.emptyMap(), DocValueFormat.RAW, 1, false, 0, stringBuckets, 0);
    InternalAggregations internalAggregations = InternalAggregations.from(Collections.singletonList(termsAgg));
    LongTerms.Bucket bucket = new LongTerms.Bucket(19, 1, internalAggregations, false, 0, DocValueFormat.RAW);
    buckets.add(bucket);
    Object[] value = (Object[]) resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms");
    assertThat(value[0], equalTo(2.0));
}
Also used : ArrayList(java.util.ArrayList) AggregationPath(org.opensearch.search.aggregations.support.AggregationPath) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) InternalAvg(org.opensearch.search.aggregations.metrics.InternalAvg) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with AggregationPath

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

the class InternalMultiBucketAggregationTests method testResolveToMissingSpecificBucket.

public void testResolveToMissingSpecificBucket() {
    AggregationPath path = AggregationPath.parse("string_terms['bar']>the_avg.value");
    List<LongTerms.Bucket> buckets = new ArrayList<>();
    InternalAggregation agg = new InternalAvg("the_avg", 2, 1, DocValueFormat.RAW, Collections.emptyMap());
    InternalAggregations internalStringAggs = InternalAggregations.from(Collections.singletonList(agg));
    List<StringTerms.Bucket> stringBuckets = Collections.singletonList(new StringTerms.Bucket(new BytesRef("foo".getBytes(StandardCharsets.UTF_8), 0, "foo".getBytes(StandardCharsets.UTF_8).length), 1, internalStringAggs, false, 0, DocValueFormat.RAW));
    InternalTerms termsAgg = new StringTerms("string_terms", BucketOrder.count(false), BucketOrder.count(false), 1, 0, Collections.emptyMap(), DocValueFormat.RAW, 1, false, 0, stringBuckets, 0);
    InternalAggregations internalAggregations = InternalAggregations.from(Collections.singletonList(termsAgg));
    LongTerms.Bucket bucket = new LongTerms.Bucket(19, 1, internalAggregations, false, 0, DocValueFormat.RAW);
    buckets.add(bucket);
    InvalidAggregationPathException e = expectThrows(InvalidAggregationPathException.class, () -> resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms"));
    assertThat(e.getMessage(), equalTo("Cannot find an key ['bar'] in [string_terms]"));
}
Also used : ArrayList(java.util.ArrayList) AggregationPath(org.opensearch.search.aggregations.support.AggregationPath) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) InternalAvg(org.opensearch.search.aggregations.metrics.InternalAvg) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) BytesRef(org.apache.lucene.util.BytesRef)

Example 5 with AggregationPath

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

the class InternalMultiBucketAggregationTests method testResolveToNothing.

public void testResolveToNothing() {
    AggregationPath path = AggregationPath.parse("foo.value");
    List<LongTerms.Bucket> buckets = new ArrayList<>();
    InternalAggregation agg = new InternalAvg("the_avg", 2, 1, DocValueFormat.RAW, Collections.emptyMap());
    InternalAggregations internalAggregations = InternalAggregations.from(Collections.singletonList(agg));
    LongTerms.Bucket bucket = new LongTerms.Bucket(1, 1, internalAggregations, false, 0, DocValueFormat.RAW);
    buckets.add(bucket);
    InvalidAggregationPathException e = expectThrows(InvalidAggregationPathException.class, () -> resolvePropertyFromPath(path.getPathElementsAsStringList(), buckets, "the_long_terms"));
    assertThat(e.getMessage(), equalTo("Cannot find an aggregation named [foo] in [the_long_terms]"));
}
Also used : InternalAvg(org.opensearch.search.aggregations.metrics.InternalAvg) ArrayList(java.util.ArrayList) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) AggregationPath(org.opensearch.search.aggregations.support.AggregationPath)

Aggregations

ArrayList (java.util.ArrayList)9 LongTerms (org.opensearch.search.aggregations.bucket.terms.LongTerms)9 InternalAvg (org.opensearch.search.aggregations.metrics.InternalAvg)9 AggregationPath (org.opensearch.search.aggregations.support.AggregationPath)9 BytesRef (org.apache.lucene.util.BytesRef)2 InternalTerms (org.opensearch.search.aggregations.bucket.terms.InternalTerms)2 StringTerms (org.opensearch.search.aggregations.bucket.terms.StringTerms)2