Search in sources :

Example 1 with IndexResultCollector

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);
}
Also used : LuceneIndexStats(org.apache.geode.cache.lucene.internal.LuceneIndexStats) StringQueryProvider(org.apache.geode.cache.lucene.internal.StringQueryProvider) InternalCache(org.apache.geode.internal.cache.InternalCache) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ResultSender(org.apache.geode.cache.execute.ResultSender) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) IndexResultCollector(org.apache.geode.cache.lucene.internal.repository.IndexResultCollector) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) InternalRegionFunctionContext(org.apache.geode.internal.cache.execute.InternalRegionFunctionContext) Region(org.apache.geode.cache.Region) RepositoryManager(org.apache.geode.cache.lucene.internal.repository.RepositoryManager) InternalLuceneService(org.apache.geode.cache.lucene.internal.InternalLuceneService) LuceneIndexImpl(org.apache.geode.cache.lucene.internal.LuceneIndexImpl) Before(org.junit.Before)

Example 2 with IndexResultCollector

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

Example 3 with IndexResultCollector

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);
    }
}
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 4 with IndexResultCollector

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;
}
Also used : IndexResultCollector(org.apache.geode.cache.lucene.internal.repository.IndexResultCollector) EntryScoreComparator(org.apache.geode.cache.lucene.internal.distributed.TopEntries.EntryScoreComparator) EntryScoreComparator(org.apache.geode.cache.lucene.internal.distributed.TopEntries.EntryScoreComparator) Comparator(java.util.Comparator)

Example 5 with IndexResultCollector

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));
}
Also used : IndexResultCollector(org.apache.geode.cache.lucene.internal.repository.IndexResultCollector) Collection(java.util.Collection) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

IndexResultCollector (org.apache.geode.cache.lucene.internal.repository.IndexResultCollector)6 UnitTest (org.apache.geode.test.junit.categories.UnitTest)3 Test (org.junit.Test)3 Region (org.apache.geode.cache.Region)2 LuceneIndexImpl (org.apache.geode.cache.lucene.internal.LuceneIndexImpl)2 LuceneIndexStats (org.apache.geode.cache.lucene.internal.LuceneIndexStats)2 IndexRepository (org.apache.geode.cache.lucene.internal.repository.IndexRepository)2 RepositoryManager (org.apache.geode.cache.lucene.internal.repository.RepositoryManager)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 CacheClosedException (org.apache.geode.cache.CacheClosedException)1 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)1 ResultSender (org.apache.geode.cache.execute.ResultSender)1 LuceneIndexNotFoundException (org.apache.geode.cache.lucene.LuceneIndexNotFoundException)1 LuceneQueryProvider (org.apache.geode.cache.lucene.LuceneQueryProvider)1 InternalLuceneService (org.apache.geode.cache.lucene.internal.InternalLuceneService)1 StringQueryProvider (org.apache.geode.cache.lucene.internal.StringQueryProvider)1 EntryScoreComparator (org.apache.geode.cache.lucene.internal.distributed.TopEntries.EntryScoreComparator)1