Search in sources :

Example 1 with DescendantNode

use of javax.jcr.query.qom.DescendantNode in project jackrabbit by apache.

the class LuceneQueryFactory method mapConstraintToQueryAndFilter.

protected Predicate mapConstraintToQueryAndFilter(QueryPair query, Constraint constraint, Map<String, NodeType> selectorMap, JackrabbitIndexSearcher searcher, IndexReader reader) throws RepositoryException, IOException {
    Predicate filter = Predicate.TRUE;
    if (constraint instanceof And) {
        And and = (And) constraint;
        filter = mapConstraintToQueryAndFilter(query, and.getConstraint1(), selectorMap, searcher, reader);
        Predicate other = mapConstraintToQueryAndFilter(query, and.getConstraint2(), selectorMap, searcher, reader);
        if (filter == Predicate.TRUE) {
            filter = other;
        } else if (other != Predicate.TRUE) {
            filter = Predicates.and(filter, other);
        }
    } else if (constraint instanceof Comparison) {
        Comparison c = (Comparison) constraint;
        Transform transform = new Transform(c.getOperand1());
        DynamicOperand left = transform.operand;
        final String operator = c.getOperator();
        StaticOperand right = c.getOperand2();
        if (left instanceof Length || left instanceof FullTextSearchScore || (((!JCR_OPERATOR_EQUAL_TO.equals(operator) && !JCR_OPERATOR_LIKE.equals(operator)) || transform.transform != TRANSFORM_NONE) && (left instanceof NodeName || left instanceof NodeLocalName))) {
            try {
                int type = PropertyType.UNDEFINED;
                if (left instanceof Length) {
                    type = PropertyType.LONG;
                } else if (left instanceof FullTextSearchScore) {
                    type = PropertyType.DOUBLE;
                }
                final DynamicOperand operand = c.getOperand1();
                final Value value = evaluator.getValue(right, type);
                filter = new RowPredicate() {

                    @Override
                    protected boolean evaluate(Row row) throws RepositoryException {
                        return new ValueComparator().evaluate(operator, evaluator.getValue(operand, row), value);
                    }
                };
            } catch (ValueFormatException e) {
                throw new InvalidQueryException(e);
            }
        } else {
            Query cq = getComparisonQuery(left, transform.transform, operator, right, selectorMap);
            query.subQuery.add(cq, MUST);
        }
    } else if (constraint instanceof DescendantNode) {
        final DescendantNode descendantNode = (DescendantNode) constraint;
        Query context = getNodeIdQuery(UUID, descendantNode.getAncestorPath());
        query.mainQuery = new DescendantSelfAxisQuery(context, query.subQuery, false);
    } else {
        query.subQuery.add(create(constraint, selectorMap, searcher), MUST);
    }
    return filter;
}
Also used : Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) DynamicOperand(javax.jcr.query.qom.DynamicOperand) StaticOperand(javax.jcr.query.qom.StaticOperand) NodeName(javax.jcr.query.qom.NodeName) ValueComparator(org.apache.jackrabbit.core.query.lucene.join.ValueComparator) Predicate(org.apache.jackrabbit.commons.predicate.Predicate) RowPredicate(org.apache.jackrabbit.commons.predicate.RowPredicate) NodeLocalName(javax.jcr.query.qom.NodeLocalName) RowPredicate(org.apache.jackrabbit.commons.predicate.RowPredicate) Comparison(javax.jcr.query.qom.Comparison) Length(javax.jcr.query.qom.Length) And(javax.jcr.query.qom.And) DescendantNode(javax.jcr.query.qom.DescendantNode) Value(javax.jcr.Value) PropertyValue(javax.jcr.query.qom.PropertyValue) ValueFormatException(javax.jcr.ValueFormatException) FullTextSearchScore(javax.jcr.query.qom.FullTextSearchScore) SelectorRow(org.apache.jackrabbit.core.query.lucene.join.SelectorRow) Row(javax.jcr.query.Row) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 2 with DescendantNode

use of javax.jcr.query.qom.DescendantNode in project jackrabbit by apache.

the class LuceneQueryFactory method getDescendantNodeQuery.

protected Query getDescendantNodeQuery(DescendantNode dn, JackrabbitIndexSearcher searcher) throws RepositoryException, IOException {
    BooleanQuery query = new BooleanQuery();
    int clauses = 0;
    try {
        LinkedList<String> ids = new LinkedList<String>();
        Node ancestor = session.getNode(dn.getAncestorPath());
        ids.add(ancestor.getIdentifier());
        while (!ids.isEmpty()) {
            String id = ids.removeFirst();
            Query q = new JackrabbitTermQuery(new Term(FieldNames.PARENT, id));
            QueryHits hits = searcher.evaluate(q);
            ScoreNode sn = hits.nextScoreNode();
            if (sn != null) {
                // reset query so it does not overflow because of the max
                // clause count condition,
                // see JCR-3108
                clauses++;
                if (clauses == BooleanQuery.getMaxClauseCount()) {
                    BooleanQuery wrapQ = new BooleanQuery();
                    wrapQ.add(query, SHOULD);
                    query = wrapQ;
                    clauses = 1;
                }
                query.add(q, SHOULD);
                do {
                    ids.add(sn.getNodeId().toString());
                    sn = hits.nextScoreNode();
                } while (sn != null);
            }
        }
    } catch (PathNotFoundException e) {
        query.add(new JackrabbitTermQuery(new Term(FieldNames.UUID, // never matches
        "invalid-node-id")), SHOULD);
    }
    return query;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) ChildNode(javax.jcr.query.qom.ChildNode) DescendantNode(javax.jcr.query.qom.DescendantNode) SameNode(javax.jcr.query.qom.SameNode) Node(javax.jcr.Node) Term(org.apache.lucene.index.Term) PathNotFoundException(javax.jcr.PathNotFoundException) Constraint(javax.jcr.query.qom.Constraint) LinkedList(java.util.LinkedList)

Example 3 with DescendantNode

use of javax.jcr.query.qom.DescendantNode in project jackrabbit by apache.

the class QueryObjectModelFactoryTest method testDescendantNode.

/**
 * Test case for {@link QueryObjectModelFactory#descendantNode(String, String)}
 */
public void testDescendantNode() throws RepositoryException {
    DescendantNode descNode = qf.descendantNode(SELECTOR_NAME1, testRootNode.getPath());
    assertEquals("Wrong selector", SELECTOR_NAME1, descNode.getSelectorName());
    assertEquals("Wrong path", testRootNode.getPath(), descNode.getAncestorPath());
}
Also used : DescendantNode(javax.jcr.query.qom.DescendantNode)

Example 4 with DescendantNode

use of javax.jcr.query.qom.DescendantNode in project jackrabbit by apache.

the class QueryObjectModelFactoryTest method testDescendantNodeWithSelector.

/**
 * Test case for {@link QueryObjectModelFactory#descendantNode(String, String)}
 */
public void testDescendantNodeWithSelector() throws RepositoryException {
    DescendantNode descNode = qf.descendantNode(SELECTOR_NAME1, testRootNode.getPath());
    assertEquals("Wrong selector name", SELECTOR_NAME1, descNode.getSelectorName());
    assertEquals("Wrong path", testRootNode.getPath(), descNode.getAncestorPath());
}
Also used : DescendantNode(javax.jcr.query.qom.DescendantNode)

Example 5 with DescendantNode

use of javax.jcr.query.qom.DescendantNode in project jackrabbit-oak by apache.

the class QomTest method descendantNode.

@Test
public void descendantNode() throws RepositoryException {
    DescendantNode d = f.descendantNode("selectorName", "path");
    assertEquals("selectorName", d.getSelectorName());
    assertEquals("path", d.getAncestorPath());
    assertEquals("ISDESCENDANTNODE([selectorName], [path])", d.toString());
    assertEquals("ISDESCENDANTNODE([p])", f.descendantNode(null, "p").toString());
}
Also used : DescendantNode(javax.jcr.query.qom.DescendantNode) Test(org.junit.Test)

Aggregations

DescendantNode (javax.jcr.query.qom.DescendantNode)6 And (javax.jcr.query.qom.And)2 ChildNode (javax.jcr.query.qom.ChildNode)2 Comparison (javax.jcr.query.qom.Comparison)2 SameNode (javax.jcr.query.qom.SameNode)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 Query (org.apache.lucene.search.Query)2 LinkedList (java.util.LinkedList)1 Node (javax.jcr.Node)1 PathNotFoundException (javax.jcr.PathNotFoundException)1 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)1 Value (javax.jcr.Value)1 ValueFormatException (javax.jcr.ValueFormatException)1 InvalidQueryException (javax.jcr.query.InvalidQueryException)1 Row (javax.jcr.query.Row)1 Constraint (javax.jcr.query.qom.Constraint)1 DynamicOperand (javax.jcr.query.qom.DynamicOperand)1 FullTextSearch (javax.jcr.query.qom.FullTextSearch)1 FullTextSearchScore (javax.jcr.query.qom.FullTextSearchScore)1 Length (javax.jcr.query.qom.Length)1