Search in sources :

Example 56 with QueryManager

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

the class LuceneIndexDescendantSuggestionTest method ambiguousSubtreeIndexWithDescendantConstraint.

@Ignore("OAK-3992")
@Test
public void ambiguousSubtreeIndexWithDescendantConstraint() throws Exception {
    String query = createSuggestQuery(NT_OAK_UNSTRUCTURED, "te", "/content2");
    String explainQuery = "EXPLAIN " + createSuggestQuery(NT_OAK_UNSTRUCTURED, "te", "/content2");
    QueryManager queryManager = session.getWorkspace().getQueryManager();
    QueryResult result = queryManager.createQuery(explainQuery, Query.JCR_SQL2).execute();
    RowIterator rows = result.getRows();
    String explanation = rows.nextRow().toString();
    assertTrue("Subtree index should get picked", explanation.contains("lucene:sugg-idx(/content2/oak:index/sugg-idx)"));
    validateSuggestions(query, newHashSet("test4"));
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 57 with QueryManager

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

the class FacetTest method testFacetsNA.

public void testFacetsNA() throws Exception {
    if (superuser.itemExists(FACET_CONFING_PROP_PATH)) {
        superuser.getItem(FACET_CONFING_PROP_PATH).remove();
        markIndexForReindex();
        superuser.save();
    }
    Session session = superuser;
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node n1 = testRootNode.addNode("node1");
    n1.setProperty("text", "foo");
    Node n2 = testRootNode.addNode("node2");
    n2.setProperty("text", "bar");
    session.save();
    String sql2 = "select [jcr:path], [rep:facet(text)] from [nt:base] " + "where contains([text], 'foo OR bar')";
    Query q = qm.createQuery(sql2, Query.JCR_SQL2);
    QueryResult result = q.execute();
    FacetResult facetResult = new FacetResult(result);
    assertNotNull(facetResult);
    assertTrue(facetResult.getDimensions().isEmpty());
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) FacetResult(org.apache.jackrabbit.oak.query.facet.FacetResult) Session(javax.jcr.Session)

Example 58 with QueryManager

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

the class FacetTest method testFacetRetrievalNumberOfFacetsConfiguredHigherThanDefault.

public void testFacetRetrievalNumberOfFacetsConfiguredHigherThanDefault() throws RepositoryException {
    Node facetsConfig = superuser.getNode(FACET_CONFING_NODE_PATH);
    facetsConfig.setProperty(LuceneIndexConstants.PROP_FACETS_TOP_CHILDREN, 11);
    markIndexForReindex();
    superuser.save();
    superuser.refresh(true);
    Session session = superuser;
    Node n1 = testRootNode.addNode("node1");
    String pn = "jcr:title";
    n1.setProperty(pn, "hello 1");
    Node n2 = testRootNode.addNode("node2");
    n2.setProperty(pn, "hallo 2");
    Node n3 = testRootNode.addNode("node3");
    n3.setProperty(pn, "hallo 3");
    Node n4 = testRootNode.addNode("node4");
    n4.setProperty(pn, "hallo 4");
    Node n5 = testRootNode.addNode("node5");
    n5.setProperty(pn, "hallo 5");
    Node n6 = testRootNode.addNode("node6");
    n6.setProperty(pn, "hallo 6");
    Node n7 = testRootNode.addNode("node7");
    n7.setProperty(pn, "hallo 7");
    Node n8 = testRootNode.addNode("node8");
    n8.setProperty(pn, "hallo 8");
    Node n9 = testRootNode.addNode("node9");
    n9.setProperty(pn, "hallo 9");
    Node n10 = testRootNode.addNode("node10");
    n10.setProperty(pn, "hallo 10");
    Node n11 = testRootNode.addNode("node11");
    n11.setProperty(pn, "hallo 11");
    Node n12 = testRootNode.addNode("node12");
    n12.setProperty(pn, "hallo 12");
    session.save();
    QueryManager qm = session.getWorkspace().getQueryManager();
    String sql2 = "select [jcr:path], [rep:facet(" + pn + ")] from [nt:base] " + "where contains([" + pn + "], 'hallo') order by [jcr:path]";
    Query q = qm.createQuery(sql2, Query.JCR_SQL2);
    QueryResult result = q.execute();
    FacetResult facetResult = new FacetResult(result);
    assertNotNull(facetResult);
    assertNotNull(facetResult.getDimensions());
    assertEquals(1, facetResult.getDimensions().size());
    assertTrue(facetResult.getDimensions().contains(pn));
    List<FacetResult.Facet> facets = facetResult.getFacets(pn);
    assertNotNull(facets);
    assertEquals(11, facets.size());
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) FacetResult(org.apache.jackrabbit.oak.query.facet.FacetResult) Session(javax.jcr.Session)

Example 59 with QueryManager

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

the class JsonIndexCommand method runQuery.

private void runQuery(String query, String language, String columnName, boolean quiet, int depth) throws RepositoryException {
    ArrayList<String> list = new ArrayList<String>();
    columnName = query.startsWith("explain") ? "plan" : columnName;
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery(query, language);
    for (String b : q.getBindVariableNames()) {
        ValueFactory vf = session.getValueFactory();
        q.bindValue(b, vf.createValue(data.get("$" + b).toString()));
    }
    QueryResult result = q.execute();
    if (depth != 0) {
        NodeIterator ni = result.getNodes();
        JsopBuilder builder = new JsopBuilder().array();
        while (ni.hasNext()) {
            Node n = ni.nextNode();
            builder.key(n.getPath());
            appendNode(builder, n, depth - 1);
        }
        output.println(JsopBuilder.prettyPrint(builder.endArray().toString()));
        return;
    }
    RowIterator ri = result.getRows();
    while (ri.hasNext()) {
        Row r = ri.nextRow();
        if (columnName != null) {
            String x = r.getValue(columnName).getString();
            list.add(x);
            if (!quiet) {
                output.println(x);
            }
        } else {
            String[] columnNames = result.getColumnNames();
            for (String cn : columnNames) {
                Value v = r.getValue(cn);
                String x = v == null ? null : v.getString();
                if (columnNames.length == 1) {
                    list.add(x);
                    if (!quiet) {
                        output.println(x);
                    }
                } else {
                    list.add(x);
                    if (!quiet) {
                        output.println(cn + ": " + x);
                    }
                }
            }
        }
    }
    data.put("$resultSize", (long) list.size());
    data.put("$result", list.toArray(new String[0]));
}
Also used : NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) ValueFactory(javax.jcr.ValueFactory) QueryResult(javax.jcr.query.QueryResult) JsopBuilder(org.apache.jackrabbit.oak.commons.json.JsopBuilder) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Value(javax.jcr.Value) Row(javax.jcr.query.Row)

Example 60 with QueryManager

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

the class FacetTest method testMultipleFacetsRetrieval.

public void testMultipleFacetsRetrieval() throws Exception {
    Session session = superuser;
    Node n1 = testRootNode.addNode("node1");
    String pn = "jcr:title";
    String pn2 = "jcr:description";
    n1.setProperty(pn, "hello");
    n1.setProperty(pn2, "a");
    Node n2 = testRootNode.addNode("node2");
    n2.setProperty(pn, "hallo");
    n2.setProperty(pn2, "b");
    Node n3 = testRootNode.addNode("node3");
    n3.setProperty(pn, "oh hallo");
    n3.setProperty(pn2, "a");
    session.save();
    QueryManager qm = session.getWorkspace().getQueryManager();
    String sql2 = "select [jcr:path], [rep:facet(" + pn + ")], [rep:facet(" + pn2 + ")] from [nt:base] " + "where contains([" + pn + "], 'hallo') order by [jcr:path]";
    Query q = qm.createQuery(sql2, Query.JCR_SQL2);
    QueryResult result = q.execute();
    FacetResult facetResult = new FacetResult(result);
    assertNotNull(facetResult);
    assertNotNull(facetResult.getDimensions());
    assertEquals(2, facetResult.getDimensions().size());
    assertTrue(facetResult.getDimensions().contains(pn));
    assertTrue(facetResult.getDimensions().contains(pn2));
    List<FacetResult.Facet> facets = facetResult.getFacets(pn);
    assertNotNull(facets);
    assertEquals("hallo", facets.get(0).getLabel());
    assertEquals(1, facets.get(0).getCount(), 0);
    assertEquals("oh hallo", facets.get(1).getLabel());
    assertEquals(1, facets.get(1).getCount(), 0);
    List<FacetResult.Facet> facets1 = facetResult.getFacets(pn2);
    assertNotNull(facets1);
    assertEquals("a", facets1.get(0).getLabel());
    assertEquals(1, facets1.get(0).getCount(), 0);
    assertEquals("b", facets1.get(1).getLabel());
    assertEquals(1, facets1.get(1).getCount(), 0);
    NodeIterator nodes = result.getNodes();
    assertTrue(nodes.hasNext());
    assertNotNull(nodes.nextNode());
    assertTrue(nodes.hasNext());
    assertNotNull(nodes.nextNode());
    assertFalse(nodes.hasNext());
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) FacetResult(org.apache.jackrabbit.oak.query.facet.FacetResult) Session(javax.jcr.Session)

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