use of com.enonic.xp.aggregation.Aggregation in project xp by enonic.
the class ValueCountAggregationTest method verifyValueCount.
private void verifyValueCount(final Bucket parentBucket, String parentBucketKey, double value) {
assertEquals(parentBucketKey, parentBucket.getKey(), "Wrong parent bucket key");
assertEquals(1, parentBucket.getSubAggregations().getSize());
final Aggregation subAgg = parentBucket.getSubAggregations().get("subquery");
assertTrue(subAgg instanceof SingleValueMetricAggregation);
final SingleValueMetricAggregation stats = (SingleValueMetricAggregation) subAgg;
assertEquals(value, stats.getValue(), 0);
}
use of com.enonic.xp.aggregation.Aggregation in project xp by enonic.
the class ValueCountAggregationTest method terms_stats_aggregation.
@Test
public void terms_stats_aggregation() throws Exception {
createNode("c1", "n1", NodePath.ROOT);
createNode("c1", "n2", NodePath.ROOT);
createNode("c1", "n3", NodePath.ROOT);
createNode("c1", "n4", NodePath.ROOT);
createNode("c2", "n5", NodePath.ROOT);
createNode("c2", "n6", NodePath.ROOT);
createNode("c3", "n7", NodePath.ROOT);
final NodeQuery query = NodeQuery.create().addAggregationQuery(TermsAggregationQuery.create("category").fieldName("category").orderDirection(TermsAggregationQuery.Direction.ASC).orderType(TermsAggregationQuery.Type.TERM).addSubQuery(ValueCountAggregationQuery.create("subquery").fieldName("category").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();
verifyValueCount(bucketIterator.next(), "c1", 4d);
verifyValueCount(bucketIterator.next(), "c2", 2d);
verifyValueCount(bucketIterator.next(), "c3", 1d);
}
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);
} else if (aggregation instanceof SingleValueMetricAggregation) {
gen.value("value", ((SingleValueMetricAggregation) aggregation).getValue());
}
gen.end();
}
}
use of com.enonic.xp.aggregation.Aggregation in project xp by enonic.
the class TermsAggregationsTest method nested_term_aggregation.
@Test
public void nested_term_aggregation() throws Exception {
createNode("c1", "d1", "node1", NodePath.ROOT);
createNode("c1", "d1", "node2", NodePath.ROOT);
createNode("c1", "d2", "node3", NodePath.ROOT);
createNode("c1", "d3", "node4", NodePath.ROOT);
createNode("c2", "d1", "node5", NodePath.ROOT);
createNode("c2", "d2", "node6", NodePath.ROOT);
createNode("c2", "d2", "node7", NodePath.ROOT);
createNode("c2", "d3", "node8", NodePath.ROOT);
createNode("c3", "d1", "node9", NodePath.ROOT);
createNode("c3", "d2", "node10", NodePath.ROOT);
createNode("c3", "d3", "node11", NodePath.ROOT);
createNode("c3", "d3", "node12", NodePath.ROOT);
final NodeQuery query = NodeQuery.create().addAggregationQuery(TermsAggregationQuery.create("category").fieldName("category").orderDirection(TermsAggregationQuery.Direction.ASC).orderType(TermsAggregationQuery.Type.TERM).addSubQuery(TermsAggregationQuery.create("subquery").fieldName("other").orderDirection(TermsAggregationQuery.Direction.ASC).orderType(TermsAggregationQuery.Type.TERM).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();
// Verify the number of values for each category, e.g in category
// c1, there are 2 'd1's, 1 'd2' and 1 'd3'
verifySubAggregation(bucketIterator.next(), "c1", 2, 1, 1);
verifySubAggregation(bucketIterator.next(), "c2", 1, 2, 1);
verifySubAggregation(bucketIterator.next(), "c3", 1, 1, 2);
}
use of com.enonic.xp.aggregation.Aggregation in project xp by enonic.
the class TermsAggregationsTest method verifySubAggregation.
private void verifySubAggregation(final Bucket parentBucket, String parentBucketKey, int first, int second, int third) {
assertEquals(parentBucketKey, parentBucket.getKey(), "Wrong parent bucket key");
assertEquals(1, parentBucket.getSubAggregations().getSize());
final Aggregation subAgg = parentBucket.getSubAggregations().get("subquery");
assertTrue(subAgg instanceof BucketAggregation);
final BucketAggregation subBucketAgg = (BucketAggregation) subAgg;
assertEquals(3, subBucketAgg.getBuckets().getSize());
final Iterator<Bucket> subQueryIterator = subBucketAgg.getBuckets().iterator();
Bucket nextSub = subQueryIterator.next();
assertEquals("d1", nextSub.getKey());
assertEquals(first, nextSub.getDocCount());
nextSub = subQueryIterator.next();
assertEquals("d2", nextSub.getKey());
assertEquals(second, nextSub.getDocCount());
nextSub = subQueryIterator.next();
assertEquals("d3", nextSub.getKey());
assertEquals(third, nextSub.getDocCount());
}
Aggregations