How to sort search results on multiple fields using a weighting function?

前端 未结 4 1302
走了就别回头了
走了就别回头了 2020-12-16 21:12

I have a Lucene index where every document has several fields which contain numeric values. Now I would like to sort the search result on a weighted sum of this field. For e

4条回答
  •  盖世英雄少女心
    2020-12-16 22:11

    Implement your own similarity class and override idf(Term, Searcher) method. In this method, you can return the score as follows. if (term.field.equals("field1") {

        if (term.field.equals("field1") {
            score = 0.5 * Integer.parseInt(term.text());
        } else if (term.field.equals("field2") {
            score = 1.4 * Integer.parseInt(term.text());
        } // and so on
        return score;
    

    When you execute the query, make sure it is on all the fields. That is query should look like

    field1:term field2:term field3:term

    The final score will also add some weights based on the query normalization. But, that will not affect the relative ranking of the documents as per the equation given by you.

提交回复
热议问题