Search in sources :

Example 21 with BucketAggregation

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

the class HistogramAggregationTest method order.

@Test
public void order() throws Exception {
    createNode(1d, "n1", NodePath.ROOT);
    createNode(2d, "n2", NodePath.ROOT);
    createNode(3d, "n3", NodePath.ROOT);
    createNode(10d, "n4", NodePath.ROOT);
    createNode(11d, "n5", NodePath.ROOT);
    createNode(20d, "n6", NodePath.ROOT);
    final NodeQuery query = NodeQuery.create().addAggregationQuery(HistogramAggregationQuery.create("count_asc").fieldName("value").interval(10L).order(HistogramAggregationQuery.Order.COUNT_ASC).build()).addAggregationQuery(HistogramAggregationQuery.create("key_asc").fieldName("value").interval(20L).order(HistogramAggregationQuery.Order.KEY_ASC).build()).build();
    FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(2, result.getAggregations().getSize());
    final BucketAggregation countAsc = (BucketAggregation) result.getAggregations().get("count_asc");
    final BucketAggregation keyAsc = (BucketAggregation) result.getAggregations().get("key_asc");
    final Iterator<Bucket> countAsIterator = countAsc.getBuckets().iterator();
    long previous = 0;
    while (countAsIterator.hasNext()) {
        final long nextCount = countAsIterator.next().getDocCount();
        assertTrue(previous < nextCount);
        previous = nextCount;
    }
    final Iterator<Bucket> keyAscIterator = keyAsc.getBuckets().iterator();
    String key = null;
    while (keyAscIterator.hasNext()) {
        final String next = keyAscIterator.next().getKey();
        assertTrue(key == null || key.compareTo(next) < 0);
        key = next;
    }
}
Also used : 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 22 with BucketAggregation

use of com.enonic.xp.aggregation.BucketAggregation 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 23 with BucketAggregation

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

the class FindNodesByQueryHandlerTest method testExample.

@Test
public void testExample() {
    final BucketAggregation duration = Aggregation.bucketAggregation("duration").buckets(Buckets.create().add(Bucket.create().key("1600").docCount(2).build()).add(Bucket.create().key("1400").docCount(1).build()).add(Bucket.create().key("1300").docCount(5).build()).build()).build();
    final Aggregations agg = Aggregations.create().add(Aggregation.bucketAggregation("urls").buckets(Buckets.create().add(Bucket.create().key("/site/draft/superhero/search").docCount(6762L).addAggregations(Aggregations.from(duration)).build()).add(Bucket.create().key("/site/draft/superhero").docCount(1245).addAggregations(Aggregations.from(duration)).build()).build()).build()).build();
    final Suggestions suggestions = Suggestions.create().add(TermSuggestion.create("termSuggestion").addSuggestionEntry(TermSuggestionEntry.create().text("text1").length(2).offset(1).addSuggestionOption(TermSuggestionOption.create().text("text1-1").score(1.0f).freq(2).build()).addSuggestionOption(TermSuggestionOption.create().text("text1-2").score(4.0f).freq(5).build()).build()).addSuggestionEntry(TermSuggestionEntry.create().text("text2").length(2).offset(2).addSuggestionOption(TermSuggestionOption.create().text("text2-1").score(2.3f).freq(4).build()).addSuggestionOption(TermSuggestionOption.create().text("text2-2").score(1.0f).freq(2).build()).build()).build()).build();
    Mockito.doReturn(FindNodesByQueryResult.create().totalHits(12902).addNodeHit(NodeHit.create().nodeId(NodeId.from("b186d24f-ac38-42ca-a6db-1c1bda6c6c26")).score(1.23f).highlight(HighlightedProperties.create().add(HighlightedProperty.create().name("property1").addFragment("fragment1").addFragment("fragment2").build()).build()).build()).addNodeHit(NodeHit.create().nodeId(NodeId.from("350ba4a6-589c-498b-8af0-f183850e1120")).score(1.40f).build()).aggregations(agg).suggestions(suggestions).build()).when(this.nodeService).findByQuery(Mockito.isA(NodeQuery.class));
    runScript("/lib/xp/examples/node/query.js");
}
Also used : Suggestions(com.enonic.xp.suggester.Suggestions) Aggregations(com.enonic.xp.aggregation.Aggregations) NodeQuery(com.enonic.xp.node.NodeQuery) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Test(org.junit.jupiter.api.Test)

Example 24 with BucketAggregation

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

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