Search in sources :

Example 1 with Aggregation

use of com.enonic.xp.aggregation.Aggregation in project xp by enonic.

the class AggregationMapper method serializeAggregations.

private static void serializeAggregations(final MapGenerator gen, final Aggregations value) {
    for (Aggregation aggregation : value) {
        gen.map(aggregation.getName());
        if (aggregation instanceof BucketAggregation) {
            final Buckets buckets = ((BucketAggregation) aggregation).getBuckets();
            serializeBuckets(gen, buckets);
        } else if (aggregation instanceof StatsAggregation) {
            final StatsAggregation statsAggregation = ((StatsAggregation) aggregation);
            serializeStatsAggregation(gen, statsAggregation);
        }
        gen.end();
    }
}
Also used : BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Aggregation(com.enonic.xp.aggregation.Aggregation) StatsAggregation(com.enonic.xp.aggregation.StatsAggregation) StatsAggregation(com.enonic.xp.aggregation.StatsAggregation) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Buckets(com.enonic.xp.aggregation.Buckets)

Example 2 with Aggregation

use of com.enonic.xp.aggregation.Aggregation in project xp by enonic.

the class StatsAggregationTest method terms_stats_aggregation.

@Test
public void terms_stats_aggregation() throws Exception {
    createNode("c1", 2.0, "n1", NodePath.ROOT);
    createNode("c1", 4.0, "n2", NodePath.ROOT);
    createNode("c1", 6.0, "n3", NodePath.ROOT);
    createNode("c1", 8.0, "n4", NodePath.ROOT);
    createNode("c2", 2.0, "n5", NodePath.ROOT);
    createNode("c2", 4.0, "n6", NodePath.ROOT);
    createNode("c3", 2.0, "n7", NodePath.ROOT);
    final NodeQuery query = NodeQuery.create().addAggregationQuery(TermsAggregationQuery.create("category").fieldName("category").orderDirection(TermsAggregationQuery.Direction.ASC).orderType(TermsAggregationQuery.Type.TERM).addSubQuery(StatsAggregationQuery.create("subquery").fieldName("other").build()).build()).build();
    FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(1, result.getAggregations().getSize());
    final Aggregation agg = result.getAggregations().get("category");
    assertTrue(agg instanceof BucketAggregation);
    final BucketAggregation categoryAgg = (BucketAggregation) agg;
    final Iterator<Bucket> bucketIterator = categoryAgg.getBuckets().iterator();
    verifyStatsAggregation(bucketIterator.next(), "c1", 2d, 8d, 5d, 20d, 4d);
    verifyStatsAggregation(bucketIterator.next(), "c2", 2d, 4d, 3d, 6d, 2d);
    verifyStatsAggregation(bucketIterator.next(), "c3", 2d, 2d, 2d, 2d, 1d);
}
Also used : BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) StatsAggregation(com.enonic.xp.aggregation.StatsAggregation) Aggregation(com.enonic.xp.aggregation.Aggregation) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) Bucket(com.enonic.xp.aggregation.Bucket) NodeQuery(com.enonic.xp.node.NodeQuery) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 3 with Aggregation

use of com.enonic.xp.aggregation.Aggregation in project xp by enonic.

the class StatsAggregationTest method verifyStatsAggregation.

private void verifyStatsAggregation(final Bucket parentBucket, String parentBucketKey, double min, double max, double avg, double sum, double count) {
    assertEquals(parentBucketKey, parentBucket.getKey(), "Wrong parent bucket key");
    assertEquals(1, parentBucket.getSubAggregations().getSize());
    final Aggregation subAgg = parentBucket.getSubAggregations().get("subquery");
    assertTrue(subAgg instanceof StatsAggregation);
    final StatsAggregation stats = (StatsAggregation) subAgg;
    assertEquals(stats.getAvg(), avg, 0);
    assertEquals(stats.getMin(), min, 0);
    assertEquals(stats.getMax(), max, 0);
    assertEquals(stats.getSum(), sum, 0);
    assertEquals(stats.getCount(), count, 0);
}
Also used : BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) StatsAggregation(com.enonic.xp.aggregation.StatsAggregation) Aggregation(com.enonic.xp.aggregation.Aggregation) StatsAggregation(com.enonic.xp.aggregation.StatsAggregation)

Example 4 with Aggregation

use of com.enonic.xp.aggregation.Aggregation in project xp by enonic.

the class TermsAggregationsTest method order_by_term.

@Test
public void order_by_term() throws Exception {
    create_c1_c2_c3_with_2_3_1_category_hits();
    final NodeQuery query = NodeQuery.create().addAggregationQuery(TermsAggregationQuery.create("category").fieldName("category").orderDirection(TermsAggregationQuery.Direction.ASC).orderType(TermsAggregationQuery.Type.TERM).build()).build();
    FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(1, result.getAggregations().getSize());
    final Aggregation agg = result.getAggregations().get("category");
    assertTrue(agg instanceof BucketAggregation);
    final BucketAggregation categoryAgg = (BucketAggregation) agg;
    assertEquals(3, categoryAgg.getBuckets().getSize());
    final Iterator<Bucket> iterator = categoryAgg.getBuckets().iterator();
    Bucket next = iterator.next();
    assertEquals("c1", next.getKey());
    assertEquals(2, next.getDocCount());
    next = iterator.next();
    assertEquals("c2", next.getKey());
    assertEquals(3, next.getDocCount());
    next = iterator.next();
    assertEquals("c3", next.getKey());
    assertEquals(1, next.getDocCount());
}
Also used : BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Aggregation(com.enonic.xp.aggregation.Aggregation) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) Bucket(com.enonic.xp.aggregation.Bucket) NodeQuery(com.enonic.xp.node.NodeQuery) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 5 with Aggregation

use of com.enonic.xp.aggregation.Aggregation in project xp by enonic.

the class TermsAggregationsTest method size.

@Test
public void size() throws Exception {
    create_c1_c2_c3_with_2_3_1_category_hits();
    final NodeQuery query = NodeQuery.create().addAggregationQuery(TermsAggregationQuery.create("category").fieldName("category").orderDirection(TermsAggregationQuery.Direction.DESC).orderType(TermsAggregationQuery.Type.DOC_COUNT).size(2).build()).build();
    FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(1, result.getAggregations().getSize());
    final Aggregation agg = result.getAggregations().get("category");
    assertTrue(agg instanceof BucketAggregation);
    final BucketAggregation categoryAgg = (BucketAggregation) agg;
    assertEquals(2, categoryAgg.getBuckets().getSize());
    final Iterator<Bucket> iterator = categoryAgg.getBuckets().iterator();
    Bucket next = iterator.next();
    assertEquals("c2", next.getKey());
    assertEquals(3, next.getDocCount());
    next = iterator.next();
    assertEquals("c1", next.getKey());
    assertEquals(2, next.getDocCount());
}
Also used : BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Aggregation(com.enonic.xp.aggregation.Aggregation) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) Bucket(com.enonic.xp.aggregation.Bucket) NodeQuery(com.enonic.xp.node.NodeQuery) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Aggregations

Aggregation (com.enonic.xp.aggregation.Aggregation)12 BucketAggregation (com.enonic.xp.aggregation.BucketAggregation)12 Bucket (com.enonic.xp.aggregation.Bucket)8 FindNodesByQueryResult (com.enonic.xp.node.FindNodesByQueryResult)7 NodeQuery (com.enonic.xp.node.NodeQuery)7 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)7 Test (org.junit.jupiter.api.Test)7 StatsAggregation (com.enonic.xp.aggregation.StatsAggregation)4 SingleValueMetricAggregation (com.enonic.xp.aggregation.SingleValueMetricAggregation)3 Buckets (com.enonic.xp.aggregation.Buckets)2