Search in sources :

Example 96 with QueryResult

use of javax.jcr.query.QueryResult 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 97 with QueryResult

use of javax.jcr.query.QueryResult in project sling by apache.

the class MockQueryManagerTest method testQueryResults_ResultHandler_Rows.

@SuppressWarnings("unchecked")
@Test
public void testQueryResults_ResultHandler_Rows() throws RepositoryException {
    final List<String> columnNames = ImmutableList.of("stringProp", "intProp", "optionalStringProp");
    MockJcr.addQueryResultHandler(queryManager, new MockQueryResultHandler() {

        @Override
        public MockQueryResult executeQuery(MockQuery query) {
            return new MockQueryResult(sampleNodes, columnNames);
        }
    });
    Query query = queryManager.createQuery("query1", Query.JCR_SQL2);
    QueryResult result = query.execute();
    assertEquals(sampleNodes, ImmutableList.copyOf(result.getNodes()));
    assertEquals(columnNames, ImmutableList.copyOf(result.getColumnNames()));
    List<Row> rows = ImmutableList.copyOf(result.getRows());
    assertEquals("value1", rows.get(0).getValue("stringProp").getString());
    assertEquals(1L, rows.get(0).getValue("intProp").getLong());
    assertEquals("optValue1", rows.get(0).getValue("optionalStringProp").getString());
    assertEquals("value2", rows.get(1).getValues()[0].getString());
    assertEquals(2L, rows.get(1).getValues()[1].getLong());
    assertNull(rows.get(1).getValues()[2]);
    assertEquals("value3", rows.get(2).getValues()[0].getString());
    assertEquals(3L, rows.get(2).getValues()[1].getLong());
    assertNull(rows.get(2).getValues()[2]);
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Row(javax.jcr.query.Row) Test(org.junit.Test)

Example 98 with QueryResult

use of javax.jcr.query.QueryResult in project sling by apache.

the class MockQueryManagerTest method testNoQueryResults.

@Test
public void testNoQueryResults() throws RepositoryException {
    Query query = queryManager.createQuery("dummy", Query.JCR_SQL2);
    QueryResult result = query.execute();
    assertFalse(result.getNodes().hasNext());
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Test(org.junit.Test)

Example 99 with QueryResult

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

the class QueryImpl method execute.

/**
     * This method simply forwards the <code>execute</code> call to the
     * {@link ExecutableQuery} object returned by
     * {@link QueryHandler#createExecutableQuery}.
     * {@inheritDoc}
     */
public QueryResult execute() throws RepositoryException {
    checkInitialized();
    long time = System.nanoTime();
    QueryResult result = sessionContext.getSessionState().perform(new SessionOperation<QueryResult>() {

        public QueryResult perform(SessionContext context) throws RepositoryException {
            return query.execute(offset, limit);
        }

        public String toString() {
            return "query.execute(" + statement + ")";
        }
    });
    time = System.nanoTime() - time;
    final long timeMs = time / 1000000;
    log.debug("executed in {} ms. ({})", timeMs, statement);
    RepositoryStatisticsImpl statistics = sessionContext.getRepositoryContext().getRepositoryStatistics();
    statistics.getCounter(Type.QUERY_COUNT).incrementAndGet();
    statistics.getCounter(Type.QUERY_DURATION).addAndGet(timeMs);
    sessionContext.getRepositoryContext().getStatManager().getQueryStat().logQuery(language, statement, timeMs);
    return result;
}
Also used : QueryResult(javax.jcr.query.QueryResult) RepositoryStatisticsImpl(org.apache.jackrabbit.stats.RepositoryStatisticsImpl) SessionContext(org.apache.jackrabbit.core.session.SessionContext) RepositoryException(javax.jcr.RepositoryException)

Example 100 with QueryResult

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

the class QueryObjectModelImpl method execute.

public QueryResult execute() throws RepositoryException {
    long time = System.nanoTime();
    final QueryResult result = sessionContext.getSessionState().perform(new SessionOperation<QueryResult>() {

        public QueryResult perform(SessionContext context) throws RepositoryException {
            final QueryEngine engine = new QueryEngine(sessionContext.getSessionImpl(), lqf, variables);
            return engine.execute(getColumns(), getSource(), getConstraint(), getOrderings(), offset, limit);
        }

        public String toString() {
            return "query.execute(" + statement + ")";
        }
    });
    time = System.nanoTime() - time;
    final long timeMs = time / 1000000;
    log.debug("executed in {} ms. ({})", timeMs, statement);
    RepositoryStatisticsImpl statistics = sessionContext.getRepositoryContext().getRepositoryStatistics();
    statistics.getCounter(Type.QUERY_COUNT).incrementAndGet();
    statistics.getCounter(Type.QUERY_DURATION).addAndGet(timeMs);
    sessionContext.getRepositoryContext().getStatManager().getQueryStat().logQuery(language, statement, timeMs);
    return result;
}
Also used : QueryResult(javax.jcr.query.QueryResult) RepositoryStatisticsImpl(org.apache.jackrabbit.stats.RepositoryStatisticsImpl) SessionContext(org.apache.jackrabbit.core.session.SessionContext) RepositoryException(javax.jcr.RepositoryException) QueryEngine(org.apache.jackrabbit.core.query.lucene.join.QueryEngine)

Aggregations

QueryResult (javax.jcr.query.QueryResult)200 Node (javax.jcr.Node)108 Query (javax.jcr.query.Query)97 QueryManager (javax.jcr.query.QueryManager)54 NodeIterator (javax.jcr.NodeIterator)52 RowIterator (javax.jcr.query.RowIterator)46 Session (javax.jcr.Session)35 Test (org.junit.Test)32 Row (javax.jcr.query.Row)21 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)21 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)15 RepositoryException (javax.jcr.RepositoryException)14 FacetResult (org.apache.jackrabbit.oak.query.facet.FacetResult)9 Value (javax.jcr.Value)8 NoSuchElementException (java.util.NoSuchElementException)7 ArrayList (java.util.ArrayList)6 ValueFactory (javax.jcr.ValueFactory)6 InvalidItemStateException (javax.jcr.InvalidItemStateException)5 JackrabbitQueryResult (org.apache.jackrabbit.api.query.JackrabbitQueryResult)5 TreeSet (java.util.TreeSet)4