Search in sources :

Example 1 with LuceneIndexStats

use of org.apache.geode.cache.lucene.internal.LuceneIndexStats in project geode by apache.

the class LuceneIndexCommandsJUnitTest method testDescribeIndex.

@Test
public void testDescribeIndex() throws Exception {
    final InternalCache mockCache = mock(InternalCache.class, "InternalCache");
    final String serverName = "mockServer";
    final ResultCollector mockResultCollector = mock(ResultCollector.class, "ResultCollector");
    final LuceneIndexCommands commands = spy(createIndexCommands(mockCache, null));
    String[] searchableFields = { "field1", "field2", "field3" };
    Map<String, Analyzer> fieldAnalyzers = new HashMap<>();
    fieldAnalyzers.put("field1", new StandardAnalyzer());
    fieldAnalyzers.put("field2", new KeywordAnalyzer());
    fieldAnalyzers.put("field3", null);
    final LuceneIndexStats mockIndexStats = getMockIndexStats(1, 10, 5, 1);
    final List<LuceneIndexDetails> indexDetails = new ArrayList<>();
    indexDetails.add(createIndexDetails("memberFive", "/Employees", searchableFields, fieldAnalyzers, mockIndexStats, true, serverName));
    doReturn(mockResultCollector).when(commands).executeFunctionOnRegion(isA(LuceneDescribeIndexFunction.class), any(LuceneIndexInfo.class), eq(true));
    doReturn(indexDetails).when(mockResultCollector).getResult();
    CommandResult result = (CommandResult) commands.describeIndex("memberFive", "/Employees");
    TabularResultData data = (TabularResultData) result.getResultData();
    assertEquals(Collections.singletonList("memberFive"), data.retrieveAllValues("Index Name"));
    assertEquals(Collections.singletonList("/Employees"), data.retrieveAllValues("Region Path"));
    assertEquals(Collections.singletonList("[field1, field2, field3]"), data.retrieveAllValues("Indexed Fields"));
    assertEquals(Collections.singletonList("{field1=StandardAnalyzer, field2=KeywordAnalyzer}"), data.retrieveAllValues("Field Analyzer"));
    assertEquals(Collections.singletonList("Initialized"), data.retrieveAllValues("Status"));
    assertEquals(Collections.singletonList("1"), data.retrieveAllValues("Query Executions"));
    assertEquals(Collections.singletonList("10"), data.retrieveAllValues("Commits"));
    assertEquals(Collections.singletonList("5"), data.retrieveAllValues("Updates"));
    assertEquals(Collections.singletonList("1"), data.retrieveAllValues("Documents"));
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) LuceneIndexStats(org.apache.geode.cache.lucene.internal.LuceneIndexStats) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) Mockito.anyString(org.mockito.Mockito.anyString) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) LuceneDescribeIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneDescribeIndexFunction) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ResultCollector(org.apache.geode.cache.execute.ResultCollector) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 2 with LuceneIndexStats

use of org.apache.geode.cache.lucene.internal.LuceneIndexStats in project geode by apache.

the class LuceneIndexCommandsJUnitTest method getMockIndexStats.

private LuceneIndexStats getMockIndexStats(int queries, int commits, int updates, int docs) {
    LuceneIndexStats mockIndexStats = mock(LuceneIndexStats.class);
    when(mockIndexStats.getQueryExecutions()).thenReturn(queries);
    when(mockIndexStats.getCommits()).thenReturn(commits);
    when(mockIndexStats.getUpdates()).thenReturn(updates);
    when(mockIndexStats.getDocuments()).thenReturn(docs);
    return mockIndexStats;
}
Also used : LuceneIndexStats(org.apache.geode.cache.lucene.internal.LuceneIndexStats)

Example 3 with LuceneIndexStats

use of org.apache.geode.cache.lucene.internal.LuceneIndexStats in project geode by apache.

the class LuceneIndexMaintenanceIntegrationTest method statsAreUpdatedAfterACommit.

@Test
public void statsAreUpdatedAfterACommit() throws Exception {
    luceneService.createIndexFactory().setFields("title", "description").create(INDEX_NAME, REGION_NAME);
    Region region = createRegion(REGION_NAME, RegionShortcut.PARTITION);
    region.put("object-1", new TestObject("title 1", "hello world"));
    region.put("object-2", new TestObject("title 2", "this will not match"));
    region.put("object-3", new TestObject("title 3", "hello world"));
    region.put("object-4", new TestObject("hello world", "hello world"));
    LuceneIndexForPartitionedRegion index = (LuceneIndexForPartitionedRegion) luceneService.getIndex(INDEX_NAME, REGION_NAME);
    luceneService.waitUntilFlushed(INDEX_NAME, REGION_NAME, WAIT_FOR_FLUSH_TIME, TimeUnit.MILLISECONDS);
    FileSystemStats fileSystemStats = index.getFileSystemStats();
    LuceneIndexStats indexStats = index.getIndexStats();
    await(() -> assertEquals(4, indexStats.getDocuments()));
    await(() -> assertTrue(fileSystemStats.getBytes() > 0));
}
Also used : LuceneIndexStats(org.apache.geode.cache.lucene.internal.LuceneIndexStats) LuceneIndexForPartitionedRegion(org.apache.geode.cache.lucene.internal.LuceneIndexForPartitionedRegion) FileSystemStats(org.apache.geode.cache.lucene.internal.filesystem.FileSystemStats) LuceneIndexForPartitionedRegion(org.apache.geode.cache.lucene.internal.LuceneIndexForPartitionedRegion) Region(org.apache.geode.cache.Region) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 4 with LuceneIndexStats

use of org.apache.geode.cache.lucene.internal.LuceneIndexStats 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 5 with LuceneIndexStats

use of org.apache.geode.cache.lucene.internal.LuceneIndexStats in project geode by apache.

the class LuceneIndexCommandsJUnitTest method testListIndexWithStats.

@Test
public void testListIndexWithStats() {
    final InternalCache mockCache = mock(InternalCache.class, "InternalCache");
    final String serverName = "mockServer";
    final AbstractExecution mockFunctionExecutor = mock(AbstractExecution.class, "Function Executor");
    final ResultCollector mockResultCollector = mock(ResultCollector.class, "ResultCollector");
    final LuceneIndexStats mockIndexStats1 = getMockIndexStats(1, 10, 5, 1);
    final LuceneIndexStats mockIndexStats2 = getMockIndexStats(2, 20, 10, 2);
    final LuceneIndexStats mockIndexStats3 = getMockIndexStats(3, 30, 15, 3);
    String[] searchableFields = { "field1", "field2", "field3" };
    Map<String, Analyzer> fieldAnalyzers = new HashMap<>();
    fieldAnalyzers.put("field1", new StandardAnalyzer());
    fieldAnalyzers.put("field2", new KeywordAnalyzer());
    fieldAnalyzers.put("field3", null);
    final LuceneIndexDetails indexDetails1 = createIndexDetails("memberFive", "/Employees", searchableFields, fieldAnalyzers, mockIndexStats1, true, serverName);
    final LuceneIndexDetails indexDetails2 = createIndexDetails("memberSix", "/Employees", searchableFields, fieldAnalyzers, mockIndexStats2, true, serverName);
    final LuceneIndexDetails indexDetails3 = createIndexDetails("memberTen", "/Employees", searchableFields, fieldAnalyzers, mockIndexStats3, true, serverName);
    final List<Set<LuceneIndexDetails>> results = new ArrayList<>();
    results.add(CollectionUtils.asSet(indexDetails2, indexDetails1, indexDetails3));
    when(mockFunctionExecutor.execute(isA(LuceneListIndexFunction.class))).thenReturn(mockResultCollector);
    when(mockResultCollector.getResult()).thenReturn(results);
    final LuceneIndexCommands commands = createIndexCommands(mockCache, mockFunctionExecutor);
    CommandResult result = (CommandResult) commands.listIndex(true);
    TabularResultData data = (TabularResultData) result.getResultData();
    assertEquals(Arrays.asList("memberFive", "memberSix", "memberTen"), data.retrieveAllValues("Index Name"));
    assertEquals(Arrays.asList("/Employees", "/Employees", "/Employees"), data.retrieveAllValues("Region Path"));
    assertEquals(Arrays.asList("[field1, field2, field3]", "[field1, field2, field3]", "[field1, field2, field3]"), data.retrieveAllValues("Indexed Fields"));
    assertEquals(Arrays.asList("{field1=StandardAnalyzer, field2=KeywordAnalyzer}", "{field1=StandardAnalyzer, field2=KeywordAnalyzer}", "{field1=StandardAnalyzer, field2=KeywordAnalyzer}"), data.retrieveAllValues("Field Analyzer"));
    assertEquals(Arrays.asList("1", "2", "3"), data.retrieveAllValues("Query Executions"));
    assertEquals(Arrays.asList("10", "20", "30"), data.retrieveAllValues("Commits"));
    assertEquals(Arrays.asList("5", "10", "15"), data.retrieveAllValues("Updates"));
    assertEquals(Arrays.asList("1", "2", "3"), data.retrieveAllValues("Documents"));
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) AbstractExecution(org.apache.geode.internal.cache.execute.AbstractExecution) LuceneIndexStats(org.apache.geode.cache.lucene.internal.LuceneIndexStats) Set(java.util.Set) HashSet(java.util.HashSet) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) HashMap(java.util.HashMap) LuceneListIndexFunction(org.apache.geode.cache.lucene.internal.cli.functions.LuceneListIndexFunction) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) Mockito.anyString(org.mockito.Mockito.anyString) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ResultCollector(org.apache.geode.cache.execute.ResultCollector) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

LuceneIndexStats (org.apache.geode.cache.lucene.internal.LuceneIndexStats)5 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Region (org.apache.geode.cache.Region)2 ResultCollector (org.apache.geode.cache.execute.ResultCollector)2 InternalCache (org.apache.geode.internal.cache.InternalCache)2 CommandResult (org.apache.geode.management.internal.cli.result.CommandResult)2 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)2 UnitTest (org.apache.geode.test.junit.categories.UnitTest)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Mockito.anyString (org.mockito.Mockito.anyString)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CacheClosedException (org.apache.geode.cache.CacheClosedException)1 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)1 LuceneIndexNotFoundException (org.apache.geode.cache.lucene.LuceneIndexNotFoundException)1