Search in sources :

Example 1 with AbstractAnalysisFactory

use of org.apache.lucene.analysis.util.AbstractAnalysisFactory in project lucene-solr by apache.

the class TestFactories method doTestTokenFilter.

private void doTestTokenFilter(String tokenfilter) throws IOException {
    Class<? extends TokenFilterFactory> factoryClazz = TokenFilterFactory.lookupClass(tokenfilter);
    TokenFilterFactory factory = (TokenFilterFactory) initialize(factoryClazz);
    if (factory != null) {
        // if it implements MultiTermAware, sanity check its impl
        if (factory instanceof MultiTermAwareComponent) {
            AbstractAnalysisFactory mtc = ((MultiTermAwareComponent) factory).getMultiTermComponent();
            assertNotNull(mtc);
            // it's not ok to return a charfilter or tokenizer here, this makes no sense
            assertTrue(mtc instanceof TokenFilterFactory);
        }
        // beast it just a little, it shouldnt throw exceptions:
        // (it should have thrown them in initialize)
        Analyzer a = new FactoryAnalyzer(assertingTokenizer, factory, null);
        checkRandomData(random(), a, 20, 20, false, false);
        a.close();
    }
}
Also used : MultiTermAwareComponent(org.apache.lucene.analysis.util.MultiTermAwareComponent) AbstractAnalysisFactory(org.apache.lucene.analysis.util.AbstractAnalysisFactory) Analyzer(org.apache.lucene.analysis.Analyzer) TokenFilterFactory(org.apache.lucene.analysis.util.TokenFilterFactory)

Example 2 with AbstractAnalysisFactory

use of org.apache.lucene.analysis.util.AbstractAnalysisFactory in project lucene-solr by apache.

the class TestFactories method doTestTokenizer.

private void doTestTokenizer(String tokenizer) throws IOException {
    Class<? extends TokenizerFactory> factoryClazz = TokenizerFactory.lookupClass(tokenizer);
    TokenizerFactory factory = (TokenizerFactory) initialize(factoryClazz);
    if (factory != null) {
        // if it implements MultiTermAware, sanity check its impl
        if (factory instanceof MultiTermAwareComponent) {
            AbstractAnalysisFactory mtc = ((MultiTermAwareComponent) factory).getMultiTermComponent();
            assertNotNull(mtc);
            // it's not ok to return e.g. a charfilter here: but a tokenizer could wrap a filter around it
            assertFalse(mtc instanceof CharFilterFactory);
        }
        // beast it just a little, it shouldnt throw exceptions:
        // (it should have thrown them in initialize)
        Analyzer a = new FactoryAnalyzer(factory, null, null);
        checkRandomData(random(), a, 20, 20, false, false);
        a.close();
    }
}
Also used : MultiTermAwareComponent(org.apache.lucene.analysis.util.MultiTermAwareComponent) TokenizerFactory(org.apache.lucene.analysis.util.TokenizerFactory) CharFilterFactory(org.apache.lucene.analysis.util.CharFilterFactory) AbstractAnalysisFactory(org.apache.lucene.analysis.util.AbstractAnalysisFactory) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 3 with AbstractAnalysisFactory

use of org.apache.lucene.analysis.util.AbstractAnalysisFactory in project lucene-solr by apache.

the class TestFactories method initialize.

/** tries to initialize a factory with no arguments */
private AbstractAnalysisFactory initialize(Class<? extends AbstractAnalysisFactory> factoryClazz) throws IOException {
    Map<String, String> args = new HashMap<>();
    args.put("luceneMatchVersion", Version.LATEST.toString());
    Constructor<? extends AbstractAnalysisFactory> ctor;
    try {
        ctor = factoryClazz.getConstructor(Map.class);
    } catch (Exception e) {
        throw new RuntimeException("factory '" + factoryClazz + "' does not have a proper ctor!");
    }
    AbstractAnalysisFactory factory = null;
    try {
        factory = ctor.newInstance(args);
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof IllegalArgumentException) {
            // it's ok if we dont provide the right parameters to throw this
            return null;
        }
    }
    if (factory instanceof ResourceLoaderAware) {
        try {
            ((ResourceLoaderAware) factory).inform(new StringMockResourceLoader(""));
        } catch (IOException ignored) {
        // it's ok if the right files arent available or whatever to throw this
        } catch (IllegalArgumentException ignored) {
        // is this ok? I guess so
        }
    }
    return factory;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourceLoaderAware(org.apache.lucene.analysis.util.ResourceLoaderAware) AbstractAnalysisFactory(org.apache.lucene.analysis.util.AbstractAnalysisFactory) HashMap(java.util.HashMap) Map(java.util.Map) StringMockResourceLoader(org.apache.lucene.analysis.util.StringMockResourceLoader)

Example 4 with AbstractAnalysisFactory

use of org.apache.lucene.analysis.util.AbstractAnalysisFactory in project lucene-solr by apache.

the class TestFactories method doTestTokenizer.

private void doTestTokenizer(String tokenizer) throws IOException {
    Class<? extends TokenizerFactory> factoryClazz = TokenizerFactory.lookupClass(tokenizer);
    TokenizerFactory factory = (TokenizerFactory) initialize(factoryClazz);
    if (factory != null) {
        // if it implements MultiTermAware, sanity check its impl
        if (factory instanceof MultiTermAwareComponent) {
            AbstractAnalysisFactory mtc = ((MultiTermAwareComponent) factory).getMultiTermComponent();
            assertNotNull(mtc);
            // it's not ok to return e.g. a charfilter here: but a tokenizer could wrap a filter around it
            assertFalse(mtc instanceof CharFilterFactory);
        }
        // beast it just a little, it shouldnt throw exceptions:
        // (it should have thrown them in initialize)
        Analyzer a = new FactoryAnalyzer(factory, null, null);
        checkRandomData(random(), a, 20, 20, false, false);
        a.close();
    }
}
Also used : MultiTermAwareComponent(org.apache.lucene.analysis.util.MultiTermAwareComponent) TokenizerFactory(org.apache.lucene.analysis.util.TokenizerFactory) CharFilterFactory(org.apache.lucene.analysis.util.CharFilterFactory) AbstractAnalysisFactory(org.apache.lucene.analysis.util.AbstractAnalysisFactory) Analyzer(org.apache.lucene.analysis.Analyzer)

Example 5 with AbstractAnalysisFactory

use of org.apache.lucene.analysis.util.AbstractAnalysisFactory in project lucene-solr by apache.

the class TestFactories method initialize.

/** tries to initialize a factory with no arguments */
private AbstractAnalysisFactory initialize(Class<? extends AbstractAnalysisFactory> factoryClazz) throws IOException {
    Map<String, String> args = new HashMap<>();
    args.put("luceneMatchVersion", Version.LATEST.toString());
    Constructor<? extends AbstractAnalysisFactory> ctor;
    try {
        ctor = factoryClazz.getConstructor(Map.class);
    } catch (Exception e) {
        throw new RuntimeException("factory '" + factoryClazz + "' does not have a proper ctor!");
    }
    AbstractAnalysisFactory factory = null;
    try {
        factory = ctor.newInstance(args);
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof IllegalArgumentException) {
            // it's ok if we dont provide the right parameters to throw this
            return null;
        }
    }
    if (factory instanceof ResourceLoaderAware) {
        try {
            ((ResourceLoaderAware) factory).inform(new StringMockResourceLoader(""));
        } catch (IOException ignored) {
        // it's ok if the right files arent available or whatever to throw this
        } catch (IllegalArgumentException ignored) {
        // is this ok? I guess so
        }
    }
    return factory;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ResourceLoaderAware(org.apache.lucene.analysis.util.ResourceLoaderAware) AbstractAnalysisFactory(org.apache.lucene.analysis.util.AbstractAnalysisFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

AbstractAnalysisFactory (org.apache.lucene.analysis.util.AbstractAnalysisFactory)9 Analyzer (org.apache.lucene.analysis.Analyzer)6 MultiTermAwareComponent (org.apache.lucene.analysis.util.MultiTermAwareComponent)6 CharFilterFactory (org.apache.lucene.analysis.util.CharFilterFactory)4 HashMap (java.util.HashMap)3 ResourceLoaderAware (org.apache.lucene.analysis.util.ResourceLoaderAware)3 TokenizerFactory (org.apache.lucene.analysis.util.TokenizerFactory)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Map (java.util.Map)2 TokenFilterFactory (org.apache.lucene.analysis.util.TokenFilterFactory)2 Path (java.nio.file.Path)1 FilesystemResourceLoader (org.apache.lucene.analysis.util.FilesystemResourceLoader)1 StringMockResourceLoader (org.apache.lucene.analysis.util.StringMockResourceLoader)1