Search in sources :

Example 1 with PageableLuceneQueryResults

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

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;
}
Also used : LuceneQuery(org.apache.geode.cache.lucene.LuceneQuery) LuceneResultStructImpl(org.apache.geode.cache.lucene.internal.LuceneResultStructImpl) PageableLuceneQueryResults(org.apache.geode.cache.lucene.PageableLuceneQueryResults) LuceneIndex(org.apache.geode.cache.lucene.LuceneIndex) ArrayList(java.util.ArrayList) InternalLuceneService(org.apache.geode.cache.lucene.internal.InternalLuceneService) LuceneQueryFactory(org.apache.geode.cache.lucene.LuceneQueryFactory) LuceneResultStruct(org.apache.geode.cache.lucene.LuceneResultStruct)

Aggregations

LuceneResultStruct (org.apache.geode.cache.lucene.LuceneResultStruct)2 PageableLuceneQueryResults (org.apache.geode.cache.lucene.PageableLuceneQueryResults)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Cache (org.apache.geode.cache.Cache)1 LuceneIndex (org.apache.geode.cache.lucene.LuceneIndex)1 LuceneQuery (org.apache.geode.cache.lucene.LuceneQuery)1 LuceneQueryException (org.apache.geode.cache.lucene.LuceneQueryException)1 LuceneQueryFactory (org.apache.geode.cache.lucene.LuceneQueryFactory)1 LuceneService (org.apache.geode.cache.lucene.LuceneService)1 InternalLuceneService (org.apache.geode.cache.lucene.internal.InternalLuceneService)1 LuceneResultStructImpl (org.apache.geode.cache.lucene.internal.LuceneResultStructImpl)1 LuceneQueryInfo (org.apache.geode.cache.lucene.internal.cli.LuceneQueryInfo)1 LuceneSearchResults (org.apache.geode.cache.lucene.internal.cli.LuceneSearchResults)1