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