Search in sources :

Example 1 with LuceneResultStruct

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

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);
}
Also used : LuceneFunctionContext(org.apache.geode.cache.lucene.internal.distributed.LuceneFunctionContext) LuceneResultStruct(org.apache.geode.cache.lucene.LuceneResultStruct) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 3 with LuceneResultStruct

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);
}
Also used : TopEntries(org.apache.geode.cache.lucene.internal.distributed.TopEntries) EntryScore(org.apache.geode.cache.lucene.internal.distributed.EntryScore) LuceneResultStruct(org.apache.geode.cache.lucene.LuceneResultStruct)

Example 4 with LuceneResultStruct

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;
}
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)4 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 EntryScore (org.apache.geode.cache.lucene.internal.distributed.EntryScore)1 LuceneFunctionContext (org.apache.geode.cache.lucene.internal.distributed.LuceneFunctionContext)1 TopEntries (org.apache.geode.cache.lucene.internal.distributed.TopEntries)1 UnitTest (org.apache.geode.test.junit.categories.UnitTest)1 Test (org.junit.Test)1