Search in sources :

Example 6 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 7 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 8 with Buckets

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

the class DateRangeAggregationTest method ranges_with_date_math_date.

@Test
public void ranges_with_date_math_date() throws Exception {
    final Instant anchor = Instant.parse("2014-12-10T10:00:00Z");
    createNode(anchor, "n1", NodePath.ROOT);
    createNode(anchor.plusSeconds(-3600), "n2", NodePath.ROOT);
    createNode(anchor.plusSeconds(-3600 * 2), "n3", NodePath.ROOT);
    createNode(anchor.plusSeconds(-3600 * 3), "n4", NodePath.ROOT);
    createNode(anchor.plusSeconds(-3600 * 4), "n5", NodePath.ROOT);
    createNode(anchor.plusSeconds(-3600 * 5), "n6", NodePath.ROOT);
    final NodeQuery query = NodeQuery.create().addAggregationQuery(DateRangeAggregationQuery.create("myDateRange").fieldName("instant").addRange(DateRange.create().to("2014-12-10T10:00:00Z||-5h").build()).addRange(DateRange.create().from("2014-12-10T10:00:00.000Z||-5h").to("2014-12-10T10:00:00.000Z||-3h").build()).addRange(DateRange.create().from("2014-12-10T10:00:00Z||-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(), 0);
    verifyBucket(iterator.next(), 2);
    verifyBucket(iterator.next(), 4);
}
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 9 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);
        } else if (aggregation instanceof SingleValueMetricAggregation) {
            gen.value("value", ((SingleValueMetricAggregation) aggregation).getValue());
        }
        gen.end();
    }
}
Also used : BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Aggregation(com.enonic.xp.aggregation.Aggregation) SingleValueMetricAggregation(com.enonic.xp.aggregation.SingleValueMetricAggregation) StatsAggregation(com.enonic.xp.aggregation.StatsAggregation) SingleValueMetricAggregation(com.enonic.xp.aggregation.SingleValueMetricAggregation) StatsAggregation(com.enonic.xp.aggregation.StatsAggregation) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Buckets(com.enonic.xp.aggregation.Buckets)

Example 10 with Buckets

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

the class QueryContentHandlerTest method setupQuery.

private void setupQuery(final int count, final boolean aggs, final boolean addHighlight) {
    final Contents contents = TestDataFixtures.newContents(count);
    final Instant t1 = Instant.parse("2014-09-01T00:00:00.00Z");
    final Instant t2 = Instant.parse("2014-10-01T00:00:00.00Z");
    final Instant t3 = Instant.parse("2014-11-01T00:00:00.00Z");
    final Buckets buckets1 = Buckets.create().add(Bucket.create().key("male").docCount(10).build()).add(Bucket.create().key("female").docCount(12).build()).build();
    final Buckets buckets2 = Buckets.create().add(Bucket.create().key("2014-01").docCount(8).build()).add(Bucket.create().key("2014-02").docCount(10).build()).add(Bucket.create().key("2014-03").docCount(12).build()).build();
    final Buckets buckets3 = Buckets.create().add(NumericRangeBucket.create().key("a").docCount(2).to(50).build()).add(NumericRangeBucket.create().key("b").docCount(4).from(50).to(100).build()).add(NumericRangeBucket.create().key("c").docCount(4).from(100).build()).build();
    final Buckets buckets4 = Buckets.create().add(DateRangeBucket.create().from(t1).docCount(2).key("date range bucket key").build()).add(DateRangeBucket.create().to(t1).from(t2).docCount(5).build()).add(DateRangeBucket.create().to(t3).docCount(7).build()).build();
    final BucketAggregation aggr1 = BucketAggregation.bucketAggregation("genders").buckets(buckets1).build();
    final BucketAggregation aggr2 = BucketAggregation.bucketAggregation("by_month").buckets(buckets2).build();
    final BucketAggregation aggr3 = BucketAggregation.bucketAggregation("price_ranges").buckets(buckets3).build();
    final BucketAggregation aggr4 = BucketAggregation.bucketAggregation("my_date_range").buckets(buckets4).build();
    final StatsAggregation aggr5 = StatsAggregation.create("item_count").avg(3).max(5).min(1).sum(15).count(5).build();
    final Aggregations aggregations = Aggregations.from(aggr1, aggr2, aggr3, aggr4, aggr5);
    final Map<ContentId, HighlightedProperties> highlight = Map.of(ContentId.from("123"), HighlightedProperties.create().add(HighlightedProperty.create().name("property1").addFragment("fragment1_1").addFragment("fragment1_2").build()).build(), ContentId.from("456"), HighlightedProperties.create().add(HighlightedProperty.create().name("property2").addFragment("fragment2_1").addFragment("fragment2_2").build()).build());
    final FindContentIdsByQueryResult findResult = FindContentIdsByQueryResult.create().hits(contents.getSize()).totalHits(20).contents(contents.getIds()).aggregations(aggs ? aggregations : null).highlight(addHighlight ? highlight : null).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) Aggregations(com.enonic.xp.aggregation.Aggregations) HighlightedProperties(com.enonic.xp.highlight.HighlightedProperties) Instant(java.time.Instant) ContentId(com.enonic.xp.content.ContentId) StatsAggregation(com.enonic.xp.aggregation.StatsAggregation) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Buckets(com.enonic.xp.aggregation.Buckets)

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 SingleValueMetricAggregation (com.enonic.xp.aggregation.SingleValueMetricAggregation)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 Content (com.enonic.xp.content.Content)1