use of org.apache.geode.cache.lucene.LuceneService in project geode by apache.
the class LuceneCreateIndexFunction method execute.
public void execute(final FunctionContext context) {
String memberId = null;
try {
final LuceneIndexInfo indexInfo = (LuceneIndexInfo) context.getArguments();
final Cache cache = getCache();
memberId = cache.getDistributedSystem().getDistributedMember().getId();
LuceneService service = LuceneServiceProvider.get(cache);
INDEX_NAME.validateName(indexInfo.getIndexName());
String[] fields = indexInfo.getSearchableFieldNames();
String[] analyzerName = indexInfo.getFieldAnalyzers();
final LuceneIndexFactory indexFactory = service.createIndexFactory();
if (analyzerName == null || analyzerName.length == 0) {
for (String field : fields) {
indexFactory.addField(field);
}
} else {
if (analyzerName.length != fields.length)
throw new Exception("Mismatch in lengths of fields and analyzers");
for (int i = 0; i < fields.length; i++) {
Analyzer analyzer = toAnalyzer(analyzerName[i]);
indexFactory.addField(fields[i], analyzer);
}
}
REGION_PATH.validateName(indexInfo.getRegionPath());
indexFactory.create(indexInfo.getIndexName(), indexInfo.getRegionPath());
// TODO - update cluster configuration by returning a valid XmlEntity
XmlEntity xmlEntity = null;
context.getResultSender().lastResult(new CliFunctionResult(memberId, xmlEntity));
} catch (Exception e) {
String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass().getName(), e.getMessage());
context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
}
}
use of org.apache.geode.cache.lucene.LuceneService 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.LuceneService in project geode by apache.
the class LuceneIndexCommandsDUnitTest method createIndexWithoutRegion.
private void createIndexWithoutRegion(final VM vm1) {
vm1.invoke(() -> {
LuceneService luceneService = LuceneServiceProvider.get(getCache());
Map<String, Analyzer> fieldAnalyzers = new HashMap();
fieldAnalyzers.put("field1", new StandardAnalyzer());
fieldAnalyzers.put("field2", new KeywordAnalyzer());
fieldAnalyzers.put("field3", null);
luceneService.createIndexFactory().setFields(fieldAnalyzers).create(INDEX_NAME, REGION_NAME);
});
}
use of org.apache.geode.cache.lucene.LuceneService 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());
});
}
use of org.apache.geode.cache.lucene.LuceneService in project geode by apache.
the class LuceneIndexCommandsDUnitTest method queryAndVerify.
private void queryAndVerify(VM vm1, String queryString, String defaultField, List<String> expectedKeys) {
vm1.invoke(() -> {
LuceneService luceneService = LuceneServiceProvider.get(getCache());
final LuceneQuery<String, TestObject> query = luceneService.createLuceneQueryFactory().create(INDEX_NAME, REGION_NAME, queryString, defaultField);
assertEquals(Collections.singletonList("A"), query.findKeys());
});
}
Aggregations