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);
}
}
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());
}
Aggregations