Search in sources :

Example 16 with LuceneService

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()));
    }
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) LuceneIndexInfo(org.apache.geode.cache.lucene.internal.cli.LuceneIndexInfo) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) LuceneIndexFactory(org.apache.geode.cache.lucene.LuceneIndexFactory) Cache(org.apache.geode.cache.Cache) LuceneService(org.apache.geode.cache.lucene.LuceneService)

Example 17 with LuceneService

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());
    });
}
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 18 with LuceneService

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);
    });
}
Also used : KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) HashMap(java.util.HashMap) 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)

Example 19 with LuceneService

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

Example 20 with LuceneService

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());
    });
}
Also used : LuceneService(org.apache.geode.cache.lucene.LuceneService)

Aggregations

LuceneService (org.apache.geode.cache.lucene.LuceneService)23 LuceneIndex (org.apache.geode.cache.lucene.LuceneIndex)10 Test (org.junit.Test)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 Analyzer (org.apache.lucene.analysis.Analyzer)6 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)6 Cache (org.apache.geode.cache.Cache)5 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)5 CommandStringBuilder (org.apache.geode.management.internal.cli.util.CommandStringBuilder)4 VM (org.apache.geode.test.dunit.VM)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)4 LuceneIndexImpl (org.apache.geode.cache.lucene.internal.LuceneIndexImpl)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CacheFactory (org.apache.geode.cache.CacheFactory)2 Region (org.apache.geode.cache.Region)2 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)2 LuceneServiceImpl (org.apache.geode.cache.lucene.internal.LuceneServiceImpl)2 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)2 MemberVM (org.apache.geode.test.dunit.rules.MemberVM)2