You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Rune Stilling <su...@rdfined.dk> on 2014/04/11 16:41:43 UTC

Multiply instead of summing two scores

Hi list

Normally when I search with multiple terms using a BooleanQuery the calculated weights of each term are added/summed together to give a document the final score. Lets say thats fine for most fields in my index. I’ll name these fields F1 and F2. So a weight is calculated likes this:

doc weight = f1_weight + f2_weight

Now I add a third field (F3) for which I want a different score calculation. If theres a match in this field I would like to multiply this field weight with the sum of the other weights, like this:

doc weight = (f1_weight + f2_weight) * f3_weight

This is a kind of query dependent document boost.

I can’t find out where to determine/control this behavior in Lucene i.e.. how the document weight is calculated from each term weight.

With regards,
Rune Stilling


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Multiply instead of summing two scores

Posted by Rune Stilling <su...@rdfined.dk>.
Hi list

For the record I have found a way to implement my requirement.

I have implemented my own CustomScoreQuery using that to check when there’s a match bewteen my query and the specific field (f3). When there’s a match I multiply with my boost score.

The implementation looks like this:

public class MatchedFieldBoostingQuery extends CustomScoreQuery {

    String matchField;
    Query q;

    public MatchedFieldBoostingQuery(Query q, String matchField) {
        super(q);
        this.matchField = matchField;
        this.q = q;
    }

    private class MatchedField extends CustomScoreProvider {

        final BinaryDocValues matchFieldValue;

        public MatchedField(AtomicReaderContext context) throws IOException {
            super(context);
            matchFieldValue = FieldCache.DEFAULT.getTerms(context.reader(), matchField, false);
        }

        public float customScore(int doc, float subQueryScore, float valSrcScore) {
            float returnValue = subQueryScore;
            HashSet<Term> terms = new HashSet<Term>();
            q.extractTerms(terms);
            Iterator iter = terms.iterator();
            while (iter.hasNext()) {
                Term term = (Term) iter.next();
                if (term.field().equalsIgnoreCase(matchField)) {
                    BytesRef value = new BytesRef();
                    matchFieldValue.get(doc, value);
                    if (value.utf8ToString().length() > 0) {
                        if (value.utf8ToString().equalsIgnoreCase(term.bytes().utf8ToString())) {
                            returnValue = (float) (subQueryScore * 1.3);
                        } else {
                            returnValue = (float) (subQueryScore * 0.7);
                        }
                    }
                }
            }
            return returnValue;
        }

    }

    public CustomScoreProvider getCustomScoreProvider(AtomicReaderContext context) throws IOException {
        return new MatchedField(context);
    }
}


Den 11/04/2014 kl. 16.41 skrev Rune Stilling <su...@rdfined.dk>:

> Hi list
> 
> Normally when I search with multiple terms using a BooleanQuery the calculated weights of each term are added/summed together to give a document the final score. Lets say thats fine for most fields in my index. I’ll name these fields F1 and F2. So a weight is calculated likes this:
> 
> doc weight = f1_weight + f2_weight
> 
> Now I add a third field (F3) for which I want a different score calculation. If theres a match in this field I would like to multiply this field weight with the sum of the other weights, like this:
> 
> doc weight = (f1_weight + f2_weight) * f3_weight
> 
> This is a kind of query dependent document boost.
> 
> I can’t find out where to determine/control this behavior in Lucene i.e.. how the document weight is calculated from each term weight.
> 
> With regards,
> Rune Stilling
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Multiply instead of summing two scores

Posted by Jose Carlos Canova <jo...@gmail.com>.
Hum,

You don't have a document weight you have a Document Score in relation of
other documents on the index during a search event. On practice the
document weight will be the sum of the weight of the terms in relation with
an Index.

You might find this presentation useful.

http://www.cs.cmu.edu/~wcohen/whirl-talk.pdf

Lucene terms weight and document scoring is a similar model.



On Fri, Apr 11, 2014 at 11:41 AM, Rune Stilling <su...@rdfined.dk> wrote:

> Hi list
>
> Normally when I search with multiple terms using a BooleanQuery the
> calculated weights of each term are added/summed together to give a
> document the final score. Lets say thats fine for most fields in my index.
> I'll name these fields F1 and F2. So a weight is calculated likes this:
>
> doc weight = f1_weight + f2_weight
>
> Now I add a third field (F3) for which I want a different score
> calculation. If theres a match in this field I would like to multiply this
> field weight with the sum of the other weights, like this:
>
> doc weight = (f1_weight + f2_weight) * f3_weight
>
> This is a kind of query dependent document boost.
>
> I can't find out where to determine/control this behavior in Lucene i.e..
> how the document weight is calculated from each term weight.
>
> With regards,
> Rune Stilling
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>