Search in sources :

Example 6 with NodeQuery

use of com.enonic.xp.node.NodeQuery in project xp by enonic.

the class NumericRangeAggregationTest method keys.

@Test
public void keys() throws Exception {
    createNode(100d, "n1", NodePath.ROOT);
    createNode(200d, "n4", NodePath.ROOT);
    createNode(300d, "n2", NodePath.ROOT);
    createNode(400d, "n3", NodePath.ROOT);
    createNode(500d, "n6", NodePath.ROOT);
    createNode(600d, "n5", NodePath.ROOT);
    final NodeQuery query = NodeQuery.create().addAggregationQuery(NumericRangeAggregationQuery.create("myNumericRange").fieldName("numeric").addRange(NumericRange.create().key("small").to(350d).build()).addRange(NumericRange.create().key("large").from(350d).build()).build()).build();
    FindNodesByQueryResult result = doFindByQuery(query);
    assertEquals(1, result.getAggregations().getSize());
    final BucketAggregation aggregation = (BucketAggregation) result.getAggregations().get("myNumericRange");
    final Buckets buckets = aggregation.getBuckets();
    final Iterator<Bucket> iterator = buckets.iterator();
    verifyBucket(iterator.next(), 3, "small");
    verifyBucket(iterator.next(), 3, "large");
}
Also used : FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) NumericRangeBucket(com.enonic.xp.aggregation.NumericRangeBucket) 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 NodeQuery

use of com.enonic.xp.node.NodeQuery in project xp by enonic.

the class StatsAggregationTest method terms_stats_aggregation.

@Test
public void terms_stats_aggregation() throws Exception {
    createNode("c1", 2.0, "n1", NodePath.ROOT);
    createNode("c1", 4.0, "n2", NodePath.ROOT);
    createNode("c1", 6.0, "n3", NodePath.ROOT);
    createNode("c1", 8.0, "n4", NodePath.ROOT);
    createNode("c2", 2.0, "n5", NodePath.ROOT);
    createNode("c2", 4.0, "n6", NodePath.ROOT);
    createNode("c3", 2.0, "n7", NodePath.ROOT);
    final NodeQuery query = NodeQuery.create().addAggregationQuery(TermsAggregationQuery.create("category").fieldName("category").orderDirection(TermsAggregationQuery.Direction.ASC).orderType(TermsAggregationQuery.Type.TERM).addSubQuery(StatsAggregationQuery.create("subquery").fieldName("other").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();
    verifyStatsAggregation(bucketIterator.next(), "c1", 2d, 8d, 5d, 20d, 4d);
    verifyStatsAggregation(bucketIterator.next(), "c2", 2d, 4d, 3d, 6d, 2d);
    verifyStatsAggregation(bucketIterator.next(), "c3", 2d, 2d, 2d, 2d, 1d);
}
Also used : BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) StatsAggregation(com.enonic.xp.aggregation.StatsAggregation) Aggregation(com.enonic.xp.aggregation.Aggregation) 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 8 with NodeQuery

use of com.enonic.xp.node.NodeQuery in project xp by enonic.

the class TermsAggregationsTest method order_by_term.

@Test
public void order_by_term() throws Exception {
    create_c1_c2_c3_with_2_3_1_category_hits();
    final NodeQuery query = NodeQuery.create().addAggregationQuery(TermsAggregationQuery.create("category").fieldName("category").orderDirection(TermsAggregationQuery.Direction.ASC).orderType(TermsAggregationQuery.Type.TERM).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;
    assertEquals(3, categoryAgg.getBuckets().getSize());
    final Iterator<Bucket> iterator = categoryAgg.getBuckets().iterator();
    Bucket next = iterator.next();
    assertEquals("c1", next.getKey());
    assertEquals(2, next.getDocCount());
    next = iterator.next();
    assertEquals("c2", next.getKey());
    assertEquals(3, next.getDocCount());
    next = iterator.next();
    assertEquals("c3", next.getKey());
    assertEquals(1, next.getDocCount());
}
Also used : BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) Aggregation(com.enonic.xp.aggregation.Aggregation) 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 9 with NodeQuery

use of com.enonic.xp.node.NodeQuery in project xp by enonic.

the class NodesHasPermissionResolver method execute.

public boolean execute() {
    final Context context = ContextAccessor.current();
    if (context.getAuthInfo().hasRole(RoleKeys.ADMIN)) {
        return true;
    }
    if (nodeIds.isEmpty()) {
        return false;
    }
    final NodeQuery query = NodeQuery.create().addQueryFilter(IdFilter.create().fieldName(NodeIndexPath.ID.getPath()).values(nodeIds).build()).addQueryFilter(ValueFilter.create().fieldName(getPermissionFieldName().getPath()).addValues(context.getAuthInfo().getPrincipals().stream().map(PrincipalKey::toString).collect(Collectors.toList())).build()).searchMode(SearchMode.COUNT).build();
    final FindNodesByQueryResult result = FindNodesByQueryCommand.create(this).query(query).build().execute();
    return result.getTotalHits() == nodeIds.getSize();
}
Also used : Context(com.enonic.xp.context.Context) FindNodesByQueryResult(com.enonic.xp.node.FindNodesByQueryResult) NodeQuery(com.enonic.xp.node.NodeQuery) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Example 10 with NodeQuery

use of com.enonic.xp.node.NodeQuery in project xp by enonic.

the class PrincipalQueryNodeQueryTranslator method translate.

public static NodeQuery translate(final PrincipalQuery principalQuery) {
    final NodeQuery.Builder nodeQueryBuilder = NodeQuery.create().from(principalQuery.getFrom()).size(principalQuery.getSize());
    final IdProviderKeys idProviders = principalQuery.getIdProviders();
    if (idProviders.isNotEmpty()) {
        nodeQueryBuilder.addQueryFilter(ValueFilter.create().fieldName(ID_PROVIDER_KEY).addValues(idProviders.stream().map(IdProviderKey::toString).collect(toList())).build());
    }
    final Set<PrincipalType> types = principalQuery.getPrincipalTypes();
    if (!types.isEmpty()) {
        nodeQueryBuilder.addQueryFilter(ValueFilter.create().fieldName(PRINCIPAL_TYPE_KEY).addValues(types.stream().map(Object::toString).collect(toList())).build());
    }
    final String searchText = principalQuery.getSearchText();
    if (!nullToEmpty(searchText).isBlank()) {
        nodeQueryBuilder.query(getQueryExpression(searchText));
    }
    final String email = principalQuery.getEmail();
    if (!nullToEmpty(email).isBlank()) {
        nodeQueryBuilder.addQueryFilter(ValueFilter.create().fieldName(EMAIL_KEY).addValues(email).build());
    }
    final String name = principalQuery.getName();
    if (!nullToEmpty(name).isBlank()) {
        nodeQueryBuilder.addQueryFilter(ValueFilter.create().fieldName(NAME_KEY).addValues(name).build());
    }
    final String displayName = principalQuery.getDisplayName();
    if (!nullToEmpty(displayName).isBlank()) {
        nodeQueryBuilder.addQueryFilter(ValueFilter.create().fieldName(DISPLAY_NAME_KEY).addValues(displayName).build());
    }
    return nodeQueryBuilder.build();
}
Also used : NodeQuery(com.enonic.xp.node.NodeQuery) IdProviderKey(com.enonic.xp.security.IdProviderKey) IdProviderKeys(com.enonic.xp.security.IdProviderKeys) PrincipalType(com.enonic.xp.security.PrincipalType)

Aggregations

NodeQuery (com.enonic.xp.node.NodeQuery)73 FindNodesByQueryResult (com.enonic.xp.node.FindNodesByQueryResult)58 Test (org.junit.jupiter.api.Test)44 Node (com.enonic.xp.node.Node)24 BucketAggregation (com.enonic.xp.aggregation.BucketAggregation)18 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)18 Bucket (com.enonic.xp.aggregation.Bucket)14 PropertyTree (com.enonic.xp.data.PropertyTree)11 DynamicConstraintExpr (com.enonic.xp.query.expr.DynamicConstraintExpr)10 Aggregation (com.enonic.xp.aggregation.Aggregation)7 Buckets (com.enonic.xp.aggregation.Buckets)6 FindNodesByMultiRepoQueryResult (com.enonic.xp.node.FindNodesByMultiRepoQueryResult)6 MultiRepoNodeQuery (com.enonic.xp.node.MultiRepoNodeQuery)6 Nodes (com.enonic.xp.node.Nodes)5 SearchTargets (com.enonic.xp.node.SearchTargets)5 Repository (com.enonic.xp.repository.Repository)5 DateRangeBucket (com.enonic.xp.aggregation.DateRangeBucket)3 ContentId (com.enonic.xp.content.ContentId)3 PrincipalKey (com.enonic.xp.security.PrincipalKey)3 NumericRangeBucket (com.enonic.xp.aggregation.NumericRangeBucket)2