Search in sources :

Example 1 with LuceneQueryProvider

use of org.apache.geode.cache.lucene.LuceneQueryProvider in project geode by apache.

the class LuceneQueryFunction method execute.

@Override
public void execute(FunctionContext context) {
    RegionFunctionContext ctx = (RegionFunctionContext) context;
    ResultSender<TopEntriesCollector> resultSender = ctx.getResultSender();
    Region region = ctx.getDataSet();
    LuceneFunctionContext<IndexResultCollector> searchContext = (LuceneFunctionContext) ctx.getArguments();
    if (searchContext == null) {
        throw new IllegalArgumentException("Missing search context");
    }
    LuceneQueryProvider queryProvider = searchContext.getQueryProvider();
    if (queryProvider == null) {
        throw new IllegalArgumentException("Missing query provider");
    }
    LuceneIndexImpl index = getLuceneIndex(region, searchContext);
    if (index == null) {
        throw new LuceneIndexNotFoundException(searchContext.getIndexName(), region.getFullPath());
    }
    RepositoryManager repoManager = index.getRepositoryManager();
    LuceneIndexStats stats = index.getIndexStats();
    Query query = getQuery(queryProvider, index);
    if (logger.isDebugEnabled()) {
        logger.debug("Executing lucene query: {}, on region {}", query, region.getFullPath());
    }
    int resultLimit = searchContext.getLimit();
    CollectorManager manager = (searchContext == null) ? null : searchContext.getCollectorManager();
    if (manager == null) {
        manager = new TopEntriesCollectorManager(null, resultLimit);
    }
    Collection<IndexResultCollector> results = new ArrayList<>();
    TopEntriesCollector mergedResult = null;
    try {
        long start = stats.startQuery();
        Collection<IndexRepository> repositories = null;
        try {
            repositories = repoManager.getRepositories(ctx);
            for (IndexRepository repo : repositories) {
                IndexResultCollector collector = manager.newCollector(repo.toString());
                if (logger.isDebugEnabled()) {
                    logger.debug("Executing search on repo: " + repo.toString());
                }
                repo.query(query, resultLimit, collector);
                results.add(collector);
            }
            mergedResult = (TopEntriesCollector) manager.reduce(results);
        } finally {
            stats.endQuery(start, mergedResult == null ? 0 : mergedResult.size());
        }
        stats.incNumberOfQueryExecuted();
        resultSender.lastResult(mergedResult);
    } catch (IOException | BucketNotFoundException | CacheClosedException | PrimaryBucketException e) {
        logger.debug("Exception during lucene query function", e);
        throw new InternalFunctionInvocationTargetException(e);
    }
}
Also used : LuceneIndexStats(org.apache.geode.cache.lucene.internal.LuceneIndexStats) Query(org.apache.lucene.search.Query) LuceneQueryProvider(org.apache.geode.cache.lucene.LuceneQueryProvider) ArrayList(java.util.ArrayList) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) PrimaryBucketException(org.apache.geode.internal.cache.PrimaryBucketException) IndexResultCollector(org.apache.geode.cache.lucene.internal.repository.IndexResultCollector) RepositoryManager(org.apache.geode.cache.lucene.internal.repository.RepositoryManager) IOException(java.io.IOException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) LuceneIndexNotFoundException(org.apache.geode.cache.lucene.LuceneIndexNotFoundException) Region(org.apache.geode.cache.Region) BucketNotFoundException(org.apache.geode.internal.cache.BucketNotFoundException) LuceneIndexImpl(org.apache.geode.cache.lucene.internal.LuceneIndexImpl)

Example 2 with LuceneQueryProvider

use of org.apache.geode.cache.lucene.LuceneQueryProvider in project geode by apache.

the class LuceneQueryFunctionContextJUnitTest method testSerialization.

@Test
public void testSerialization() {
    LuceneServiceImpl.registerDataSerializables();
    LuceneQueryProvider provider = new StringQueryProvider("text", DEFAULT_FIELD);
    CollectorManager<TopEntriesCollector> manager = new TopEntriesCollectorManager("test");
    LuceneFunctionContext<TopEntriesCollector> context = new LuceneFunctionContext<>(provider, "testIndex", manager, 123);
    LuceneFunctionContext<TopEntriesCollector> copy = CopyHelper.deepCopy(context);
    assertEquals(123, copy.getLimit());
    assertNotNull(copy.getQueryProvider());
    assertEquals("text", ((StringQueryProvider) copy.getQueryProvider()).getQueryString());
    assertEquals(TopEntriesCollectorManager.class, copy.getCollectorManager().getClass());
    assertEquals("test", ((TopEntriesCollectorManager) copy.getCollectorManager()).getId());
    assertEquals("testIndex", copy.getIndexName());
}
Also used : LuceneQueryProvider(org.apache.geode.cache.lucene.LuceneQueryProvider) StringQueryProvider(org.apache.geode.cache.lucene.internal.StringQueryProvider) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

LuceneQueryProvider (org.apache.geode.cache.lucene.LuceneQueryProvider)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CacheClosedException (org.apache.geode.cache.CacheClosedException)1 Region (org.apache.geode.cache.Region)1 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)1 LuceneIndexNotFoundException (org.apache.geode.cache.lucene.LuceneIndexNotFoundException)1 LuceneIndexImpl (org.apache.geode.cache.lucene.internal.LuceneIndexImpl)1 LuceneIndexStats (org.apache.geode.cache.lucene.internal.LuceneIndexStats)1 StringQueryProvider (org.apache.geode.cache.lucene.internal.StringQueryProvider)1 IndexRepository (org.apache.geode.cache.lucene.internal.repository.IndexRepository)1 IndexResultCollector (org.apache.geode.cache.lucene.internal.repository.IndexResultCollector)1 RepositoryManager (org.apache.geode.cache.lucene.internal.repository.RepositoryManager)1 BucketNotFoundException (org.apache.geode.internal.cache.BucketNotFoundException)1 PrimaryBucketException (org.apache.geode.internal.cache.PrimaryBucketException)1 InternalFunctionInvocationTargetException (org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException)1 UnitTest (org.apache.geode.test.junit.categories.UnitTest)1 Query (org.apache.lucene.search.Query)1 Test (org.junit.Test)1