Search in sources :

Example 1 with LuceneFunctionContext

use of org.apache.geode.cache.lucene.internal.distributed.LuceneFunctionContext in project geode by apache.

the class LuceneQueryImpl method findTopEntries.

private TopEntries<K> findTopEntries() throws LuceneQueryException {
    TopEntriesCollectorManager manager = new TopEntriesCollectorManager(null, limit);
    LuceneFunctionContext<TopEntriesCollector> context = new LuceneFunctionContext<>(query, indexName, manager, limit);
    // TODO provide a timeout to the user?
    TopEntries<K> entries = null;
    try {
        TopEntriesFunctionCollector collector = new TopEntriesFunctionCollector(context);
        ResultCollector<TopEntriesCollector, TopEntries<K>> rc = onRegion().setArguments(context).withCollector(collector).execute(LuceneQueryFunction.ID);
        entries = rc.getResult();
    } catch (FunctionException e) {
        if (e.getCause() instanceof LuceneQueryException) {
            throw new LuceneQueryException(e);
        } else if (e.getCause() instanceof TransactionException) {
            // When run from client with single hop disabled
            throw new LuceneQueryException(LUCENE_QUERY_CANNOT_BE_EXECUTED_WITHIN_A_TRANSACTION);
        } else if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    } catch (TransactionException e) {
        // When function execution is run from server
        throw new LuceneQueryException(LUCENE_QUERY_CANNOT_BE_EXECUTED_WITHIN_A_TRANSACTION);
    }
    return entries;
}
Also used : LuceneFunctionContext(org.apache.geode.cache.lucene.internal.distributed.LuceneFunctionContext) TopEntriesCollector(org.apache.geode.cache.lucene.internal.distributed.TopEntriesCollector) LuceneQueryException(org.apache.geode.cache.lucene.LuceneQueryException) TopEntriesFunctionCollector(org.apache.geode.cache.lucene.internal.distributed.TopEntriesFunctionCollector) FunctionException(org.apache.geode.cache.execute.FunctionException) TransactionException(org.apache.geode.cache.TransactionException) TopEntriesCollectorManager(org.apache.geode.cache.lucene.internal.distributed.TopEntriesCollectorManager) TopEntries(org.apache.geode.cache.lucene.internal.distributed.TopEntries)

Example 2 with LuceneFunctionContext

use of org.apache.geode.cache.lucene.internal.distributed.LuceneFunctionContext in project geode by apache.

the class LuceneQueryImplJUnitTest method shouldInvokeLuceneFunctionWithCorrectArguments.

@Test
public void shouldInvokeLuceneFunctionWithCorrectArguments() throws Exception {
    addValueToResults();
    PageableLuceneQueryResults<Object, Object> results = query.findPages();
    verify(execution).execute(eq(LuceneQueryFunction.ID));
    ArgumentCaptor<LuceneFunctionContext> captor = ArgumentCaptor.forClass(LuceneFunctionContext.class);
    verify(execution).setArguments(captor.capture());
    LuceneFunctionContext context = captor.getValue();
    assertEquals(LIMIT, context.getLimit());
    assertEquals(provider, context.getQueryProvider());
    assertEquals("index", context.getIndexName());
    assertEquals(5, results.getMaxScore(), 0.01);
    assertEquals(1, results.size());
    final List<LuceneResultStruct<Object, Object>> page = results.next();
    assertEquals(1, page.size());
    LuceneResultStruct element = page.iterator().next();
    assertEquals("hi", element.getKey());
    assertEquals("value", element.getValue());
    assertEquals(5, element.getScore(), 0.01);
}
Also used : LuceneFunctionContext(org.apache.geode.cache.lucene.internal.distributed.LuceneFunctionContext) LuceneResultStruct(org.apache.geode.cache.lucene.LuceneResultStruct) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

LuceneFunctionContext (org.apache.geode.cache.lucene.internal.distributed.LuceneFunctionContext)2 TransactionException (org.apache.geode.cache.TransactionException)1 FunctionException (org.apache.geode.cache.execute.FunctionException)1 LuceneQueryException (org.apache.geode.cache.lucene.LuceneQueryException)1 LuceneResultStruct (org.apache.geode.cache.lucene.LuceneResultStruct)1 TopEntries (org.apache.geode.cache.lucene.internal.distributed.TopEntries)1 TopEntriesCollector (org.apache.geode.cache.lucene.internal.distributed.TopEntriesCollector)1 TopEntriesCollectorManager (org.apache.geode.cache.lucene.internal.distributed.TopEntriesCollectorManager)1 TopEntriesFunctionCollector (org.apache.geode.cache.lucene.internal.distributed.TopEntriesFunctionCollector)1 UnitTest (org.apache.geode.test.junit.categories.UnitTest)1 Test (org.junit.Test)1