Search in sources :

Example 1 with LuceneService

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

the class LuceneSearchIndexFunction method execute.

public void execute(final FunctionContext context) {
    Set<LuceneSearchResults> result = new HashSet<>();
    final Cache cache = getCache();
    final LuceneQueryInfo queryInfo = (LuceneQueryInfo) context.getArguments();
    LuceneService luceneService = LuceneServiceProvider.get(getCache());
    try {
        if (luceneService.getIndex(queryInfo.getIndexName(), queryInfo.getRegionPath()) == null) {
            throw new Exception("Index " + queryInfo.getIndexName() + " not found on region " + queryInfo.getRegionPath());
        }
        final LuceneQuery<K, V> query = luceneService.createLuceneQueryFactory().setLimit(queryInfo.getLimit()).create(queryInfo.getIndexName(), queryInfo.getRegionPath(), queryInfo.getQueryString(), queryInfo.getDefaultField());
        if (queryInfo.getKeysOnly()) {
            query.findKeys().forEach(key -> result.add(new LuceneSearchResults(key.toString())));
        } else {
            PageableLuceneQueryResults pageableLuceneQueryResults = query.findPages();
            while (pageableLuceneQueryResults.hasNext()) {
                List<LuceneResultStruct> page = pageableLuceneQueryResults.next();
                page.stream().forEach(searchResult -> result.add(new LuceneSearchResults<K, V>(searchResult.getKey().toString(), searchResult.getValue().toString(), searchResult.getScore())));
            }
        }
        if (result != null) {
            context.getResultSender().lastResult(result);
        }
    } catch (LuceneQueryException e) {
        result.add(new LuceneSearchResults(true, e.getRootCause().getMessage()));
        context.getResultSender().lastResult(result);
    } catch (Exception e) {
        result.add(new LuceneSearchResults(true, e.getMessage()));
        context.getResultSender().lastResult(result);
    }
}
Also used : LuceneSearchResults(org.apache.geode.cache.lucene.internal.cli.LuceneSearchResults) LuceneQueryException(org.apache.geode.cache.lucene.LuceneQueryException) LuceneQueryException(org.apache.geode.cache.lucene.LuceneQueryException) LuceneQueryInfo(org.apache.geode.cache.lucene.internal.cli.LuceneQueryInfo) LuceneService(org.apache.geode.cache.lucene.LuceneService) LuceneResultStruct(org.apache.geode.cache.lucene.LuceneResultStruct) PageableLuceneQueryResults(org.apache.geode.cache.lucene.PageableLuceneQueryResults) HashSet(java.util.HashSet) Cache(org.apache.geode.cache.Cache)

Example 2 with LuceneService

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

the class LuceneIndexCommandsDUnitTest method createIndexWithAnalyzersShouldCreateANewIndex.

@Test
public void createIndexWithAnalyzersShouldCreateANewIndex() 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, String.join(",", analyzerNames));
    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 3 with LuceneService

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

the class LuceneClusterConfigurationDUnitTest method indexWithAnalyzerGetsCreatedUsingClusterConfiguration.

@Test
public void indexWithAnalyzerGetsCreatedUsingClusterConfiguration() throws Exception {
    startNodeUsingClusterConfiguration(1);
    // Connect Gfsh to locator.
    gfshConnector.connectAndVerify(locator);
    // Create lucene index.
    // createLuceneIndexUsingGfsh();
    createLuceneIndexWithAnalyzerUsingGfsh(false);
    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);
        String[] fields = new String[] { "field1", "field2", "field3" };
        validateIndexFields(fields, index);
        // Add this check back when we complete xml generation for analyzer.
        validateIndexFieldAnalyzer(fields, new String[] { "org.apache.lucene.analysis.standard.StandardAnalyzer", "org.apache.lucene.analysis.standard.StandardAnalyzer", "org.apache.lucene.analysis.standard.StandardAnalyzer" }, index);
    });
}
Also used : LuceneIndex(org.apache.geode.cache.lucene.LuceneIndex) MemberVM(org.apache.geode.test.dunit.rules.MemberVM) LuceneService(org.apache.geode.cache.lucene.LuceneService) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 4 with LuceneService

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

the class LuceneManagementDUnitTest method queryEntries.

private void queryEntries(String regionName, String indexName) throws LuceneQueryException {
    LuceneService service = LuceneServiceProvider.get(getCache());
    LuceneQuery query = service.createLuceneQueryFactory().create(indexName, regionName, "field0:0", null);
    query.findValues();
}
Also used : LuceneQuery(org.apache.geode.cache.lucene.LuceneQuery) LuceneService(org.apache.geode.cache.lucene.LuceneService)

Example 5 with LuceneService

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

the class LuceneServiceImplIntegrationTest method luceneServiceProviderGetShouldAcceptClientCacheAsAParameter.

@Test
public void luceneServiceProviderGetShouldAcceptClientCacheAsAParameter() {
    clientCache = getClientCache();
    LuceneService luceneService = LuceneServiceProvider.get(clientCache);
    assertNotNull(luceneService);
}
Also used : LuceneService(org.apache.geode.cache.lucene.LuceneService) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

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