use of org.apache.geode.cache.lucene.PageableLuceneQueryResults 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.PageableLuceneQueryResults in project geode by apache.
the class LuceneSearchIndexFunctionJUnitTest method getMockLuceneService.
private InternalLuceneService getMockLuceneService(String resultKey, String resultValue, String resultScore) throws LuceneQueryException {
InternalLuceneService service = mock(InternalLuceneService.class);
LuceneQueryFactory mockQueryFactory = spy(LuceneQueryFactory.class);
LuceneQuery mockQuery = mock(LuceneQuery.class);
PageableLuceneQueryResults pageableLuceneQueryResults = mock(PageableLuceneQueryResults.class);
LuceneResultStruct<String, String> resultStruct = new LuceneResultStructImpl(resultKey, resultValue, Float.valueOf(resultScore));
List<LuceneResultStruct<String, String>> queryResults = new ArrayList<>();
queryResults.add(resultStruct);
doReturn(mock(LuceneIndex.class)).when(service).getIndex(anyString(), anyString());
doReturn(mockQueryFactory).when(service).createLuceneQueryFactory();
doReturn(mockQueryFactory).when(mockQueryFactory).setLimit(anyInt());
doReturn(mockQuery).when(mockQueryFactory).create(any(), any(), any(), any());
when(mockQuery.findPages()).thenReturn(pageableLuceneQueryResults);
when(pageableLuceneQueryResults.hasNext()).thenReturn(true).thenReturn(false);
when(pageableLuceneQueryResults.next()).thenReturn(queryResults);
return service;
}
Aggregations