Search in sources :

Example 46 with QueryManager

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

the class LuceneIndexSuggestionTest method checkSuggestions.

/**
     * Utility method to check suggestion over {@code queryNodeType} when the index definition is created for
     * {@code indexNodeType}
     */
private void checkSuggestions(final String indexNodeType, final String queryNodeType, final String indexPropName, final String indexPropValue, final boolean addFullText, final boolean useUserSession, final String suggestQueryText, final boolean shouldSuggest, final boolean suggestAnalyzed) throws Exception {
    createSuggestIndex("lucene-suggest", indexNodeType, indexPropName, addFullText, suggestAnalyzed);
    Node indexedNode = root.addNode("indexedNode1", queryNodeType);
    if (indexPropValue != null) {
        indexedNode.setProperty(indexPropName, indexPropValue + " 1");
        indexedNode = root.addNode("indexedNode2", queryNodeType);
        indexedNode.setProperty(indexPropName, indexPropValue + " 2");
    }
    if (useUserSession) {
        session.getUserManager().createUser(TEST_USER_NAME, TEST_USER_NAME);
    }
    session.save();
    Session userSession = session;
    if (useUserSession) {
        AccessControlUtils.allow(indexedNode, TEST_USER_NAME, Privilege.JCR_READ);
        session.save();
        userSession = repository.login(new SimpleCredentials(TEST_USER_NAME, TEST_USER_NAME.toCharArray()));
    }
    String suggQuery = createSuggestQuery(queryNodeType, suggestQueryText);
    QueryManager queryManager = userSession.getWorkspace().getQueryManager();
    QueryResult result = queryManager.createQuery(suggQuery, Query.JCR_SQL2).execute();
    RowIterator rows = result.getRows();
    String value = null;
    while (rows.hasNext()) {
        Row firstRow = rows.nextRow();
        value = firstRow.getValue("suggestion").getString();
    }
    if (shouldSuggest) {
        assertNotNull("There should be some suggestion", value);
    } else {
        assertNull("There shouldn't be any suggestion", value);
    }
    userSession.logout();
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) QueryResult(javax.jcr.query.QueryResult) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Row(javax.jcr.query.Row) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 47 with QueryManager

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

the class MassiveWildcardTest method testWildcardQuery.

/**
     * Executes a wildcard query covering 2'000 different property values.
     */
public void testWildcardQuery() throws RepositoryException {
    int count = 0;
    for (int i = 0; i < 20; i++) {
        Node child = testRootNode.addNode("node" + i);
        for (int j = 0; j < 100; j++) {
            Node n = child.addNode("node" + j);
            n.setProperty("foo", "" + count + "foo");
            n.setProperty("bar", "bar" + count++);
        }
        // save every 100 nodes
        testRootNode.save();
    }
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    String stmt = testPath + "//*[jcr:contains(., '*foo')]";
    QueryResult res = qm.createQuery(stmt, Query.XPATH).execute();
    checkResult(res, 2000);
    stmt = testPath + "//*[jcr:contains(., 'bar*')]";
    res = qm.createQuery(stmt, Query.XPATH).execute();
    checkResult(res, 2000);
}
Also used : QueryResult(javax.jcr.query.QueryResult) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager)

Example 48 with QueryManager

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

the class QueryResultTest method testSkip.

public void testSkip() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    for (int i = 0; i < 10; i++) {
        String stmt = testPath + "/*[@" + propertyName1 + " < 1000]";
        QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
        for (int j = 0; j < INITIAL_NODE_NUM - i; j++) {
            // skip to each node in the result
            NodeIterator it = result.getNodes();
            it.skip(j);
            long propValue = it.nextNode().getProperty(propertyName1).getLong();
            // expected = number of skipped nodes + number of deleted nodes
            long expected = j + i;
            assertEquals("Wrong node after skip()", expected, propValue);
        }
        try {
            NodeIterator it = result.getNodes();
            it.skip(it.getSize() + 1);
            fail("must throw NoSuchElementException");
        } catch (NoSuchElementException e) {
        // correct
        }
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager) NoSuchElementException(java.util.NoSuchElementException)

Example 49 with QueryManager

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

the class QueryResultTest method testGetPositionOrderBy.

public void testGetPositionOrderBy() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    for (int i = 0; i < 10; i++) {
        String stmt = testPath + "/*[@" + propertyName1 + " < 1000] order by jcr:score()";
        QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
        NodeIterator it = result.getNodes();
        assertEquals("Wrong position", 0, it.getPosition());
        int count = 0;
        while (it.hasNext()) {
            long position = it.getPosition();
            it.nextNode();
            assertEquals("Wrong position", count++, position);
        }
        try {
            it.next();
            fail("must throw NoSuchElementException");
        } catch (Exception e) {
        // correct
        }
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager) RepositoryException(javax.jcr.RepositoryException) NoSuchElementException(java.util.NoSuchElementException)

Example 50 with QueryManager

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

the class QueryResultTest method testPositionEmptyResult.

public void testPositionEmptyResult() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    String stmt = testPath + "/*[@" + propertyName1 + " > 1000]";
    QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
    assertEquals("Wrong position", 0, result.getNodes().getPosition());
    assertEquals("Wrong position", 0, result.getRows().getPosition());
    stmt += " order by jcr:score()";
    result = qm.createQuery(stmt, Query.XPATH).execute();
    assertEquals("Wrong position", 0, result.getNodes().getPosition());
    assertEquals("Wrong position", 0, result.getRows().getPosition());
}
Also used : QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager)

Aggregations

QueryManager (javax.jcr.query.QueryManager)103 Query (javax.jcr.query.Query)69 Node (javax.jcr.Node)61 QueryResult (javax.jcr.query.QueryResult)55 Session (javax.jcr.Session)54 NodeIterator (javax.jcr.NodeIterator)34 RowIterator (javax.jcr.query.RowIterator)27 Test (org.junit.Test)27 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)22 RepositoryException (javax.jcr.RepositoryException)18 Row (javax.jcr.query.Row)14 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)14 FacetResult (org.apache.jackrabbit.oak.query.facet.FacetResult)9 ValueFactory (javax.jcr.ValueFactory)7 NoSuchElementException (java.util.NoSuchElementException)6 InvalidItemStateException (javax.jcr.InvalidItemStateException)3 Value (javax.jcr.Value)3 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2