Search in sources :

Example 61 with Analyzer

use of org.apache.lucene.analysis.Analyzer in project geode by apache.

the class LuceneIndexCommandsDUnitTest method createIndex.

private void createIndex(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);
        createRegion();
    });
}
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 62 with Analyzer

use of org.apache.lucene.analysis.Analyzer in project geode by apache.

the class LuceneIndexXmlParserIntegrationJUnitTest method parseIndexWithAnalyzers.

@Test
public void parseIndexWithAnalyzers() throws FileNotFoundException {
    RegionCreation region = createRegionCreation("region");
    // Validate expected indexes
    Map<String, String[]> expectedIndexes = new HashMap<String, String[]>();
    expectedIndexes.put("index", new String[] { "a", "b", "c" });
    validateExpectedIndexes(region, expectedIndexes);
    // Validate expected analyzers
    Map<String, Map<String, Class<? extends Analyzer>>> expectedIndexAnalyzers = new HashMap<>();
    Map<String, Class<? extends Analyzer>> expectedFieldAnalyzers = new HashMap<>();
    expectedFieldAnalyzers.put("a", KeywordAnalyzer.class);
    expectedFieldAnalyzers.put("b", SimpleAnalyzer.class);
    expectedFieldAnalyzers.put("c", ClassicAnalyzer.class);
    expectedIndexAnalyzers.put("index", expectedFieldAnalyzers);
    validateExpectedAnalyzers(region, expectedIndexAnalyzers);
}
Also used : HashMap(java.util.HashMap) ClassicAnalyzer(org.apache.lucene.analysis.standard.ClassicAnalyzer) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) SimpleAnalyzer(org.apache.lucene.analysis.core.SimpleAnalyzer) RegionCreation(org.apache.geode.internal.cache.xmlcache.RegionCreation) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 63 with Analyzer

use of org.apache.lucene.analysis.Analyzer in project jackrabbit by apache.

the class JackrabbitAnalyzer method getAnalyzerInstance.

/**
     * Returns a new instance of the named Lucene {@link Analyzer} class,
     * or the default analyzer if the given class can not be instantiated.
     *
     * @param className name of the analyzer class
     * @return new analyzer instance, or the default analyzer
     */
static Analyzer getAnalyzerInstance(String className) {
    Class<?> analyzerClass;
    try {
        analyzerClass = Class.forName(className);
    } catch (ClassNotFoundException e) {
        log.warn(className + " could not be found", e);
        return DEFAULT_ANALYZER;
    }
    if (!Analyzer.class.isAssignableFrom(analyzerClass)) {
        log.warn(className + " is not a Lucene Analyzer");
        return DEFAULT_ANALYZER;
    } else if (JackrabbitAnalyzer.class.isAssignableFrom(analyzerClass)) {
        log.warn(className + " can not be used as a JackrabbitAnalyzer component");
        return DEFAULT_ANALYZER;
    }
    Exception cause = null;
    Constructor<?>[] constructors = analyzerClass.getConstructors();
    for (Constructor<?> constructor : constructors) {
        Class<?>[] types = constructor.getParameterTypes();
        if (types.length == 1 && types[0] == Version.class) {
            try {
                return (Analyzer) constructor.newInstance(Version.LUCENE_36);
            } catch (Exception e) {
                cause = e;
            }
        }
    }
    for (Constructor<?> constructor : constructors) {
        if (constructor.getParameterTypes().length == 0) {
            try {
                return (Analyzer) constructor.newInstance();
            } catch (Exception e) {
                cause = e;
            }
        }
    }
    log.warn(className + " could not be instantiated", cause);
    return DEFAULT_ANALYZER;
}
Also used : Version(org.apache.lucene.util.Version) Constructor(java.lang.reflect.Constructor) ClassicAnalyzer(org.apache.lucene.analysis.standard.ClassicAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) IOException(java.io.IOException)

Example 64 with Analyzer

use of org.apache.lucene.analysis.Analyzer in project jackrabbit-oak by apache.

the class LuceneIndexAugmentTest method nullBehavior.

//OAK-3576
@Test
public void nullBehavior() throws Exception {
    //setup repo and index
    NodeTypeRegistry.register(root, IOUtils.toInputStream(TestUtil.TEST_NODE_TYPE), "test nodeType");
    Tree props = createIndex(TestUtil.NT_TEST);
    TestUtil.enableForFullText(props, "foo");
    root.commit();
    Tree rootTree = root.getTree("/").addChild("test");
    //Note: augmentor behavior is tested elsewhere... we are just checking if default works
    int testIndex = 1;
    //both query and index augmentors are null (no exception expected)
    checkSimpleBehavior(rootTree, testIndex++);
    //Set a very sad query augmentor
    factory.fulltextQueryTermsProvider = new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            return null;
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return FulltextQueryTermsProvider.DEFAULT.getSupportedTypes();
        }
    };
    checkSimpleBehavior(rootTree, testIndex++);
    //Set query augmentor... with null query
    factory.fulltextQueryTermsProvider = new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            return null;
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    checkSimpleBehavior(rootTree, testIndex++);
    //Set query augmentor... with some query
    factory.fulltextQueryTermsProvider = new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            return new TermQuery(new Term("bar", "baz"));
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    checkSimpleBehavior(rootTree, testIndex++);
    factory.fulltextQueryTermsProvider = null;
    //Set a very sad index augmentor
    factory.indexFieldProvider = IndexFieldProvider.DEFAULT;
    checkSimpleBehavior(rootTree, testIndex++);
    //Set index augmentor... with null fields
    factory.indexFieldProvider = new IndexFieldProvider() {

        @Nonnull
        @Override
        public Iterable<Field> getAugmentedFields(String path, NodeState document, NodeState indexDefinition) {
            return IndexFieldProvider.DEFAULT.getAugmentedFields(path, document, indexDefinition);
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    checkSimpleBehavior(rootTree, testIndex++);
    //Set index augmentor... with some fields
    factory.fulltextQueryTermsProvider = null;
    factory.indexFieldProvider = new IndexFieldProvider() {

        @Nonnull
        @Override
        public Iterable<Field> getAugmentedFields(String path, NodeState document, NodeState indexDefinition) {
            List<Field> fields = Lists.newArrayList();
            fields.add(new StringField("bar", "baz", Field.Store.NO));
            return fields;
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    checkSimpleBehavior(rootTree, testIndex++);
    //setup composite query term provider with one returning null query
    factory.registerQueryTermsProvider(new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            return null;
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    });
    factory.useSuperBehavior = true;
    checkSimpleBehavior(rootTree, testIndex++);
}
Also used : IndexFieldProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.IndexFieldProvider) TermQuery(org.apache.lucene.search.TermQuery) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) Set(java.util.Set) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) Nonnull(javax.annotation.Nonnull) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) StringField(org.apache.lucene.document.StringField) Tree(org.apache.jackrabbit.oak.api.Tree) List(java.util.List) Test(org.junit.Test) AbstractQueryTest(org.apache.jackrabbit.oak.query.AbstractQueryTest)

Example 65 with Analyzer

use of org.apache.lucene.analysis.Analyzer in project jackrabbit-oak by apache.

the class LuceneIndexAugmentTest method propertyIndexUsingAugmentors.

//OAK-3576
@Test
public void propertyIndexUsingAugmentors() throws Exception {
    //setup repo and index
    NodeTypeRegistry.register(root, IOUtils.toInputStream(TestUtil.TEST_NODE_TYPE), "test nodeType");
    Tree props = createIndex(TestUtil.NT_TEST);
    TestUtil.enablePropertyIndex(props, "foo1", false);
    TestUtil.enablePropertyIndex(props, "subChild/foo2", false);
    root.commit();
    //setup augmentors
    final AtomicInteger indexingCounter = new AtomicInteger(0);
    factory.indexFieldProvider = new IndexFieldProvider() {

        @Nonnull
        @Override
        public Iterable<Field> getAugmentedFields(String path, NodeState document, NodeState indexDefinition) {
            indexingCounter.incrementAndGet();
            return IndexFieldProvider.DEFAULT.getAugmentedFields(path, document, indexDefinition);
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    final AtomicInteger queryingCounter = new AtomicInteger(0);
    factory.fulltextQueryTermsProvider = new FulltextQueryTermsProvider() {

        @Override
        public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) {
            queryingCounter.set(1);
            return null;
        }

        @Nonnull
        @Override
        public Set<String> getSupportedTypes() {
            return Collections.singleton(TestUtil.NT_TEST);
        }
    };
    //add content
    Tree node1 = createNodeWithType(root.getTree("/"), "node1", TestUtil.NT_TEST);
    node1.setProperty("foo1", "bar1");
    node1.addChild("subChild").setProperty("foo2", "bar2");
    root.commit();
    //indexing assertions
    assertEquals("Indexing augment should get called once", 1, indexingCounter.get());
    String query = "SELECT [jcr:path] from [" + TestUtil.NT_TEST + "] WHERE [foo1]='bar1'";
    executeQuery(query, SQL2);
    assertEquals("Query augmentor should not get called for property constraints", 0, queryingCounter.get());
    query = "EXPLAIN " + query;
    List<String> paths = executeQuery(query, SQL2, false);
    assertTrue("property index should have made the index selected (" + paths.get(0) + ")", paths.get(0).contains("/* lucene:test-index("));
    query = "SELECT [jcr:path] from [" + TestUtil.NT_TEST + "] WHERE [subChild/foo2]='bar2'";
    executeQuery(query, SQL2);
    assertEquals("Query augmentor should not get called for property constraints", 0, queryingCounter.get());
    query = "EXPLAIN " + query;
    paths = executeQuery(query, SQL2);
    assertTrue("property index should have made the index selected (" + paths.get(0) + ")", paths.get(0).contains("/* lucene:test-index("));
}
Also used : IndexFieldProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.IndexFieldProvider) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) Set(java.util.Set) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) Nonnull(javax.annotation.Nonnull) FulltextQueryTermsProvider(org.apache.jackrabbit.oak.plugins.index.lucene.spi.FulltextQueryTermsProvider) Analyzer(org.apache.lucene.analysis.Analyzer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tree(org.apache.jackrabbit.oak.api.Tree) Test(org.junit.Test) AbstractQueryTest(org.apache.jackrabbit.oak.query.AbstractQueryTest)

Aggregations

Analyzer (org.apache.lucene.analysis.Analyzer)1020 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)396 Tokenizer (org.apache.lucene.analysis.Tokenizer)265 MockTokenizer (org.apache.lucene.analysis.MockTokenizer)228 Document (org.apache.lucene.document.Document)207 Directory (org.apache.lucene.store.Directory)192 KeywordTokenizer (org.apache.lucene.analysis.core.KeywordTokenizer)176 BytesRef (org.apache.lucene.util.BytesRef)122 Test (org.junit.Test)119 TokenStream (org.apache.lucene.analysis.TokenStream)107 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)92 Term (org.apache.lucene.index.Term)92 IndexReader (org.apache.lucene.index.IndexReader)67 InputArrayIterator (org.apache.lucene.search.suggest.InputArrayIterator)65 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)64 Input (org.apache.lucene.search.suggest.Input)63 CharArraySet (org.apache.lucene.analysis.CharArraySet)58 ArrayList (java.util.ArrayList)57 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)57 TextField (org.apache.lucene.document.TextField)55