use of org.apache.geode.cache.lucene.internal.repository.IndexResultCollector in project geode by apache.
the class LuceneQueryFunctionJUnitTest method createMocksAndCommonObjects.
@Before
public void createMocksAndCommonObjects() throws Exception {
mockContext = mock(InternalRegionFunctionContext.class);
mockResultSender = mock(ResultSender.class);
mockRegion = mock(Region.class);
mockRepoManager = mock(RepositoryManager.class);
mockRepository1 = mock(IndexRepository.class, "repo1");
mockRepository2 = mock(IndexRepository.class, "repo2");
mockCollector = mock(IndexResultCollector.class);
mockStats = mock(LuceneIndexStats.class);
repos = new ArrayList<IndexRepository>();
repos.add(mockRepository1);
repos.add(mockRepository2);
mockIndex = mock(LuceneIndexImpl.class);
mockService = mock(InternalLuceneService.class);
mockCache = mock(InternalCache.class);
Analyzer analyzer = new StandardAnalyzer();
Mockito.doReturn(analyzer).when(mockIndex).getAnalyzer();
queryProvider = new StringQueryProvider("gemfire:lucene", DEFAULT_FIELD);
searchArgs = new LuceneFunctionContext<IndexResultCollector>(queryProvider, "indexName");
when(mockRegion.getCache()).thenReturn(mockCache);
when(mockRegion.getFullPath()).thenReturn(regionPath);
when(mockCache.getService(any())).thenReturn(mockService);
when(mockService.getIndex(eq("indexName"), eq(regionPath))).thenReturn(mockIndex);
when(mockIndex.getRepositoryManager()).thenReturn(mockRepoManager);
when(mockIndex.getFieldNames()).thenReturn(new String[] { "gemfire" });
when(mockIndex.getIndexStats()).thenReturn(mockStats);
query = queryProvider.getQuery(mockIndex);
}
use of org.apache.geode.cache.lucene.internal.repository.IndexResultCollector in project geode by apache.
the class LuceneQueryFunctionJUnitTest method testRepoQueryAndMerge.
@Test
public void testRepoQueryAndMerge() throws Exception {
when(mockContext.getDataSet()).thenReturn(mockRegion);
when(mockContext.getArguments()).thenReturn(searchArgs);
when(mockContext.<TopEntriesCollector>getResultSender()).thenReturn(mockResultSender);
when(mockRepoManager.getRepositories(eq(mockContext))).thenReturn(repos);
doAnswer(invocation -> {
IndexResultCollector collector = invocation.getArgumentAt(2, IndexResultCollector.class);
collector.collect(r1_1.getKey(), r1_1.getScore());
collector.collect(r1_2.getKey(), r1_2.getScore());
collector.collect(r1_3.getKey(), r1_3.getScore());
return null;
}).when(mockRepository1).query(eq(query), eq(LuceneQueryFactory.DEFAULT_LIMIT), any(IndexResultCollector.class));
doAnswer(invocation -> {
IndexResultCollector collector = invocation.getArgumentAt(2, IndexResultCollector.class);
collector.collect(r2_1.getKey(), r2_1.getScore());
collector.collect(r2_2.getKey(), r2_2.getScore());
return null;
}).when(mockRepository2).query(eq(query), eq(LuceneQueryFactory.DEFAULT_LIMIT), any(IndexResultCollector.class));
LuceneQueryFunction function = new LuceneQueryFunction();
function.execute(mockContext);
ArgumentCaptor<TopEntriesCollector> resultCaptor = ArgumentCaptor.forClass(TopEntriesCollector.class);
verify(mockResultSender).lastResult(resultCaptor.capture());
TopEntriesCollector result = resultCaptor.getValue();
List<EntryScore> hits = result.getEntries().getHits();
assertEquals(5, hits.size());
LuceneTestUtilities.verifyResultOrder(result.getEntries().getHits(), r1_1, r2_1, r1_2, r2_2, r1_3);
}
use of org.apache.geode.cache.lucene.internal.repository.IndexResultCollector 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.internal.repository.IndexResultCollector in project geode by apache.
the class TopEntriesCollectorManager method reduce.
@Override
public TopEntriesCollector reduce(Collection<TopEntriesCollector> collectors) {
TopEntriesCollector mergedResult = new TopEntriesCollector(id, limit);
if (collectors.isEmpty()) {
return mergedResult;
}
final EntryScoreComparator scoreComparator = new TopEntries().new EntryScoreComparator();
// orders a entry with higher score above a doc with lower score
Comparator<ListScanner> entryListComparator = new Comparator<ListScanner>() {
@Override
public int compare(ListScanner l1, ListScanner l2) {
EntryScore o1 = l1.peek();
EntryScore o2 = l2.peek();
return scoreComparator.compare(o1, o2);
}
};
// The queue contains iterators for all bucket results. The queue puts the entry with the
// highest score at the head
// using score comparator.
PriorityQueue<ListScanner> entryListsPriorityQueue;
entryListsPriorityQueue = new PriorityQueue<ListScanner>(collectors.size(), Collections.reverseOrder(entryListComparator));
for (IndexResultCollector collector : collectors) {
logger.debug("Number of entries found in collector {} is {}", collector.getName(), collector.size());
if (collector.size() > 0) {
entryListsPriorityQueue.add(new ListScanner(((TopEntriesCollector) collector).getEntries().getHits()));
}
}
logger.debug("Only {} count of entries will be reduced. Other entries will be ignored", limit);
while (entryListsPriorityQueue.size() > 0 && limit > mergedResult.size()) {
ListScanner scanner = entryListsPriorityQueue.remove();
EntryScore entry = scanner.next();
mergedResult.collect(entry);
if (scanner.hasNext()) {
entryListsPriorityQueue.add(scanner);
}
}
logger.debug("Reduced size of {} is {}", mergedResult.getName(), mergedResult.size());
return mergedResult;
}
use of org.apache.geode.cache.lucene.internal.repository.IndexResultCollector in project geode by apache.
the class LuceneQueryFunctionJUnitTest method injectCustomCollectorManager.
@Test
public void injectCustomCollectorManager() throws Exception {
final CollectorManager mockManager = mock(CollectorManager.class);
searchArgs = new LuceneFunctionContext<IndexResultCollector>(queryProvider, "indexName", mockManager);
when(mockContext.getDataSet()).thenReturn(mockRegion);
when(mockContext.getArguments()).thenReturn(searchArgs);
when(mockContext.<TopEntriesCollector>getResultSender()).thenReturn(mockResultSender);
repos.remove(0);
when(mockRepoManager.getRepositories(eq(mockContext))).thenReturn(repos);
when(mockManager.newCollector(eq("repo2"))).thenReturn(mockCollector);
when(mockManager.reduce(any(Collection.class))).thenAnswer(invocation -> {
Collection<IndexResultCollector> collectors = invocation.getArgumentAt(0, Collection.class);
assertEquals(1, collectors.size());
assertEquals(mockCollector, collectors.iterator().next());
return new TopEntriesCollector(null);
});
doAnswer(invocation -> {
IndexResultCollector collector = invocation.getArgumentAt(2, IndexResultCollector.class);
collector.collect(r2_1.getKey(), r2_1.getScore());
return null;
}).when(mockRepository2).query(eq(query), eq(LuceneQueryFactory.DEFAULT_LIMIT), any(IndexResultCollector.class));
LuceneQueryFunction function = new LuceneQueryFunction();
function.execute(mockContext);
verify(mockCollector).collect(eq("key-2-1"), eq(.45f));
verify(mockResultSender).lastResult(any(TopEntriesCollector.class));
}
Aggregations