Search in sources :

Example 1 with ScoreNode

use of org.apache.jackrabbit.core.query.lucene.ScoreNode in project jackrabbit by apache.

the class QueryConstraint method initMatches.

/**
     * Initializes the matches for the constraint query. If the matches are
     * already initialized then this method returns immediately.
     *
     * @param context the evaluation context.
     * @throws IOException if an error occurs while reading from the index.
     */
private void initMatches(EvaluationContext context) throws IOException {
    if (matches == null) {
        Query selectorQuery;
        BooleanQuery and = new BooleanQuery();
        try {
            selectorQuery = factory.create(getSelector());
            and.add(selectorQuery, BooleanClause.Occur.MUST);
            and.add(constraint, BooleanClause.Occur.MUST);
        } catch (RepositoryException e) {
            throw Util.createIOException(e);
        }
        IndexReader reader = context.getIndexReader();
        QueryHits hits = context.evaluate(and);
        try {
            matches = new HashMap<Integer, Float>();
            ScoreNode sn;
            while ((sn = hits.nextScoreNode()) != null) {
                matches.put(sn.getDoc(reader), sn.getScore());
            }
        } finally {
            hits.close();
        }
    }
}
Also used : ScoreNode(org.apache.jackrabbit.core.query.lucene.ScoreNode) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryHits(org.apache.jackrabbit.core.query.lucene.QueryHits) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) IndexReader(org.apache.lucene.index.IndexReader) RepositoryException(javax.jcr.RepositoryException)

Example 2 with ScoreNode

use of org.apache.jackrabbit.core.query.lucene.ScoreNode in project jackrabbit by apache.

the class ChildNodeConstraint method evaluate.

/**
     * {@inheritDoc}
     */
public boolean evaluate(ScoreNode[] row, Name[] selectorNames, EvaluationContext context) throws IOException {
    ScoreNode sn = row[getSelectorIndex(selectorNames)];
    if (sn == null) {
        return false;
    }
    SessionImpl session = context.getSession();
    NodeImpl parent;
    try {
        parent = (NodeImpl) session.getNodeById(sn.getNodeId()).getParent();
    } catch (RepositoryException e) {
        return false;
    }
    return parent.getId().equals(getBaseNodeId(context));
}
Also used : ScoreNode(org.apache.jackrabbit.core.query.lucene.ScoreNode) ChildNodeImpl(org.apache.jackrabbit.spi.commons.query.qom.ChildNodeImpl) NodeImpl(org.apache.jackrabbit.core.NodeImpl) RepositoryException(javax.jcr.RepositoryException) SessionImpl(org.apache.jackrabbit.core.SessionImpl)

Example 3 with ScoreNode

use of org.apache.jackrabbit.core.query.lucene.ScoreNode in project jackrabbit by apache.

the class DescendantNodeConstraint method evaluate.

/**
     * {@inheritDoc}
     */
public boolean evaluate(ScoreNode[] row, Name[] selectorNames, EvaluationContext context) throws IOException {
    NodeId baseId = getBaseNodeId(context);
    if (baseId == null) {
        return false;
    }
    ScoreNode sn = row[getSelectorIndex(selectorNames)];
    if (sn == null) {
        return false;
    }
    NodeId id = sn.getNodeId();
    SessionImpl session = context.getSession();
    try {
        NodeImpl parent = session.getNodeById(id);
        for (; ; ) {
            // throws exception if there is no parent
            parent = (NodeImpl) parent.getParent();
            if (parent.getId().equals(baseId)) {
                return true;
            }
        }
    } catch (RepositoryException e) {
        return false;
    }
}
Also used : ScoreNode(org.apache.jackrabbit.core.query.lucene.ScoreNode) NodeImpl(org.apache.jackrabbit.core.NodeImpl) DescendantNodeImpl(org.apache.jackrabbit.spi.commons.query.qom.DescendantNodeImpl) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) SessionImpl(org.apache.jackrabbit.core.SessionImpl)

Aggregations

RepositoryException (javax.jcr.RepositoryException)3 ScoreNode (org.apache.jackrabbit.core.query.lucene.ScoreNode)3 NodeImpl (org.apache.jackrabbit.core.NodeImpl)2 SessionImpl (org.apache.jackrabbit.core.SessionImpl)2 NodeId (org.apache.jackrabbit.core.id.NodeId)1 QueryHits (org.apache.jackrabbit.core.query.lucene.QueryHits)1 ChildNodeImpl (org.apache.jackrabbit.spi.commons.query.qom.ChildNodeImpl)1 DescendantNodeImpl (org.apache.jackrabbit.spi.commons.query.qom.DescendantNodeImpl)1 IndexReader (org.apache.lucene.index.IndexReader)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 Query (org.apache.lucene.search.Query)1