Search in sources :

Example 1 with LuceneSearchResults

use of org.apache.geode.cache.lucene.internal.cli.LuceneSearchResults in project geode by apache.

the class LuceneSearchIndexFunction method execute.

public void execute(final FunctionContext context) {
    Set<LuceneSearchResults> result = new HashSet<>();
    final Cache cache = getCache();
    final LuceneQueryInfo queryInfo = (LuceneQueryInfo) context.getArguments();
    LuceneService luceneService = LuceneServiceProvider.get(getCache());
    try {
        if (luceneService.getIndex(queryInfo.getIndexName(), queryInfo.getRegionPath()) == null) {
            throw new Exception("Index " + queryInfo.getIndexName() + " not found on region " + queryInfo.getRegionPath());
        }
        final LuceneQuery<K, V> query = luceneService.createLuceneQueryFactory().setLimit(queryInfo.getLimit()).create(queryInfo.getIndexName(), queryInfo.getRegionPath(), queryInfo.getQueryString(), queryInfo.getDefaultField());
        if (queryInfo.getKeysOnly()) {
            query.findKeys().forEach(key -> result.add(new LuceneSearchResults(key.toString())));
        } else {
            PageableLuceneQueryResults pageableLuceneQueryResults = query.findPages();
            while (pageableLuceneQueryResults.hasNext()) {
                List<LuceneResultStruct> page = pageableLuceneQueryResults.next();
                page.stream().forEach(searchResult -> result.add(new LuceneSearchResults<K, V>(searchResult.getKey().toString(), searchResult.getValue().toString(), searchResult.getScore())));
            }
        }
        if (result != null) {
            context.getResultSender().lastResult(result);
        }
    } catch (LuceneQueryException e) {
        result.add(new LuceneSearchResults(true, e.getRootCause().getMessage()));
        context.getResultSender().lastResult(result);
    } catch (Exception e) {
        result.add(new LuceneSearchResults(true, e.getMessage()));
        context.getResultSender().lastResult(result);
    }
}
Also used : LuceneSearchResults(org.apache.geode.cache.lucene.internal.cli.LuceneSearchResults) LuceneQueryException(org.apache.geode.cache.lucene.LuceneQueryException) LuceneQueryException(org.apache.geode.cache.lucene.LuceneQueryException) LuceneQueryInfo(org.apache.geode.cache.lucene.internal.cli.LuceneQueryInfo) LuceneService(org.apache.geode.cache.lucene.LuceneService) LuceneResultStruct(org.apache.geode.cache.lucene.LuceneResultStruct) PageableLuceneQueryResults(org.apache.geode.cache.lucene.PageableLuceneQueryResults) HashSet(java.util.HashSet) Cache(org.apache.geode.cache.Cache)

Example 2 with LuceneSearchResults

use of org.apache.geode.cache.lucene.internal.cli.LuceneSearchResults in project geode by apache.

the class LuceneSearchIndexFunctionJUnitTest method testExecute.

@Test
@SuppressWarnings("unchecked")
public void testExecute() throws LuceneQueryException {
    FunctionContext context = mock(FunctionContext.class);
    ResultSender resultSender = mock(ResultSender.class);
    GemFireCacheImpl cache = Fakes.cache();
    LuceneQueryInfo queryInfo = createMockQueryInfo("index", "region", "field1:region1", "field1", 1);
    InternalLuceneService service = getMockLuceneService("A", "Value", "1.2");
    Region mockRegion = mock(Region.class);
    LuceneSearchIndexFunction function = spy(LuceneSearchIndexFunction.class);
    doReturn(queryInfo).when(context).getArguments();
    doReturn(resultSender).when(context).getResultSender();
    doReturn(cache).when(function).getCache();
    when(cache.getService(eq(InternalLuceneService.class))).thenReturn(service);
    when(cache.getRegion(queryInfo.getRegionPath())).thenReturn(mockRegion);
    function.execute(context);
    ArgumentCaptor<Set> resultCaptor = ArgumentCaptor.forClass(Set.class);
    verify(resultSender).lastResult(resultCaptor.capture());
    Set<LuceneSearchResults> result = resultCaptor.getValue();
    assertEquals(1, result.size());
    for (LuceneSearchResults searchResult : result) {
        assertEquals("A", searchResult.getKey());
        assertEquals("Value", searchResult.getValue());
        assertEquals(1.2, searchResult.getScore(), .1);
    }
}
Also used : Set(java.util.Set) LuceneSearchResults(org.apache.geode.cache.lucene.internal.cli.LuceneSearchResults) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) Region(org.apache.geode.cache.Region) InternalLuceneService(org.apache.geode.cache.lucene.internal.InternalLuceneService) FunctionContext(org.apache.geode.cache.execute.FunctionContext) ResultSender(org.apache.geode.cache.execute.ResultSender) LuceneQueryInfo(org.apache.geode.cache.lucene.internal.cli.LuceneQueryInfo) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

LuceneQueryInfo (org.apache.geode.cache.lucene.internal.cli.LuceneQueryInfo)2 LuceneSearchResults (org.apache.geode.cache.lucene.internal.cli.LuceneSearchResults)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Cache (org.apache.geode.cache.Cache)1 Region (org.apache.geode.cache.Region)1 FunctionContext (org.apache.geode.cache.execute.FunctionContext)1 ResultSender (org.apache.geode.cache.execute.ResultSender)1 LuceneQueryException (org.apache.geode.cache.lucene.LuceneQueryException)1 LuceneResultStruct (org.apache.geode.cache.lucene.LuceneResultStruct)1 LuceneService (org.apache.geode.cache.lucene.LuceneService)1 PageableLuceneQueryResults (org.apache.geode.cache.lucene.PageableLuceneQueryResults)1 InternalLuceneService (org.apache.geode.cache.lucene.internal.InternalLuceneService)1 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)1 UnitTest (org.apache.geode.test.junit.categories.UnitTest)1 Test (org.junit.Test)1