ClassCastException when trying to apply custom fil

2019-07-17 06:11发布

问题:

I'm using 4.10.3 solr and after I implemented Factory with Filter

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.util.TokenFilterFactory;

import java.util.Map;

public class CustomFilterFactory extends TokenFilterFactory {

protected CustomFilterFactory(Map<String, String> args) {
    super(args);
}

@Override
public TokenStream create(TokenStream tokenStream) {
    return new CustomFilter(tokenStream);
}
}  

and when Solr starts, I'm getting strange errors in log:

Caused by: 
java.lang.ClassCastException: class org.apache.lucene.analysis.tr.ApostropheFilterFactory
at java.lang.Class.asSubclass(Class.java:3116)
at org.apache.lucene.util.SPIClassIterator.next(SPIClassIterator.java:141)
at org.apache.lucene.analysis.util.AnalysisSPILoader.reload(AnalysisSPILoader.java:79)
at org.apache.lucene.analysis.util.AnalysisSPILoader.<init>(AnalysisSPILoader.java:60)
at org.apache.lucene.analysis.util.AnalysisSPILoader.<init>(AnalysisSPILoader.java:49)
at org.apache.lucene.analysis.util.TokenFilterFactory.<clinit>(TokenFilterFactory.java:31)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.apache.solr.core.SolrResourceLoader.findClass(SolrResourceLoader.java:474)
at org.apache.solr.core.SolrResourceLoader.newInstance(SolrResourceLoader.java:593)
at org.apache.solr.schema.FieldTypePluginLoader$1.create(FieldTypePluginLoader.java:306)
at org.apache.solr.schema.FieldTypePluginLoader$1.create(FieldTypePluginLoader.java:299)
at org.apache.solr.util.plugin.AbstractPluginLoader.load(AbstractPluginLoader.java:151)
at org.apache.solr.schema.FieldTypePluginLoader.readAnalyzer(FieldTypePluginLoader.java:325)
at org.apache.solr.schema.FieldTypePluginLoader.create(FieldTypePluginLoader.java:95)
at org.apache.solr.schema.FieldTypePluginLoader.create(FieldTypePluginLoader.java:43)
at org.apache.solr.util.plugin.AbstractPluginLoader.load(AbstractPluginLoader.java:151)
at org.apache.solr.schema.IndexSchema.readSchema(IndexSchema.java:486)

Any idea what could cause this state?

Field definition:

<field name="ii" type="customFilter" indexed="true" stored="false"  required="false" multiValued="false"/>
<fieldType name="customFilter" class="solr.TextField">
    <analyzer>
        <charFilter class="com.nosql.search.solr.CustomFilterFactory" /> 
    </analyzer> 
</fieldType>

I'm using same Java version for my custom jar with filter and for starting Solr.

It works if I remove the reference to CustomFilterFactory.

回答1:

I had the same issue with Solr 6.4. But my solr is deployed within the Hybris 6 framework. The point is that I tried to add my custom token filter library directly to solr server libraries ${solr_instance}/server/lib, then I got an exception that the engine could find the parent lucene class TokenFilterFactory. After the adding of the related jars lucene-analysers-common-x, lucene-core-x, I got the same error as yours (java.lang.ClassCastException: class org.apache.lucene.analysis.tr.ApostropheFilterFactory)

To resolve this I just moved my custom jar to the custom folder ${solr_instance}/contrib/hybris/lib without any additional lucene libs. Moreover my solrconfig.xml already had a link to it:

<lib dir="${solr.install.dir:../../../..}/contrib/hybris/lib" regex=".*\.jar" />

So, one can conclude that this kind of exception refers to classpath ambiguity issue, which confuses Solr.



标签: solr lucene