use of org.apache.geode.cache.lucene.LuceneResultStruct 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.LuceneResultStruct 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);
}
use of org.apache.geode.cache.lucene.LuceneResultStruct in project geode by apache.
the class LuceneQueryImplJUnitTest method addValueToResults.
private void addValueToResults() {
TopEntries entries = new TopEntries();
entries.addHit(new EntryScore("hi", 5));
when(collector.getResult()).thenReturn(entries);
when(results.getMaxScore()).thenReturn(5f);
when(results.size()).thenReturn(1);
List<LuceneResultStruct<Object, Object>> page = Collections.singletonList(new LuceneResultStructImpl<>("hi", "value", 5f));
when(results.next()).thenReturn(page);
when(results.hasNext()).thenReturn(true);
}
use of org.apache.geode.cache.lucene.LuceneResultStruct 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