You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Sébastien Rainville <se...@gmail.com> on 2008/07/17 03:19:16 UTC

Using custom Similarity class

Hi,

I'm trying to make solr use my custom similarity class:

import org.apache.lucene.search.DefaultSimilarity;

public class CustomSimilarity extends DefaultSimilarity {
}

but I keep getting this error:

com.example.CustomSimilarity cannot be cast to
org.apache.lucene.search.Similarity
    at org.apache.solr.schema.IndexSchema.readConfig(IndexSchema.java:449)
    ... 28 more

and in schema.xml I have this:
<similarity class="com.example.CustomSimilarity"/>

I even tried to extend Similarity directly just to make sure and it's not
working either. It means that it finds the class, it's able to instantiate
it but somehow it's not the right kind of object?!? What am I missing?

Thanks in advance,
Sebastien

Re: Using custom Similarity class

Posted by Chris Hostetter <ho...@fucit.org>.
: Thanks for the tip but I solved it but using the old way of loading custom
: libs into solr: unpack the war file, add the custom library to WEB-INF/lib
: and repack it. It definetly is a class loader problem, as the wiki specifies
: that the new way of loading custom jars is using a custom class loader that
: *might* not work...

Hmmm... the "might not work" comment on that wiki is pretty old, it's been 
fairly well vetted at this point, and so far I haven't seen anyone have 
any serious problems with it.

I know you've got things working fine for you know, but whould you mind 
helping us diagnose the root problem incase it happens to other people who 
aren't as familiar with building their own war?

 * which servlet container are you using?
 * which JVM ?
before you rolled your own war...
 * what did the full directory structure of your solr home look like?
 * with logging at at least the INFO level, did you see any log messages 
   that mentioned "Solr classloader" or "solr lib class loader" ?


-Hoss


Re: Using custom Similarity class

Posted by Sébastien Rainville <se...@gmail.com>.
Thanks for the tip but I solved it but using the old way of loading custom
libs into solr: unpack the war file, add the custom library to WEB-INF/lib
and repack it. It definetly is a class loader problem, as the wiki specifies
that the new way of loading custom jars is using a custom class loader that
*might* not work...

Sebastien




On Thu, Jul 17, 2008 at 10:35 AM, Koji Sekiguchi <ko...@r.email.ne.jp> wrote:

> > com.example.CustomSimilarity cannot be cast to
> > org.apache.lucene.search.Similarity
> > at org.apache.solr.schema.IndexSchema.readConfig(IndexSchema.java:449)
> > ... 28 more
>
> I think you've got a class loader problem.
> If you have solr-1.2.0 source code, see the line 499 of IndexSchema.java:
>
>     similarity =
> (Similarity)Config.newInstance(node.getNodeValue().trim());
>
> Change the line to:
>
> Object o = Config.newInstance(node.getNodeValue().trim());
> System.err.println("(1) The object " + o + " classloader is " +
> o.getClass().getClassLoader());
> System.err.println("(2) Class Similarity class loader is " +
> Similarity.class.getClassLoader());
> similarity = (Similarity) o;
>
> You will see different classloader between (1) and (2).
> I'm not familiar with jetty classloader, but you can
> google "jetty classloader" to get a solution.
>
> Cheers,
>
> Koji
>
>

Re: Using custom Similarity class

Posted by Koji Sekiguchi <ko...@r.email.ne.jp>.
 > com.example.CustomSimilarity cannot be cast to
 > org.apache.lucene.search.Similarity
 > at org.apache.solr.schema.IndexSchema.readConfig(IndexSchema.java:449)
 > ... 28 more

I think you've got a class loader problem.
If you have solr-1.2.0 source code, see the line 499 of IndexSchema.java:

      similarity = 
(Similarity)Config.newInstance(node.getNodeValue().trim());

Change the line to:

Object o = Config.newInstance(node.getNodeValue().trim());
System.err.println("(1) The object " + o + " classloader is " + 
o.getClass().getClassLoader());
System.err.println("(2) Class Similarity class loader is " + 
Similarity.class.getClassLoader());
similarity = (Similarity) o;

You will see different classloader between (1) and (2).
I'm not familiar with jetty classloader, but you can
google "jetty classloader" to get a solution.

Cheers,

Koji


Re: Using custom Similarity class

Posted by Sébastien Rainville <se...@gmail.com>.
I'm using solr-1.2.0. I didn't have a no-arg constructor. I just tried with
one and it doesn't fix it. My servlet container is Jetty.

Sebastien



On Wed, Jul 16, 2008 at 10:22 PM, Erik Hatcher <er...@ehatchersolutions.com>
wrote:

> What version of Solr are you using?   Seems to be not quite trunk at least,
> as IndexSchema.java:449 isn't the the similarity stuff currently).
>
> Does your CustomSimilarity have a no-arg constructor?
>
>        Erik
>
>
>
>
> On Jul 16, 2008, at 9:19 PM, Sébastien Rainville wrote:
>
>  Hi,
>>
>> I'm trying to make solr use my custom similarity class:
>>
>> import org.apache.lucene.search.DefaultSimilarity;
>>
>> public class CustomSimilarity extends DefaultSimilarity {
>> }
>>
>> but I keep getting this error:
>>
>> com.example.CustomSimilarity cannot be cast to
>> org.apache.lucene.search.Similarity
>>   at org.apache.solr.schema.IndexSchema.readConfig(IndexSchema.java:449)
>>   ... 28 more
>>
>> and in schema.xml I have this:
>> <similarity class="com.example.CustomSimilarity"/>
>>
>> I even tried to extend Similarity directly just to make sure and it's not
>> working either. It means that it finds the class, it's able to instantiate
>> it but somehow it's not the right kind of object?!? What am I missing?
>>
>> Thanks in advance,
>> Sebastien
>>
>
>

Re: Using custom Similarity class

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
What version of Solr are you using?   Seems to be not quite trunk at  
least, as IndexSchema.java:449 isn't the the similarity stuff  
currently).

Does your CustomSimilarity have a no-arg constructor?

	Erik



On Jul 16, 2008, at 9:19 PM, Sébastien Rainville wrote:

> Hi,
>
> I'm trying to make solr use my custom similarity class:
>
> import org.apache.lucene.search.DefaultSimilarity;
>
> public class CustomSimilarity extends DefaultSimilarity {
> }
>
> but I keep getting this error:
>
> com.example.CustomSimilarity cannot be cast to
> org.apache.lucene.search.Similarity
>    at org.apache.solr.schema.IndexSchema.readConfig(IndexSchema.java: 
> 449)
>    ... 28 more
>
> and in schema.xml I have this:
> <similarity class="com.example.CustomSimilarity"/>
>
> I even tried to extend Similarity directly just to make sure and  
> it's not
> working either. It means that it finds the class, it's able to  
> instantiate
> it but somehow it's not the right kind of object?!? What am I missing?
>
> Thanks in advance,
> Sebastien