Solr Custom Similarity
Asked Answered
L

2

9

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

Lucilius answered 6/12, 2013 at 16:21 Comment(0)
L
13

I figured it out on my own. I have stored my own implementation of DefaultSimilarity under /dist/ folder in solr. Then i add <lib dir="../../../dist/org/apache/lucene/search/similarities/" regex=".*\.jar"/> to my solrconfig.xml and everything works fine.

package org.apache.lucene.search.similarities;
import org.apache.lucene.index.FieldInvertState;
import org.apache.lucene.search.similarities.DefaultSimilarity;

public class MyNewSimilarityClass extends DefaultSimilarity {

@Override
public float coord(int overlap, int maxOverlap) {
    return 1.0f;
}

@Override
public float idf(long docFreq, long numDocs) {
    return 1.0f;
}

@Override
public float lengthNorm(FieldInvertState arg0) {
    return 1.0f;
}

@Override
public float tf(float freq) {
    return 1.0f;
}

}

Gist: https://gist.github.com/FabianKoestring/7846845

Lucilius answered 7/12, 2013 at 16:45 Comment(2)
DefaultSimilarity seems to have been removed in latest versions of solr, is there an update gist for the same?Heredity
@Heredity see my post for newer versions.Prana
P
3

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>

Prana answered 13/8, 2020 at 14:45 Comment(1)
If I were to use CustomSimScorer to override the score, how do I access a specific field from the document (say POPULARITY_RANK) and influence the final score. For - take only 10% of the solr computed score and add POPULARITY_RANK to it.Heredity

© 2022 - 2024 — McMap. All rights reserved.