Search in sources :

Example 1 with Buckets

use of com.enonic.xp.aggregation.Buckets 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 Buckets

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

the class QueryContentHandlerTest method setUpForMetricsAggregations.

private void setUpForMetricsAggregations(final SingleValueMetricAggregation aggregation) {
    final List<Content> toAddContents = new ArrayList<>();
    for (int i = 1; i <= 5; i++) {
        final PropertyTree data = new PropertyTree();
        data.addString("category", "books");
        data.addString("productName", "product " + i);
        data.addDouble("price", 10.0 * i);
        final Content content = Content.create().id(ContentId.from("id" + i)).name("name" + i).displayName("My Content " + i).parentPath(ContentPath.from("/a/b")).modifier(PrincipalKey.from("user:system:admin")).modifiedTime(Instant.ofEpochSecond(0)).creator(PrincipalKey.from("user:system:admin")).createdTime(Instant.ofEpochSecond(0)).data(data).build();
        toAddContents.add(content);
    }
    final Buckets bucket = Buckets.create().add(Bucket.create().key("books").docCount(5).addAggregations(Aggregations.from(aggregation)).build()).build();
    final Contents contents = Contents.from(toAddContents);
    final FindContentIdsByQueryResult findResult = FindContentIdsByQueryResult.create().hits(contents.getSize()).totalHits(5).contents(contents.getIds()).aggregations(Aggregations.from(BucketAggregation.bucketAggregation("products").buckets(bucket).build())).build();
    Mockito.when(this.contentService.find(Mockito.isA(ContentQuery.class))).thenReturn(findResult);
    Mockito.when(this.contentService.getByIds(Mockito.isA(GetContentByIdsParams.class))).thenReturn(contents);
}
Also used : FindContentIdsByQueryResult(com.enonic.xp.content.FindContentIdsByQueryResult) Contents(com.enonic.xp.content.Contents) GetContentByIdsParams(com.enonic.xp.content.GetContentByIdsParams) ContentQuery(com.enonic.xp.content.ContentQuery) Content(com.enonic.xp.content.Content) PropertyTree(com.enonic.xp.data.PropertyTree) ArrayList(java.util.ArrayList) Buckets(com.enonic.xp.aggregation.Buckets)

Example 3 with Buckets

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

the class DateRangeAggregationTest method ranges.

@Test
public void ranges() throws Exception {
    createNode(Instant.parse("2014-12-10T10:00:00Z"), "n1", NodePath.ROOT);
    createNode(Instant.parse("2014-12-10T10:30:00Z"), "n2", NodePath.ROOT);
    createNode(Instant.parse("2014-12-10T11:30:00Z"), "n3", NodePath.ROOT);
    createNode(Instant.parse("2014-12-10T12:45:00Z"), "n4", NodePath.ROOT);
    createNode(Instant.parse("2014-12-10T13:59:59Z"), "n5", NodePath.ROOT);
    createNode(Instant.parse("2014-12-10T14:01:00Z"), "n6", NodePath.ROOT);
    final NodeQuery query = NodeQuery.create().addAggregationQuery(DateRangeAggregationQuery.create("myDateRange").fieldName("instant").addRange(DateRange.create().to(Instant.parse("2014-12-10T11:00:00Z")).build()).addRange(DateRange.create().from(Instant.parse("2014-12-10T11:00:00Z")).to(Instant.parse("2014-12-10T14:00:00Z")).build()).addRange(DateRange.create().from(Instant.parse("2014-12-10T14:00:00Z")).build()).build()).build();
    FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(1, result.getAggregations().getSize());
    final BucketAggregation aggregation = (BucketAggregation) result.getAggregations().get("myDateRange");
    final Buckets buckets = aggregation.getBuckets();
    final Iterator<Bucket> iterator = buckets.iterator();
    verifyBucket(iterator.next(), 2);
    verifyBucket(iterator.next(), 3);
    verifyBucket(iterator.next(), 1);
}
Also used : FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) DateRangeBucket(com.enonic.xp.aggregation.DateRangeBucket) Bucket(com.enonic.xp.aggregation.Bucket) NodeQuery(com.enonic.xp.node.NodeQuery) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Buckets(com.enonic.xp.aggregation.Buckets) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 4 with Buckets

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

the class DateRangeAggregationTest method ranges_with_date_math.

@Test
public void ranges_with_date_math() throws Exception {
    final Instant now = Instant.now();
    createNode(now, "n1", NodePath.ROOT);
    createNode(now.plusSeconds(-3600), "n2", NodePath.ROOT);
    createNode(now.plusSeconds(-3600 * 2), "n3", NodePath.ROOT);
    createNode(now.plusSeconds(-3600 * 3), "n4", NodePath.ROOT);
    createNode(now.plusSeconds(-3600 * 4), "n5", NodePath.ROOT);
    createNode(now.plusSeconds(-3600 * 5), "n6", NodePath.ROOT);
    final NodeQuery query = NodeQuery.create().addAggregationQuery(DateRangeAggregationQuery.create("myDateRange").fieldName("instant").addRange(DateRange.create().to("now-5h").build()).addRange(DateRange.create().from("now-5h").to("now-3h").build()).addRange(DateRange.create().from("now-3h").build()).build()).build();
    FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(1, result.getAggregations().getSize());
    final BucketAggregation aggregation = (BucketAggregation) result.getAggregations().get("myDateRange");
    final Buckets buckets = aggregation.getBuckets();
    final Iterator<Bucket> iterator = buckets.iterator();
    verifyBucket(iterator.next(), 1);
    verifyBucket(iterator.next(), 2);
    verifyBucket(iterator.next(), 3);
}
Also used : FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) DateRangeBucket(com.enonic.xp.aggregation.DateRangeBucket) Bucket(com.enonic.xp.aggregation.Bucket) Instant(java.time.Instant) NodeQuery(com.enonic.xp.node.NodeQuery) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Buckets(com.enonic.xp.aggregation.Buckets) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 5 with Buckets

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

the class GeoDistanceAggregationTest method ranges.

@Test
public void ranges() throws Exception {
    createNode(OSLO, "oslo", NodePath.ROOT);
    createNode(BERLIN, "berlin", NodePath.ROOT);
    createNode(NEW_YORK, "new-york", NodePath.ROOT);
    createNode(TRONDHEIM, "trondheim", NodePath.ROOT);
    createNode(MOSCOW, "moscow", NodePath.ROOT);
    createNode(FREDRIKSTAD, "fredrikstad", NodePath.ROOT);
    createNode(LONDON, "london", NodePath.ROOT);
    final NodeQuery query = NodeQuery.create().addAggregationQuery(GeoDistanceAggregationQuery.create("myGeoDistance").fieldName("geoPoint").origin(OSLO).unit("km").addRange(DistanceRange.create().to(200.0).build()).addRange(DistanceRange.create().from(200.0).to(800.0).build()).addRange(DistanceRange.create().from(800.0).to(1600.0).build()).addRange(DistanceRange.create().from(1600.0).to(2400.0).build()).addRange(DistanceRange.create().from(2400.0).build()).build()).build();
    FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(1, result.getAggregations().getSize());
    final BucketAggregation aggregation = (BucketAggregation) result.getAggregations().get("myGeoDistance");
    final Buckets buckets = aggregation.getBuckets();
    final Iterator<Bucket> iterator = buckets.iterator();
    // OSLO, FREDRIKSTAD
    verifyBucket(iterator.next(), 2, null);
    // TRONDHEIM
    verifyBucket(iterator.next(), 1, null);
    // BERLIN, LONDON
    verifyBucket(iterator.next(), 2, null);
    // MOSCOW
    verifyBucket(iterator.next(), 1, null);
    // NEW YORK
    verifyBucket(iterator.next(), 1, null);
}
Also used : FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) GeoDistanceRangeBucket(com.enonic.xp.aggregation.GeoDistanceRangeBucket) Bucket(com.enonic.xp.aggregation.Bucket) NodeQuery(com.enonic.xp.node.NodeQuery) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Buckets(com.enonic.xp.aggregation.Buckets) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Aggregations

Buckets (com.enonic.xp.aggregation.Buckets)10 BucketAggregation (com.enonic.xp.aggregation.BucketAggregation)9 Bucket (com.enonic.xp.aggregation.Bucket)6 FindNodesByQueryResult (com.enonic.xp.node.FindNodesByQueryResult)6 NodeQuery (com.enonic.xp.node.NodeQuery)6 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)6 Test (org.junit.jupiter.api.Test)6 DateRangeBucket (com.enonic.xp.aggregation.DateRangeBucket)3 StatsAggregation (com.enonic.xp.aggregation.StatsAggregation)3 Instant (java.time.Instant)3 Aggregation (com.enonic.xp.aggregation.Aggregation)2 NumericRangeBucket (com.enonic.xp.aggregation.NumericRangeBucket)2 ContentQuery (com.enonic.xp.content.ContentQuery)2 Contents (com.enonic.xp.content.Contents)2 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)2 GetContentByIdsParams (com.enonic.xp.content.GetContentByIdsParams)2 Aggregations (com.enonic.xp.aggregation.Aggregations)1 GeoDistanceRangeBucket (com.enonic.xp.aggregation.GeoDistanceRangeBucket)1 SingleValueMetricAggregation (com.enonic.xp.aggregation.SingleValueMetricAggregation)1 Content (com.enonic.xp.content.Content)1