From 436175cfafbdd610b9d8f69f2f6e4de98fd5338c Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Wed, 25 Jul 2012 23:23:02 +0000 Subject: [PATCH] LUCENE-4250: fix trap in PayloadFunction explain API git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1365837 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 4 ++++ .../lucene/search/payloads/AveragePayloadFunction.java | 9 --------- .../lucene/search/payloads/MaxPayloadFunction.java | 9 --------- .../lucene/search/payloads/MinPayloadFunction.java | 9 --------- .../apache/lucene/search/payloads/PayloadFunction.java | 6 +++--- .../apache/lucene/search/payloads/PayloadNearQuery.java | 3 ++- .../apache/lucene/search/payloads/PayloadTermQuery.java | 4 +++- 7 files changed, 12 insertions(+), 32 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index bf7934174fa..671a8bcf1c4 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -63,6 +63,10 @@ API Changes at all. If you want to tweak things like positionIncrementGap and offsetGap, analyze the field with KeywordTokenizer instead. (Grant Ingersoll, Robert Muir) +* LUCENE-4250: Pass fieldName to the PayloadFunction explain method, so it + parallels with docScore and the default implementation is correct. + (Robert Muir) + Optimizations * LUCENE-4171: Performance improvements to Packed64. diff --git a/lucene/core/src/java/org/apache/lucene/search/payloads/AveragePayloadFunction.java b/lucene/core/src/java/org/apache/lucene/search/payloads/AveragePayloadFunction.java index 1f5e8a328b6..808c3c2f7de 100644 --- a/lucene/core/src/java/org/apache/lucene/search/payloads/AveragePayloadFunction.java +++ b/lucene/core/src/java/org/apache/lucene/search/payloads/AveragePayloadFunction.java @@ -1,6 +1,5 @@ package org.apache.lucene.search.payloads; -import org.apache.lucene.search.Explanation; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -36,14 +35,6 @@ public class AveragePayloadFunction extends PayloadFunction{ public float docScore(int docId, String field, int numPayloadsSeen, float payloadScore) { return numPayloadsSeen > 0 ? (payloadScore / numPayloadsSeen) : 1; } - @Override - public Explanation explain(int doc, int numPayloadsSeen, float payloadScore) { - Explanation payloadBoost = new Explanation(); - float avgPayloadScore = (numPayloadsSeen > 0 ? (payloadScore / numPayloadsSeen) : 1); - payloadBoost.setValue(avgPayloadScore); - payloadBoost.setDescription("AveragePayloadFunction(...)"); - return payloadBoost; - } @Override public int hashCode() { diff --git a/lucene/core/src/java/org/apache/lucene/search/payloads/MaxPayloadFunction.java b/lucene/core/src/java/org/apache/lucene/search/payloads/MaxPayloadFunction.java index c113fbca509..b868e3bbcca 100644 --- a/lucene/core/src/java/org/apache/lucene/search/payloads/MaxPayloadFunction.java +++ b/lucene/core/src/java/org/apache/lucene/search/payloads/MaxPayloadFunction.java @@ -1,6 +1,5 @@ package org.apache.lucene.search.payloads; -import org.apache.lucene.search.Explanation; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -40,14 +39,6 @@ public class MaxPayloadFunction extends PayloadFunction { return numPayloadsSeen > 0 ? payloadScore : 1; } - @Override - public Explanation explain(int doc, int numPayloadsSeen, float payloadScore) { - Explanation expl = new Explanation(); - float maxPayloadScore = (numPayloadsSeen > 0 ? payloadScore : 1); - expl.setValue(maxPayloadScore); - expl.setDescription("MaxPayloadFunction(...)"); - return expl; - } @Override public int hashCode() { final int prime = 31; diff --git a/lucene/core/src/java/org/apache/lucene/search/payloads/MinPayloadFunction.java b/lucene/core/src/java/org/apache/lucene/search/payloads/MinPayloadFunction.java index 40cbbb01a2d..789ceaa8a9f 100644 --- a/lucene/core/src/java/org/apache/lucene/search/payloads/MinPayloadFunction.java +++ b/lucene/core/src/java/org/apache/lucene/search/payloads/MinPayloadFunction.java @@ -1,6 +1,5 @@ package org.apache.lucene.search.payloads; -import org.apache.lucene.search.Explanation; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -38,14 +37,6 @@ public class MinPayloadFunction extends PayloadFunction { return numPayloadsSeen > 0 ? payloadScore : 1; } - @Override - public Explanation explain(int doc, int numPayloadsSeen, float payloadScore) { - Explanation expl = new Explanation(); - float minPayloadScore = (numPayloadsSeen > 0 ? payloadScore : 1); - expl.setValue(minPayloadScore); - expl.setDescription("MinPayloadFunction(...)"); - return expl; - } @Override public int hashCode() { final int prime = 31; diff --git a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadFunction.java b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadFunction.java index cb34a0b6262..976ed09f2d9 100644 --- a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadFunction.java +++ b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadFunction.java @@ -55,10 +55,10 @@ public abstract class PayloadFunction { */ public abstract float docScore(int docId, String field, int numPayloadsSeen, float payloadScore); - public Explanation explain(int docId, int numPayloadsSeen, float payloadScore){ + public Explanation explain(int docId, String field, int numPayloadsSeen, float payloadScore){ Explanation result = new Explanation(); - result.setDescription("Unimpl Payload Function Explain"); - result.setValue(1); + result.setDescription(getClass().getSimpleName() + ".docScore()"); + result.setValue(docScore(docId, field, numPayloadsSeen, payloadScore)); return result; }; diff --git a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java index fe1433b97fa..5a94f0886d7 100644 --- a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadNearQuery.java @@ -167,8 +167,9 @@ public class PayloadNearQuery extends SpanNearQuery { Explanation scoreExplanation = docScorer.explain(doc, new Explanation(freq, "phraseFreq=" + freq)); expl.addDetail(scoreExplanation); expl.setValue(scoreExplanation.getValue()); + String field = ((SpanQuery)getQuery()).getField(); // now the payloads part - Explanation payloadExpl = function.explain(doc, scorer.payloadsSeen, scorer.payloadScore); + Explanation payloadExpl = function.explain(doc, field, scorer.payloadsSeen, scorer.payloadScore); // combined ComplexExplanation result = new ComplexExplanation(); result.addDetail(expl); diff --git a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java index 30d5bacfbcf..1ac4dd4a94f 100644 --- a/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java @@ -28,6 +28,7 @@ import org.apache.lucene.search.ComplexExplanation; import org.apache.lucene.search.similarities.DefaultSimilarity; import org.apache.lucene.search.similarities.Similarity; import org.apache.lucene.search.similarities.Similarity.SloppySimScorer; +import org.apache.lucene.search.spans.SpanQuery; import org.apache.lucene.search.spans.TermSpans; import org.apache.lucene.search.spans.SpanTermQuery; import org.apache.lucene.search.spans.SpanWeight; @@ -190,7 +191,8 @@ public class PayloadTermQuery extends SpanTermQuery { // whether to load the payload or not // GSI: I suppose we could toString the payload, but I don't think that // would be a good idea - Explanation payloadExpl = function.explain(doc, scorer.payloadsSeen, scorer.payloadScore); + String field = ((SpanQuery)getQuery()).getField(); + Explanation payloadExpl = function.explain(doc, field, scorer.payloadsSeen, scorer.payloadScore); payloadExpl.setValue(scorer.getPayloadScore()); // combined ComplexExplanation result = new ComplexExplanation();