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);
}
}
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);
}
}
Aggregations