Search in sources :

Example 16 with RowIterator

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

the class XPathQueryLevel2Test method testPathColumn.

/**
     * Test if the jcr:path pseudo property is contained in result.
     * <p>
     * For configuration description see {@link #setUpFullTextTest()}.
     */
public void testPathColumn() throws Exception {
    setUpFullTextTest();
    QueryResult result = execute(getFullTextStatement());
    RowIterator rows = result.getRows();
    if (getSize(rows) < 1) {
        fail("Query result did not return any nodes");
    }
    // re-aquire rows
    rows = result.getRows();
    // test mere existence
    rows.nextRow().getValue(jcrPath);
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator)

Example 17 with RowIterator

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

the class FullTextSearchScoreTest method testOrdering.

public void testOrdering() throws RepositoryException {
    QueryObjectModel qom = qf.createQuery(qf.selector(testNodeType, "s"), qf.and(qf.fullTextSearch("s", null, qf.literal(vf.createValue("fox"))), qf.descendantNode("s", testRootNode.getPath())), new Ordering[] { qf.ascending(qf.fullTextSearchScore("s")) }, null);
    forQOMandSQL2(qom, new Callable() {

        public Object call(Query query) throws RepositoryException {
            RowIterator rows = query.execute().getRows();
            double previousScore = Double.NaN;
            while (rows.hasNext()) {
                double score = rows.nextRow().getScore("s");
                if (!Double.isNaN(previousScore)) {
                    assertTrue("wrong order", previousScore <= score);
                }
                previousScore = score;
            }
            return null;
        }
    });
}
Also used : Query(javax.jcr.query.Query) RowIterator(javax.jcr.query.RowIterator) QueryObjectModel(javax.jcr.query.qom.QueryObjectModel) RepositoryException(javax.jcr.RepositoryException)

Example 18 with RowIterator

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

the class QueryEngine method execute.

protected QueryResult execute(Column[] columns, Selector selector, Constraint constraint, Ordering[] orderings, long offset, long limit, int printIndentation) throws RepositoryException {
    long time = System.currentTimeMillis();
    Map<String, NodeType> selectorMap = getSelectorNames(selector);
    String[] selectorNames = selectorMap.keySet().toArray(new String[selectorMap.size()]);
    Map<String, PropertyValue> columnMap = getColumnMap(columns, selectorMap);
    String[] columnNames = columnMap.keySet().toArray(new String[columnMap.size()]);
    Sort sort = new Sort();
    if (NATIVE_SORT) {
        sort = new Sort(createSortFields(orderings, session));
    }
    // if true it means that the LuceneQueryFactory should just let the
    // QueryEngine take care of sorting and applying offset and limit
    // constraints
    boolean externalSort = !NATIVE_SORT;
    RowIterator rows = null;
    try {
        rows = new RowIteratorAdapter(lqf.execute(columnMap, selector, constraint, sort, externalSort, offset, limit));
    } catch (IOException e) {
        throw new RepositoryException("Failed to access the query index", e);
    } finally {
        log.debug("{}SQL2 SELECT took {} ms. selector: {}, columns: {}, constraint: {}, offset {}, limit {}", new Object[] { genString(printIndentation), System.currentTimeMillis() - time, selector, Arrays.toString(columnNames), constraint, offset, limit });
    }
    QueryResult result = new SimpleQueryResult(columnNames, selectorNames, rows);
    if (NATIVE_SORT) {
        return result;
    }
    long timeSort = System.currentTimeMillis();
    QueryResult sorted = sort(result, orderings, evaluator, offset, limit);
    log.debug("{}SQL2 SORT took {} ms.", genString(printIndentation), System.currentTimeMillis() - timeSort);
    return sorted;
}
Also used : PropertyValue(javax.jcr.query.qom.PropertyValue) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) QueryResult(javax.jcr.query.QueryResult) RowIteratorAdapter(org.apache.jackrabbit.commons.iterator.RowIteratorAdapter) NodeType(javax.jcr.nodetype.NodeType) RowIterator(javax.jcr.query.RowIterator) Sort(org.apache.lucene.search.Sort)

Example 19 with RowIterator

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

the class SimpleQueryResult method getRows.

/**
     * Returns the query result rows.
     *
     * @return query result rows
     * @throws RepositoryException if the query results have already
     *                             been iterated through
     */
public synchronized RowIterator getRows() throws RepositoryException {
    if (rowIterator != null) {
        RowIterator iterator = rowIterator;
        rowIterator = null;
        return iterator;
    } else {
        throw new RepositoryException("This query result has already been iterated through");
    }
}
Also used : RowIterator(javax.jcr.query.RowIterator) RepositoryException(javax.jcr.RepositoryException)

Example 20 with RowIterator

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

the class SQL2OrderByTest method testOrderByScore.

public void testOrderByScore() throws RepositoryException {
    Node n1 = testRootNode.addNode("node1");
    Node n2 = testRootNode.addNode("node2");
    Node n3 = testRootNode.addNode("node3");
    n1.setProperty("text", "aaa");
    n1.setProperty("value", 3);
    testRootNode.getSession().save();
    n2.setProperty("text", "bbb");
    n2.setProperty("value", 2);
    testRootNode.getSession().save();
    n3.setProperty("text", "ccc");
    n3.setProperty("value", 2);
    testRootNode.getSession().save();
    QueryResult qr = executeSQL2Query("SELECT * FROM [nt:base] WHERE ISCHILDNODE([" + testRoot + "]) ORDER BY SCORE()");
    RowIterator rows = qr.getRows();
    long size = rows.getSize();
    assertTrue(size == 3 || size == -1);
    size = 0;
    double score = Double.MIN_VALUE;
    while (rows.hasNext()) {
        double nextScore = rows.nextRow().getScore();
        assertTrue(nextScore >= score);
        score = nextScore;
        size++;
    }
    assertEquals(3, size);
}
Also used : QueryResult(javax.jcr.query.QueryResult) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator)

Aggregations

RowIterator (javax.jcr.query.RowIterator)86 Node (javax.jcr.Node)48 QueryResult (javax.jcr.query.QueryResult)45 QueryManager (javax.jcr.query.QueryManager)27 Row (javax.jcr.query.Row)27 Query (javax.jcr.query.Query)25 Test (org.junit.Test)20 Session (javax.jcr.Session)17 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)15 RepositoryException (javax.jcr.RepositoryException)12 Value (javax.jcr.Value)11 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)8 ValueFactory (javax.jcr.ValueFactory)7 ArrayList (java.util.ArrayList)5 NodeIterator (javax.jcr.NodeIterator)5 NoSuchElementException (java.util.NoSuchElementException)4 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)4 RowIteratorAdapter (org.apache.jackrabbit.commons.iterator.RowIteratorAdapter)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3