开发者

Lucene payload scoring

开发者 https://www.devze.com 2023-03-15 12:28 出处:网络
I开发者_StackOverflow want to figure out how payload scoring works in lucene. Since I don\'t understand where PayloadFunction fits in, I think I don\'t really understand how it works. Tried googling f

I开发者_StackOverflow want to figure out how payload scoring works in lucene. Since I don't understand where PayloadFunction fits in, I think I don't really understand how it works. Tried googling for it, but couldn't find much apart from advice to go through source. Well, it would be nice if someone can explain it here, else source code it is :)


There are three parts of it. First of all you should generate payloads during analysis. This could be done using PayloadAttribute. You just need to add this attribute to terms you want during analysis.

class MyFilter extends TokenFilter {

  private PayloadAttribute attr;

  public MyFilter() {
    attr = addAttribute(PayloadAttribute.class);
  }

  public final boolean incrementToken() throws IOException {
    if (input.incrementToken()) {
      Payload p = new Payload(PayloadHelper.encodeFloat(42));
      attr.setPayload(p);
    } else {
      attr.setPayload(null);
    }
}

Then during searching you should use special query class PayloadTermQuery. This class behaves as SpanTermQuery but do track of payloads in index. Using custom Similarity implementation you could score each payload occurrence in document.

public class MySimilarity extends DefaultSimilarity {

  public float scorePayload(int docID, String fieldName,
                            int start, int end, byte[] payload,
                            int offset, int length) {
    if (payload != null) {
      return PayloadHelper.decodeFloat(payload, offset);
    } else {
      return 1.0f;
    }
  }
}

Finally, using PayloadFunction you could aggregate payload scores over document to produce final document score.

0

精彩评论

暂无评论...
验证码 换一张
取 消