Search in sources :

Example 71 with QueryManager

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

the class ResultSizeTest method doTestResultSize.

private void doTestResultSize(boolean union, int expected) throws RepositoryException {
    Session session = superuser;
    QueryManager qm = session.getWorkspace().getQueryManager();
    String xpath;
    if (union) {
        xpath = "/jcr:root//*[jcr:contains(@text, 'Hello') or jcr:contains(@text, 'World')]";
    } else {
        xpath = "/jcr:root//*[jcr:contains(@text, 'Hello World')]";
    }
    Query q;
    long result;
    NodeIterator it;
    StringBuilder buff;
    // fast (insecure) case
    // enabled by default now, in LuceneOakRepositoryStub 
    System.clearProperty("oak.fastQuerySize");
    q = qm.createQuery(xpath, "xpath");
    it = q.execute().getNodes();
    result = it.getSize();
    assertTrue("size: " + result + " expected around " + expected, result > expected - 50 && result < expected + 50);
    buff = new StringBuilder();
    while (it.hasNext()) {
        Node n = it.nextNode();
        buff.append(n.getPath()).append('\n');
    }
    String fastSizeResult = buff.toString();
    q = qm.createQuery(xpath, "xpath");
    q.setLimit(90);
    it = q.execute().getNodes();
    assertEquals(90, it.getSize());
    // default (secure) case
    // manually disabled
    System.setProperty("oak.fastQuerySize", "false");
    q = qm.createQuery(xpath, "xpath");
    it = q.execute().getNodes();
    result = it.getSize();
    assertEquals(-1, result);
    buff = new StringBuilder();
    while (it.hasNext()) {
        Node n = it.nextNode();
        buff.append(n.getPath()).append('\n');
    }
    String regularResult = buff.toString();
    assertEquals(regularResult, fastSizeResult);
    System.clearProperty("oak.fastQuerySize");
}
Also used : NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session)

Example 72 with QueryManager

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

the class LuceneIndexSuggestionTest method avoidInfiniteSuggestions.

@Test
public void avoidInfiniteSuggestions() throws Exception {
    final String nodeType = "nt:unstructured";
    final String indexPropName = "description";
    final String higherRankPropValue = "DD DD DD DD";
    final String exceptionThrowingPropValue = "DD~@#$%^&*()_+{}\":?><`1234567890-=[]";
    final String suggestQueryText = "dd";
    createSuggestIndex("lucene-suggest", nodeType, indexPropName);
    root.addNode("higherRankNode", nodeType).setProperty(indexPropName, higherRankPropValue);
    root.addNode("exceptionThrowingNode", nodeType).setProperty(indexPropName, exceptionThrowingPropValue);
    session.save();
    String suggQuery = createSuggestQuery(nodeType, suggestQueryText);
    QueryManager queryManager = session.getWorkspace().getQueryManager();
    QueryResult result = queryManager.createQuery(suggQuery, Query.JCR_SQL2).execute();
    RowIterator rows = result.getRows();
    int count = 0;
    while (count < 3 && rows.hasNext()) {
        count++;
        rows.nextRow();
    }
    assertTrue("There must not be more than 2 suggestions", count <= 2);
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Test(org.junit.Test)

Example 73 with QueryManager

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

the class HybridIndexClusterIT method queryResult.

private static List<String> queryResult(Session session, String indexedPropName, String value) throws RepositoryException {
    session.refresh(false);
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery("select * from [nt:base] where [" + indexedPropName + "] = $value", Query.JCR_SQL2);
    q.bindValue("value", session.getValueFactory().createValue(value));
    QueryResult result = q.execute();
    List<String> paths = Lists.newArrayList();
    for (Row r : JcrUtils.getRows(result)) {
        paths.add(r.getPath());
    }
    return paths;
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) QueryManager(javax.jcr.query.QueryManager) Row(javax.jcr.query.Row)

Example 74 with QueryManager

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

the class PathBasedQueryTest method runTest.

@Override
protected void runTest() throws Exception {
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery("/jcr:root" + root.getPath() + "/*/*[@count = 250]", Query.XPATH);
    for (int i = 0; i < 10; i++) {
        q.execute().getNodes().nextNode();
    }
}
Also used : Query(javax.jcr.query.Query) QueryManager(javax.jcr.query.QueryManager)

Example 75 with QueryManager

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

the class SimpleSearchTest method runTest.

public void runTest() throws Exception {
    QueryManager manager = session.getWorkspace().getQueryManager();
    for (int i = 0; i < NODE_COUNT; i++) {
        Query query = createQuery(manager, i);
        NodeIterator iterator = query.execute().getNodes();
        while (iterator.hasNext()) {
            Node node = iterator.nextNode();
            if (node.getProperty("testcount").getLong() != i) {
                throw new Exception("Invalid test result: " + node.getPath());
            }
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) RepositoryException(javax.jcr.RepositoryException)

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