lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joel Barry <>
Subject questions on PerFieldSimilarityWrapper
Date Wed, 07 Nov 2012 22:23:48 GMT
Hi folks,

I have a question on PerFieldSimilarityWrapper.  It seems that it is
not possible to get per-field behavior on queryNorm() and coord()...

The documentation for PerFieldAnalyzerWrapper (lucene 4.0) says:

  Subclasses should implement get(String) to return an appropriate
  Similarity (for example, using field-specific parameter values) for
  the field.

This leads the user to believe that *only* get() needs to be
overridden. However, I've found that I must override queryNorm() as
well, otherwise Similarity.queryNorm() will be called (because
PerFieldAnalyzerWrapper extends Similarity), not the user-supplied

The test cases in lucene seem always to override queryNorm() and
(coord() too), but I don't see tests for the per-field behavior of
these. Indeed, there seems to be no way to get the field name from
these methods. And that's the problem.  I'd like to have per-field
behavior for queryNorm() and coord().

Below is some code to illustrate the issue:

class MyPerFieldSimilarity1 extends PerFieldSimilarityWrapper {
    public Similarity get(String name) {
        return new DefaultSimilarity();

class MyPerFieldSimilarity2 extends PerFieldSimilarityWrapper {
    public Similarity get(String name) {
        return new DefaultSimilarity();

    public float queryNorm(float valueForNormalization) {
        // Notice that I don't have access to the read field name here...
        return get("dummy").queryNorm(valueForNormalization);

public class PerFieldSimilarityWrapperTest {
    private float runTest(Similarity similarity) throws IOException {
        IndexWriterConfig config = new
IndexWriterConfig(Version.LUCENE_40, new
        Directory dir = new RAMDirectory();
        IndexWriter writer = new IndexWriter(dir, config);
        Document doc = new Document();
        String fieldName = "some_field";
        doc.add(new TextField(fieldName, "some text", Store.YES));

        IndexReader reader =;
        IndexSearcher searcher = new IndexSearcher(reader);
        TermQuery query = new TermQuery(new Term(fieldName, "text"));
        TopDocs topDocs =, 1);
        float score = topDocs.scoreDocs[0].score;
        return score;

    public static void main(String[] args) throws IOException {
        PerFieldSimilarityWrapperTest that = new
        System.out.println(that.runTest(new DefaultSimilarity()));
        System.out.println(that.runTest(new MyPerFieldSimilarity1()));
        System.out.println(that.runTest(new MyPerFieldSimilarity2()));

Running this produces:


Am I overlooking something here or is this a bug?

- Joel

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message