Search in sources :

Example 11 with LuceneIndex

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

the class LuceneIndexXmlGeneratorIntegrationJUnitTest method generateWithFields.

/**
   * Test of generating and reading cache configuration back in.
   */
@Test
public void generateWithFields() {
    cache = new CacheFactory().set(MCAST_PORT, "0").create();
    LuceneService service = LuceneServiceProvider.get(cache);
    service.createIndexFactory().setFields("a", "b", "c").create("index", "region");
    cache.createRegionFactory(RegionShortcut.PARTITION).create("region");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintWriter pw = new PrintWriter(baos);
    CacheXmlGenerator.generate(cache, pw, true, false, false);
    pw.flush();
    cache.close();
    cache = new CacheFactory().set(MCAST_PORT, "0").create();
    byte[] bytes = baos.toByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    System.out.println("---FILE---");
    System.out.println(new String(bytes, Charset.defaultCharset()));
    cache.loadCacheXml(new ByteArrayInputStream(bytes));
    LuceneService service2 = LuceneServiceProvider.get(cache);
    assertTrue(service != service2);
    LuceneIndex index = service2.getIndex("index", "region");
    assertNotNull(index);
    assertArrayEquals(new String[] { "a", "b", "c" }, index.getFieldNames());
}
Also used : LuceneIndex(org.apache.geode.cache.lucene.LuceneIndex) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheFactory(org.apache.geode.cache.CacheFactory) LuceneService(org.apache.geode.cache.lucene.LuceneService) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 12 with LuceneIndex

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

the class LuceneListIndexFunctionJUnitTest method testExecute.

@Test
@SuppressWarnings("unchecked")
public void testExecute() throws Throwable {
    GemFireCacheImpl cache = Fakes.cache();
    final String serverName = "mockServer";
    LuceneServiceImpl service = mock(LuceneServiceImpl.class);
    when(cache.getService(InternalLuceneService.class)).thenReturn(service);
    FunctionContext context = mock(FunctionContext.class);
    ResultSender resultSender = mock(ResultSender.class);
    when(context.getResultSender()).thenReturn(resultSender);
    LuceneIndexImpl index1 = getMockLuceneIndex("index1");
    LuceneIndexImpl index2 = getMockLuceneIndex("index2");
    TreeSet expectedResult = new TreeSet();
    expectedResult.add(new LuceneIndexDetails(index1, serverName));
    expectedResult.add(new LuceneIndexDetails(index2, serverName));
    ArrayList<LuceneIndex> allIndexes = new ArrayList();
    allIndexes.add(index1);
    allIndexes.add(index2);
    when(service.getAllIndexes()).thenReturn(allIndexes);
    LuceneListIndexFunction function = new LuceneListIndexFunction();
    function = spy(function);
    Mockito.doReturn(cache).when(function).getCache();
    function.execute(context);
    ArgumentCaptor<Set> resultCaptor = ArgumentCaptor.forClass(Set.class);
    verify(resultSender).lastResult(resultCaptor.capture());
    Set<String> result = resultCaptor.getValue();
    assertEquals(2, result.size());
    assertEquals(expectedResult, result);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext) ResultSender(org.apache.geode.cache.execute.ResultSender) LuceneIndex(org.apache.geode.cache.lucene.LuceneIndex) TreeSet(java.util.TreeSet) LuceneIndexDetails(org.apache.geode.cache.lucene.internal.cli.LuceneIndexDetails) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) LuceneServiceImpl(org.apache.geode.cache.lucene.internal.LuceneServiceImpl) LuceneIndexImpl(org.apache.geode.cache.lucene.internal.LuceneIndexImpl) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 13 with LuceneIndex

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

the class LuceneClusterConfigurationDUnitTest method indexGetsCreatedUsingClusterConfiguration.

@Test
public void indexGetsCreatedUsingClusterConfiguration() throws Exception {
    Member vm1 = startNodeUsingClusterConfiguration(1);
    // Connect Gfsh to locator.
    gfshConnector.connectAndVerify(locator);
    // Create lucene index.
    createLuceneIndexUsingGfsh();
    createRegionUsingGfsh(REGION_NAME, RegionShortcut.PARTITION, null);
    // Start vm2. This should have lucene index created using cluster
    // configuration.
    MemberVM vm2 = startNodeUsingClusterConfiguration(2);
    vm2.invoke(() -> {
        LuceneService luceneService = LuceneServiceProvider.get(LocatorServerStartupRule.serverStarter.getCache());
        final LuceneIndex index = luceneService.getIndex(INDEX_NAME, REGION_NAME);
        assertNotNull(index);
        validateIndexFields(new String[] { "field1", "field2", "field3" }, index);
    });
}
Also used : LuceneIndex(org.apache.geode.cache.lucene.LuceneIndex) MemberVM(org.apache.geode.test.dunit.rules.MemberVM) Member(org.apache.geode.test.dunit.rules.Member) LuceneService(org.apache.geode.cache.lucene.LuceneService) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 14 with LuceneIndex

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

the class LuceneIndexCommandsDUnitTest method createIndexShouldTrimAnalyzerNames.

@Test
public void createIndexShouldTrimAnalyzerNames() throws Exception {
    final VM vm1 = Host.getHost(0).getVM(-1);
    vm1.invoke(() -> {
        getCache();
    });
    List<String> analyzerNames = new ArrayList<>();
    analyzerNames.add(StandardAnalyzer.class.getCanonicalName());
    analyzerNames.add(KeywordAnalyzer.class.getCanonicalName());
    analyzerNames.add(StandardAnalyzer.class.getCanonicalName());
    CommandStringBuilder csb = new CommandStringBuilder(LuceneCliStrings.LUCENE_CREATE_INDEX);
    csb.addOption(LuceneCliStrings.LUCENE__INDEX_NAME, INDEX_NAME);
    csb.addOption(LuceneCliStrings.LUCENE__REGION_PATH, REGION_NAME);
    csb.addOption(LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD, "field1,field2,field3");
    csb.addOption(LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER, "\"org.apache.lucene.analysis.standard.StandardAnalyzer, org.apache.lucene.analysis.core.KeywordAnalyzer, org.apache.lucene.analysis.standard.StandardAnalyzer\"");
    String resultAsString = executeCommandAndLogResult(csb);
    vm1.invoke(() -> {
        LuceneService luceneService = LuceneServiceProvider.get(getCache());
        createRegion();
        final LuceneIndex index = luceneService.getIndex(INDEX_NAME, REGION_NAME);
        final Map<String, Analyzer> fieldAnalyzers = index.getFieldAnalyzers();
        assertEquals(StandardAnalyzer.class, fieldAnalyzers.get("field1").getClass());
        assertEquals(KeywordAnalyzer.class, fieldAnalyzers.get("field2").getClass());
        assertEquals(StandardAnalyzer.class, fieldAnalyzers.get("field3").getClass());
    });
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) LuceneIndex(org.apache.geode.cache.lucene.LuceneIndex) CommandStringBuilder(org.apache.geode.management.internal.cli.util.CommandStringBuilder) VM(org.apache.geode.test.dunit.VM) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ArrayList(java.util.ArrayList) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) LuceneService(org.apache.geode.cache.lucene.LuceneService) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 15 with LuceneIndex

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

the class LuceneIndexCommandsDUnitTest method createIndexWithWhitespaceOrDefaultKeywordAnalyzerShouldUseStandardAnalyzer.

@Test
public void createIndexWithWhitespaceOrDefaultKeywordAnalyzerShouldUseStandardAnalyzer() throws Exception {
    final VM vm1 = Host.getHost(0).getVM(-1);
    vm1.invoke(() -> {
        getCache();
    });
    // Test whitespace analyzer name
    String analyzerList = StandardAnalyzer.class.getCanonicalName() + ",     ," + KeywordAnalyzer.class.getCanonicalName();
    CommandStringBuilder csb = new CommandStringBuilder(LuceneCliStrings.LUCENE_CREATE_INDEX);
    csb.addOption(LuceneCliStrings.LUCENE__INDEX_NAME, "space");
    csb.addOption(LuceneCliStrings.LUCENE__REGION_PATH, REGION_NAME);
    csb.addOption(LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD, "field1,field2,field3");
    csb.addOption(LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER, "'" + analyzerList + "'");
    String resultAsString = executeCommandAndLogResult(csb);
    // Test empty analyzer name
    analyzerList = StandardAnalyzer.class.getCanonicalName() + ",," + KeywordAnalyzer.class.getCanonicalName();
    csb = new CommandStringBuilder(LuceneCliStrings.LUCENE_CREATE_INDEX);
    csb.addOption(LuceneCliStrings.LUCENE__INDEX_NAME, "empty");
    csb.addOption(LuceneCliStrings.LUCENE__REGION_PATH, REGION_NAME);
    csb.addOption(LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD, "field1,field2,field3");
    csb.addOption(LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER, analyzerList);
    resultAsString = executeCommandAndLogResult(csb);
    // Test keyword analyzer name
    analyzerList = StandardAnalyzer.class.getCanonicalName() + ",DEFAULT," + KeywordAnalyzer.class.getCanonicalName();
    csb = new CommandStringBuilder(LuceneCliStrings.LUCENE_CREATE_INDEX);
    csb.addOption(LuceneCliStrings.LUCENE__INDEX_NAME, "keyword");
    csb.addOption(LuceneCliStrings.LUCENE__REGION_PATH, REGION_NAME);
    csb.addOption(LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD, "field1,field2,field3");
    csb.addOption(LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER, analyzerList);
    resultAsString = executeCommandAndLogResult(csb);
    vm1.invoke(() -> {
        LuceneService luceneService = LuceneServiceProvider.get(getCache());
        createRegion();
        final LuceneIndex spaceIndex = luceneService.getIndex("space", REGION_NAME);
        final Map<String, Analyzer> spaceFieldAnalyzers = spaceIndex.getFieldAnalyzers();
        final LuceneIndex emptyIndex = luceneService.getIndex("empty", REGION_NAME);
        final Map<String, Analyzer> emptyFieldAnalyzers2 = emptyIndex.getFieldAnalyzers();
        final LuceneIndex keywordIndex = luceneService.getIndex("keyword", REGION_NAME);
        final Map<String, Analyzer> keywordFieldAnalyzers = keywordIndex.getFieldAnalyzers();
        // Test whitespace analyzers
        assertEquals(StandardAnalyzer.class.getCanonicalName(), spaceFieldAnalyzers.get("field1").getClass().getCanonicalName());
        assertEquals(StandardAnalyzer.class.getCanonicalName(), spaceFieldAnalyzers.get("field2").getClass().getCanonicalName());
        assertEquals(KeywordAnalyzer.class.getCanonicalName(), spaceFieldAnalyzers.get("field3").getClass().getCanonicalName());
        // Test empty analyzers
        assertEquals(StandardAnalyzer.class.getCanonicalName(), emptyFieldAnalyzers2.get("field1").getClass().getCanonicalName());
        assertEquals(StandardAnalyzer.class.getCanonicalName(), emptyFieldAnalyzers2.get("field2").getClass().getCanonicalName());
        assertEquals(KeywordAnalyzer.class.getCanonicalName(), emptyFieldAnalyzers2.get("field3").getClass().getCanonicalName());
        // Test keyword analyzers
        assertEquals(StandardAnalyzer.class.getCanonicalName(), keywordFieldAnalyzers.get("field1").getClass().getCanonicalName());
        assertEquals(StandardAnalyzer.class.getCanonicalName(), keywordFieldAnalyzers.get("field2").getClass().getCanonicalName());
        assertEquals(KeywordAnalyzer.class.getCanonicalName(), keywordFieldAnalyzers.get("field3").getClass().getCanonicalName());
    });
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) LuceneIndex(org.apache.geode.cache.lucene.LuceneIndex) CommandStringBuilder(org.apache.geode.management.internal.cli.util.CommandStringBuilder) VM(org.apache.geode.test.dunit.VM) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) LuceneService(org.apache.geode.cache.lucene.LuceneService) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

LuceneIndex (org.apache.geode.cache.lucene.LuceneIndex)16 LuceneService (org.apache.geode.cache.lucene.LuceneService)10 Test (org.junit.Test)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 LuceneIndexImpl (org.apache.geode.cache.lucene.internal.LuceneIndexImpl)4 CommandStringBuilder (org.apache.geode.management.internal.cli.util.CommandStringBuilder)4 VM (org.apache.geode.test.dunit.VM)4 ArrayList (java.util.ArrayList)3 Cache (org.apache.geode.cache.Cache)3 LuceneServiceImpl (org.apache.geode.cache.lucene.internal.LuceneServiceImpl)3 LuceneIndexDetails (org.apache.geode.cache.lucene.internal.cli.LuceneIndexDetails)3 Analyzer (org.apache.lucene.analysis.Analyzer)3 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)3 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)3 HashSet (java.util.HashSet)2 CacheFactory (org.apache.geode.cache.CacheFactory)2 LuceneIndexCreationProfile (org.apache.geode.cache.lucene.internal.LuceneIndexCreationProfile)2 MemberVM (org.apache.geode.test.dunit.rules.MemberVM)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 UnitTest (org.apache.geode.test.junit.categories.UnitTest)2