Search in sources :

Example 1 with AggregationQueries

use of com.enonic.xp.query.aggregation.AggregationQueries in project xp by enonic.

the class QueryAggregationParams method getAggregations.

public AggregationQueries getAggregations(final Map<String, Object> aggregationsMap) {
    if (aggregationsMap == null) {
        return AggregationQueries.empty();
    }
    final AggregationQueries.Builder aggregations = AggregationQueries.create();
    aggregationsMap.forEach((name, aggregationQueryMap) -> {
        final AggregationQuery aggregationQuery = aggregationQueryFromParams(name, (Map<String, Object>) aggregationQueryMap);
        if (aggregationQuery != null) {
            aggregations.add(aggregationQuery);
        }
    });
    return aggregations.build();
}
Also used : BucketAggregationQuery(com.enonic.xp.query.aggregation.BucketAggregationQuery) MinAggregationQuery(com.enonic.xp.query.aggregation.metric.MinAggregationQuery) HistogramAggregationQuery(com.enonic.xp.query.aggregation.HistogramAggregationQuery) NumericRangeAggregationQuery(com.enonic.xp.query.aggregation.NumericRangeAggregationQuery) TermsAggregationQuery(com.enonic.xp.query.aggregation.TermsAggregationQuery) ValueCountAggregationQuery(com.enonic.xp.query.aggregation.metric.ValueCountAggregationQuery) DateHistogramAggregationQuery(com.enonic.xp.query.aggregation.DateHistogramAggregationQuery) MaxAggregationQuery(com.enonic.xp.query.aggregation.metric.MaxAggregationQuery) DateRangeAggregationQuery(com.enonic.xp.query.aggregation.DateRangeAggregationQuery) AggregationQuery(com.enonic.xp.query.aggregation.AggregationQuery) GeoDistanceAggregationQuery(com.enonic.xp.query.aggregation.GeoDistanceAggregationQuery) StatsAggregationQuery(com.enonic.xp.query.aggregation.metric.StatsAggregationQuery) AggregationQueries(com.enonic.xp.query.aggregation.AggregationQueries)

Example 2 with AggregationQueries

use of com.enonic.xp.query.aggregation.AggregationQueries in project xp by enonic.

the class QueryAggregationParamsTest method testTermsAggregation.

@Test
public void testTermsAggregation() {
    final Map<String, Object> termsAggregation = new HashMap<>();
    termsAggregation.put("field", "fieldName");
    termsAggregation.put("order", "_count desc");
    termsAggregation.put("size", 10);
    termsAggregation.put("minDocCount", 5);
    final Map<String, Object> aggregations = new HashMap<>();
    aggregations.put("aggName", Collections.singletonMap("terms", termsAggregation));
    final AggregationQueries result = new QueryAggregationParams().getAggregations(aggregations);
    assertEquals(1, result.getSize());
    assertTrue(result.first() instanceof TermsAggregationQuery);
    final TermsAggregationQuery query = (TermsAggregationQuery) result.first();
    assertNotNull(query);
    assertEquals("fieldName", query.getFieldName());
    assertEquals(5, query.getMinDocCount());
}
Also used : TermsAggregationQuery(com.enonic.xp.query.aggregation.TermsAggregationQuery) HashMap(java.util.HashMap) AggregationQueries(com.enonic.xp.query.aggregation.AggregationQueries) Test(org.junit.jupiter.api.Test)

Example 3 with AggregationQueries

use of com.enonic.xp.query.aggregation.AggregationQueries in project xp by enonic.

the class QueryAggregationParamsTest method testMetricAggregations.

@Test
public void testMetricAggregations() {
    final Map<String, Object> aggregations = new HashMap<>();
    aggregations.put("maxPrice", createMetricAggregationAsMap("max", "fieldName1"));
    aggregations.put("minPrice", createMetricAggregationAsMap("min", "fieldName2"));
    aggregations.put("countNodesWithNonEmptyPrice", createMetricAggregationAsMap("count", "fieldName3"));
    final AggregationQueries result = new QueryAggregationParams().getAggregations(aggregations);
    assertEquals(3, result.getSize());
    result.forEach(aggregationQuery -> {
        if (aggregationQuery instanceof MaxAggregationQuery) {
            assertEquals("maxPrice", aggregationQuery.getName());
            assertEquals("fieldName1", ((MaxAggregationQuery) aggregationQuery).getFieldName());
        } else if (aggregationQuery instanceof MinAggregationQuery) {
            assertEquals("minPrice", aggregationQuery.getName());
            assertEquals("fieldName2", ((MinAggregationQuery) aggregationQuery).getFieldName());
        } else {
            assertEquals("countNodesWithNonEmptyPrice", aggregationQuery.getName());
            assertEquals("fieldName3", ((ValueCountAggregationQuery) aggregationQuery).getFieldName());
        }
    });
}
Also used : MinAggregationQuery(com.enonic.xp.query.aggregation.metric.MinAggregationQuery) ValueCountAggregationQuery(com.enonic.xp.query.aggregation.metric.ValueCountAggregationQuery) HashMap(java.util.HashMap) AggregationQueries(com.enonic.xp.query.aggregation.AggregationQueries) MaxAggregationQuery(com.enonic.xp.query.aggregation.metric.MaxAggregationQuery) Test(org.junit.jupiter.api.Test)

Example 4 with AggregationQueries

use of com.enonic.xp.query.aggregation.AggregationQueries in project xp by enonic.

the class QueryAggregationParamsTest method testTermsAggregationWithoutMinDocCount.

@Test
public void testTermsAggregationWithoutMinDocCount() {
    final Map<String, Object> termsAggregation = new HashMap<>();
    termsAggregation.put("field", "fieldName");
    termsAggregation.put("order", "_count desc");
    termsAggregation.put("size", 10);
    final Map<String, Object> aggregations = new HashMap<>();
    aggregations.put("aggName", Collections.singletonMap("terms", termsAggregation));
    final AggregationQueries result = new QueryAggregationParams().getAggregations(aggregations);
    assertEquals(1, result.getSize());
    assertTrue(result.first() instanceof TermsAggregationQuery);
    final TermsAggregationQuery query = (TermsAggregationQuery) result.first();
    assertNotNull(query);
    assertEquals("fieldName", query.getFieldName());
    assertEquals(1, query.getMinDocCount());
}
Also used : TermsAggregationQuery(com.enonic.xp.query.aggregation.TermsAggregationQuery) HashMap(java.util.HashMap) AggregationQueries(com.enonic.xp.query.aggregation.AggregationQueries) Test(org.junit.jupiter.api.Test)

Example 5 with AggregationQueries

use of com.enonic.xp.query.aggregation.AggregationQueries in project xp by enonic.

the class AbstractFindNodesQueryHandler method createNodeQuery.

NodeQuery createNodeQuery() {
    final int start = valueOrDefault(this.start, 0);
    final int count = valueOrDefault(this.count, 10);
    final QueryExpr queryExpr = QueryExpr.from(buildConstraintExpr(), buildOrderExpr());
    final Filters filters = JsonToFilterMapper.create(this.filters);
    final AggregationQueries aggregations = new QueryAggregationParams().getAggregations(this.aggregations);
    final SuggestionQueries suggestions = new QuerySuggestionParams().getSuggestions(this.suggestions);
    final HighlightQuery highlight = new QueryHighlightParams().getHighlightQuery(this.highlight);
    return NodeQuery.create().from(start).size(count).addAggregationQueries(aggregations).addSuggestionQueries(suggestions).highlight(highlight).query(queryExpr).addQueryFilters(filters).explain(this.explain).build();
}
Also used : SuggestionQueries(com.enonic.xp.query.suggester.SuggestionQueries) Filters(com.enonic.xp.query.filter.Filters) QueryExpr(com.enonic.xp.query.expr.QueryExpr) AggregationQueries(com.enonic.xp.query.aggregation.AggregationQueries) HighlightQuery(com.enonic.xp.query.highlight.HighlightQuery)

Aggregations

AggregationQueries (com.enonic.xp.query.aggregation.AggregationQueries)6 TermsAggregationQuery (com.enonic.xp.query.aggregation.TermsAggregationQuery)3 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 MaxAggregationQuery (com.enonic.xp.query.aggregation.metric.MaxAggregationQuery)2 MinAggregationQuery (com.enonic.xp.query.aggregation.metric.MinAggregationQuery)2 ValueCountAggregationQuery (com.enonic.xp.query.aggregation.metric.ValueCountAggregationQuery)2 AggregationQuery (com.enonic.xp.query.aggregation.AggregationQuery)1 BucketAggregationQuery (com.enonic.xp.query.aggregation.BucketAggregationQuery)1 DateHistogramAggregationQuery (com.enonic.xp.query.aggregation.DateHistogramAggregationQuery)1 DateRangeAggregationQuery (com.enonic.xp.query.aggregation.DateRangeAggregationQuery)1 GeoDistanceAggregationQuery (com.enonic.xp.query.aggregation.GeoDistanceAggregationQuery)1 HistogramAggregationQuery (com.enonic.xp.query.aggregation.HistogramAggregationQuery)1 NumericRangeAggregationQuery (com.enonic.xp.query.aggregation.NumericRangeAggregationQuery)1 StatsAggregationQuery (com.enonic.xp.query.aggregation.metric.StatsAggregationQuery)1 QueryExpr (com.enonic.xp.query.expr.QueryExpr)1 Filters (com.enonic.xp.query.filter.Filters)1 HighlightQuery (com.enonic.xp.query.highlight.HighlightQuery)1 SuggestionQueries (com.enonic.xp.query.suggester.SuggestionQueries)1 Collections.emptyMap (java.util.Collections.emptyMap)1