Solr Custom Similarity

Viewed 7435

i want to set my own custom similarity in my solr schema.xml but i have a few problems with understanding this feature. I want to completely deactivate solr scoring (tf,idf,coord and fieldNorm).

I dont know where to start. Things i know

  1. I have to write my own DefaultSimilarity implementation.
  2. Override the (tf,idf,coord and fieldNorm) - methods.
  3. Load the class in schem.xml

Where to store the class ? Are there any working examples in the web ? I cant find one!

THANKS

2 Answers

The implementation of the Similarity changed in solr 8.0.

Here an example how to do it since solr 8.

public class CustomSimilarityFactory extends SchemaSimilarityFactory {

    @Override
    public Similarity getSimilarity() {
        return new CustomSimilarity();
    }

}

public class CustomSimilarity extends Similarity{

    private final SimScorer customSimScorer = new CustomSimScorer();

    @Override
    public long computeNorm(FieldInvertState fis) {
        return 1L;
    }

    @Override
    public SimScorer scorer(float f, CollectionStatistics cs, TermStatistics... tss) {
        return customSimScorer;
    }

}

public class CustomSimScorer extends SimScorer {

    @Override
    public float score(float f, long l) {
        return 1f;
    }

}

Add your lib to the solrconfig.xml <lib dir="/yourCustomDir/" regex=".*\.jar"/> and your custom similarity to your schema.xml <similarity class="com.christoph.solr.CustomSimilarityFactory"></similarity>

Related