ClassCastException: Failed to load custom similarity plugin

Hi,
I wrote our own custom similarity plugin, installed it successfully, but when booting elastic I get:

2016-09-29 10:31:10.875044500 Exception in thread "main" java.lang.ClassCastException: class >org.elasticsearch.index.similarity.LengthNormalizingSimilarityProvider
2016-09-29 10:31:10.875412500 at java.lang.Class.asSubclass(Class.java:3404)
2016-09-29 10:31:10.875414500 at org.elasticsearch.plugins.PluginsService.loadPluginClass(PluginsService.java:463)
2016-09-29 10:31:10.875416500 at org.elasticsearch.plugins.PluginsService.loadBundles(PluginsService.java:431)
2016-09-29 10:31:10.875417500 at org.elasticsearch.plugins.PluginsService.(PluginsService.java:129)
2016-09-29 10:31:10.875419500 at org.elasticsearch.node.Node.(Node.java:158)
2016-09-29 10:31:10.875550500 at org.elasticsearch.node.Node.(Node.java:140)
2016-09-29 10:31:10.875552500 at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:143)
2016-09-29 10:31:10.875553500 at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:178)
2016-09-29 10:31:10.875663500 at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:270)
2016-09-29 10:31:10.875664500 at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35)
2016-09-29 10:31:10.875666500 Refer to the log for complete error details.

The plugin I wrote is:

public class BuzzillaSimilarityPlugin extends Plugin {
@Override
public String name() {
return "buzzilla-similarity-plugin";
}
@Override
public String description() {
return "Implements similarity required by the Buzzilla search";
}
public void onModule(final SimilarityModule module) {
module.addSimilarity("buzzilla-similarity", LengthNormalizingSimilarityProvider.class);
}
}

The custom similarity is:

public class LengthNormalizingSimilarityProvider extends AbstractSimilarityProvider {
private ClassicSimilarity similarity;
@Inject
public LengthNormalizingSimilarityProvider(@Assisted String name, @Assisted Settings settings) {
super(name);
this.similarity = new ClassicSimilarity() {
@Override
public float lengthNorm(FieldInvertState state) {
state.setLength(state.getLength() < 15 ? 15 : state.getLength());
return super.lengthNorm(state);
}
};
}
@Override
public ClassicSimilarity get() {
return similarity;
}
}

and the plugin-descriptor.properties is:

description=buzzill-similarity-plugin
name=buzzilla-similarity-plugin
jvm=true
java.version=1.8.0
elasticsearch.version=2.3.4
classname=org.elasticsearch.index.similarity.LengthNormalizingSimilarityProvider

What am I missing?

Do not declare a similarity provider as a plugin class. Use

classname=[yourpackage].BuzzillaSimilarityPlugin

Thanks, it solved this issue and elastic boot with no errors, but now, when I try to index docs I get:

2016-09-29 12:46:04.653317500 java.lang.IllegalArgumentException: Unknown Similarity type [org.elasticsearch.index.similarity.LengthNormalizingSimilarityProvider] for [index]
2016-09-29 12:46:04.653320500 at org.elasticsearch.index.similarity.SimilarityModule.configure(SimilarityModule.java:82)
2016-09-29 12:46:04.653321500 at org.elasticsearch.common.inject.AbstractModule.configure(AbstractModule.java:60)
2016-09-29 12:46:04.653321500 at org.elasticsearch.common.inject.spi.Elements$RecordingBinder.install(Elements.java:233)
2016-09-29 12:46:04.653324500 at org.elasticsearch.common.inject.spi.Elements.getElements(Elements.java:105)
2016-09-29 12:46:04.653325500 at org.elasticsearch.common.inject.InjectorShell$Builder.build(InjectorShell.java:143)
2016-09-29 12:46:04.653325500 at org.elasticsearch.common.inject.InjectorBuilder.build(InjectorBuilder.java:99)
2016-09-29 12:46:04.653326500 at org.elasticsearch.common.inject.InjectorImpl.createChildInjector(InjectorImpl.java:157)
2016-09-29 12:46:04.653331500 at org.elasticsearch.common.inject.ModulesBuilder.createChildInjector(ModulesBuilder.java:55)
2016-09-29 12:46:04.653331500 at org.elasticsearch.indices.IndicesService.createIndex(IndicesService.java:358)
2016-09-29 12:46:04.653332500 at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService$1.execute(MetaDataCreateIndexService.java:309)
2016-09-29 12:46:04.653332500 at org.elasticsearch.cluster.ClusterStateUpdateTask.execute(ClusterStateUpdateTask.java:45)
2016-09-29 12:46:04.653336500 at org.elasticsearch.cluster.service.InternalClusterService.runTasksForExecutor(InternalClusterService.java:468)
2016-09-29 12:46:04.653337500 at org.elasticsearch.cluster.service.InternalClusterService$UpdateTask.run(InternalClusterService.java:772)
2016-09-29 12:46:04.653337500 at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.runAndClean(PrioritizedEsThreadPoolExecutor.java:231)
2016-09-29 12:46:04.653340500 at org.elasticsearch.common.util.concurrent.PrioritizedEsThreadPoolExecutor$TieBreakingPrioritizedRunnable.run(PrioritizedEsThreadPoolExecutor.java:194)
2016-09-29 12:46:04.653341500 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
2016-09-29 12:46:04.653346500 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
2016-09-29 12:46:04.653347500 at java.lang.Thread.run(Thread.java:745)

in indexSettings I put:

"similarity":{
"index":{
"type":"org.elasticsearch.index.similarity.LengthNormalizingSimilarityProvider"
},
"search":{
"type":"org.elasticsearch.index.similarity.LengthNormalizingSimilarityProvider"
}
}

How can I solve it, please?

Fixed after doing as described in
http://stefansavev.com/blog/custom-similarity-for-elasticsearch/
Thanks

Hi Moshe,
I am trying to deploy the custom similarity plugin given in the mentioned blog but getting the error as

{
"error": {
"root_cause": [
{
"type": "index_creation_exception",
"reason": "failed to create index"
}
],
"type": "illegal_argument_exception",
"reason": "Unknown Similarity type [overlapsimilarity] for [custom_similarity]"
},
"status": 400
}

I have followed all the steps mentioned in the blog but I guess I am missing something.
Any idea what can be wrong here ? (I am using ES version 2.3.4)

Hi Krati,
I had a mismatch with files names and the similarity name.
After reading the above post, I renamed the class names and their calls and it worked.

Thanks Moshe for the quick help.
I started again carefully, checking every minute detail and its working now. :slight_smile: