Search in sources :

Example 16 with Row

use of javax.jcr.query.Row 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 17 with Row

use of javax.jcr.query.Row in project jackrabbit by apache.

the class LuceneQueryFactory method execute.

/**
     * @param columns
     * @param selector
     * @param constraint
     * @param externalSort
     *            if <code>true</code> it means that the lqf should just let the
     *            QueryEngine take care of sorting and applying applying offset
     *            and limit constraints
     * @param offsetIn
     *            used in pagination
     * @param limitIn
     *            used in pagination
     * @return a list of rows
     * @throws RepositoryException
     * @throws IOException
     */
public List<Row> execute(Map<String, PropertyValue> columns, Selector selector, Constraint constraint, Sort sort, boolean externalSort, long offsetIn, long limitIn) throws RepositoryException, IOException {
    final IndexReader reader = index.getIndexReader(true);
    final int offset = offsetIn < 0 ? 0 : (int) offsetIn;
    final int limit = limitIn < 0 ? Integer.MAX_VALUE : (int) limitIn;
    QueryHits hits = null;
    try {
        JackrabbitIndexSearcher searcher = new JackrabbitIndexSearcher(session, reader, index.getContext().getItemStateManager());
        searcher.setSimilarity(index.getSimilarity());
        Predicate filter = Predicate.TRUE;
        BooleanQuery query = new BooleanQuery();
        QueryPair qp = new QueryPair(query);
        query.add(create(selector), MUST);
        if (constraint != null) {
            String name = selector.getSelectorName();
            NodeType type = ntManager.getNodeType(selector.getNodeTypeName());
            filter = mapConstraintToQueryAndFilter(qp, constraint, Collections.singletonMap(name, type), searcher, reader);
        }
        List<Row> rows = new ArrayList<Row>();
        // TODO depending on the filters, we could push the offset info
        // into the searcher
        hits = searcher.evaluate(qp.mainQuery, sort, offset + limit);
        int currentNode = 0;
        int addedNodes = 0;
        ScoreNode node = hits.nextScoreNode();
        while (node != null) {
            Row row = null;
            try {
                row = new SelectorRow(columns, evaluator, selector.getSelectorName(), session.getNodeById(node.getNodeId()), node.getScore());
            } catch (ItemNotFoundException e) {
            // skip the node
            }
            if (row != null && filter.evaluate(row)) {
                if (externalSort) {
                    // return everything and not worry about sort
                    rows.add(row);
                } else {
                    // apply limit and offset rules locally
                    if (currentNode >= offset && currentNode - offset < limit) {
                        rows.add(row);
                        addedNodes++;
                    }
                    currentNode++;
                    // end the loop when going over the limit
                    if (addedNodes == limit) {
                        break;
                    }
                }
            }
            node = hits.nextScoreNode();
        }
        return rows;
    } finally {
        if (hits != null) {
            hits.close();
        }
        Util.closeOrRelease(reader);
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ArrayList(java.util.ArrayList) Constraint(javax.jcr.query.qom.Constraint) Predicate(org.apache.jackrabbit.commons.predicate.Predicate) RowPredicate(org.apache.jackrabbit.commons.predicate.RowPredicate) NodeType(javax.jcr.nodetype.NodeType) IndexReader(org.apache.lucene.index.IndexReader) SelectorRow(org.apache.jackrabbit.core.query.lucene.join.SelectorRow) SelectorRow(org.apache.jackrabbit.core.query.lucene.join.SelectorRow) Row(javax.jcr.query.Row) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 18 with Row

use of javax.jcr.query.Row in project jackrabbit by apache.

the class AbstractQueryLevel2Test method checkValue.

/**
     * Tests if all results contain only the searched value is contained in the
     * selected property
     *
     * @param itr           rows of the query result.
     * @param propertyName  selected property, that should contain the value.
     * @param expectedValue the value that is expected to be found
     */
protected void checkValue(RowIterator itr, String propertyName, String expectedValue) throws RepositoryException {
    while (itr.hasNext()) {
        Row row = itr.nextRow();
        // check fullText
        Value value = row.getValue(propertyName);
        if (value == null) {
            fail("Search Test: fails result does not contain value for selected property");
        }
        assertEquals("Value in query result row does not match expected value", expectedValue, value.getString());
    }
}
Also used : Value(javax.jcr.Value) Row(javax.jcr.query.Row)

Example 19 with Row

use of javax.jcr.query.Row in project jackrabbit by apache.

the class ThreeWayJoinTest method runTest.

public void runTest() throws Exception {
    int x = random.nextInt(NODE_COUNT);
    String query = "SELECT a.foo AS a, b.bar AS b, c.baz AS c" + " FROM [nt:unstructured] AS a" + " INNER JOIN [nt:unstructured] AS b ON a.foo = b.bar" + " INNER JOIN [nt:unstructured] AS c ON b.bar = c.baz" + " WHERE a.foo = " + x;
    QueryManager manager = session.getWorkspace().getQueryManager();
    RowIterator iterator = manager.createQuery(query, "JCR-SQL2").execute().getRows();
    int count = 0;
    while (iterator.hasNext()) {
        Row row = iterator.nextRow();
        long a = row.getValue("a").getLong();
        long b = row.getValue("b").getLong();
        long c = row.getValue("c").getLong();
        if (a != x || b != x || c != x) {
            throw new Exception("Invalid test result: " + x + " -> " + a + ", " + b + ", " + c);
        }
        count++;
    }
    if (count != NODE_COUNT * NODE_COUNT * NODE_COUNT) {
        throw new Exception("Invalid test result count: " + count);
    }
}
Also used : RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Row(javax.jcr.query.Row) RepositoryException(javax.jcr.RepositoryException)

Example 20 with Row

use of javax.jcr.query.Row in project jackrabbit by apache.

the class TwoWayJoinTest method runTest.

public void runTest() throws Exception {
    int x = random.nextInt(NODE_COUNT);
    String query = "SELECT a.foo AS a, b.bar AS b" + " FROM [nt:unstructured] AS a" + " INNER JOIN [nt:unstructured] AS b ON a.foo = b.bar" + " WHERE a.foo = " + x;
    QueryManager manager = session.getWorkspace().getQueryManager();
    RowIterator iterator = manager.createQuery(query, "JCR-SQL2").execute().getRows();
    int count = 0;
    while (iterator.hasNext()) {
        Row row = iterator.nextRow();
        long a = row.getValue("a").getLong();
        long b = row.getValue("b").getLong();
        if (a != x || a != b) {
            throw new Exception("Invalid test result: " + x + " -> " + a + ", " + b);
        }
        count++;
    }
    if (count != NODE_COUNT) {
        throw new Exception("Invalid test result count: " + count + " !=" + NODE_COUNT);
    }
}
Also used : RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Row(javax.jcr.query.Row) RepositoryException(javax.jcr.RepositoryException)

Aggregations

Row (javax.jcr.query.Row)52 RowIterator (javax.jcr.query.RowIterator)27 QueryResult (javax.jcr.query.QueryResult)21 QueryManager (javax.jcr.query.QueryManager)14 ArrayList (java.util.ArrayList)13 Node (javax.jcr.Node)13 Value (javax.jcr.Value)9 Query (javax.jcr.query.Query)9 Test (org.junit.Test)9 Constraint (javax.jcr.query.qom.Constraint)8 RepositoryException (javax.jcr.RepositoryException)7 Session (javax.jcr.Session)7 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)6 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)6 ValueFactory (javax.jcr.ValueFactory)5 HashSet (java.util.HashSet)4 TreeSet (java.util.TreeSet)4 HashMap (java.util.HashMap)3 NoSuchElementException (java.util.NoSuchElementException)3 NodeIterator (javax.jcr.NodeIterator)2