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 <s...@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-unsubscr...@lucene.apache.org
> For additional commands, e-mail: java-user-h...@lucene.apache.org
> 


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

Reply via email to