Lucene有效载荷得分

时间:2011-06-27 13:04:14

标签: lucene payload

我想弄清楚有效负载评分在lucene中是如何工作的。由于我不明白PayloadFunction适合哪里,我想我真的不明白它是如何工作的。尝试使用谷歌搜索,但除了通过来源的建议之外找不到多少。好吧,如果有人可以在这里解释它,那将是很好的,否则源代码是:)

1 个答案:

答案 0 :(得分:3)

它有三个部分。首先,您应该在分析期间生成有效负载。这可以使用PayloadAttribute完成。您只需要在分析期间将此属性添加到您想要的术语中。

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);
    }
}

然后在搜索过程中,您应该使用特殊查询类PayloadTermQuery。此类的行为与SpanTermQuery相同,但会跟踪索引中的有效负载。使用自定义Similarity实现,您可以对文档中的每个有效负载事件进行评分。

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;
    }
  }
}

最后,使用PayloadFunction您可以在文档上聚合有效负载分数以生成最终文档分数。