Search in sources :

Example 1 with SelectorRow

use of org.apache.jackrabbit.core.query.lucene.join.SelectorRow 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)

Aggregations

ArrayList (java.util.ArrayList)1 ItemNotFoundException (javax.jcr.ItemNotFoundException)1 NodeType (javax.jcr.nodetype.NodeType)1 Row (javax.jcr.query.Row)1 Constraint (javax.jcr.query.qom.Constraint)1 Predicate (org.apache.jackrabbit.commons.predicate.Predicate)1 RowPredicate (org.apache.jackrabbit.commons.predicate.RowPredicate)1 SelectorRow (org.apache.jackrabbit.core.query.lucene.join.SelectorRow)1 IndexReader (org.apache.lucene.index.IndexReader)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1