diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 999063704de..564e3b71768 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -540,6 +540,9 @@ Bug fixes * LUCENE-3412: SloppyPhraseScorer was returning non-deterministic results for queries with many repeats (Doron Cohen) +* LUCENE-3421: PayloadTermQuery's explain was wrong when includeSpanScore=false. + (Edward Drapkin via Robert Muir) + ======================= Lucene 3.4.0 ======================= Bug fixes diff --git a/lucene/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java b/lucene/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java index 1a32ff0a25d..0df4220a4a8 100644 --- a/lucene/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java +++ b/lucene/src/java/org/apache/lucene/search/payloads/PayloadTermQuery.java @@ -194,11 +194,17 @@ public class PayloadTermQuery extends SpanTermQuery { payloadExpl.setValue(scorer.getPayloadScore()); // combined ComplexExplanation result = new ComplexExplanation(); - result.addDetail(expl); - result.addDetail(payloadExpl); - result.setValue(expl.getValue() * payloadExpl.getValue()); - result.setDescription("btq, product of:"); - result.setMatch(expl.getValue() == 0 ? Boolean.FALSE : Boolean.TRUE); // LUCENE-1303 + if (includeSpanScore) { + result.addDetail(expl); + result.addDetail(payloadExpl); + result.setValue(expl.getValue() * payloadExpl.getValue()); + result.setDescription("btq, product of:"); + } else { + result.addDetail(payloadExpl); + result.setValue(payloadExpl.getValue()); + result.setDescription("btq(includeSpanScore=false), result of:"); + } + result.setMatch(true); // LUCENE-1303 return result; } } diff --git a/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadExplanations.java b/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadExplanations.java new file mode 100644 index 00000000000..bc64e05e306 --- /dev/null +++ b/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadExplanations.java @@ -0,0 +1,98 @@ +package org.apache.lucene.search.payloads; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.index.Term; +import org.apache.lucene.search.DefaultSimilarity; +import org.apache.lucene.search.DefaultSimilarityProvider; +import org.apache.lucene.search.Similarity; +import org.apache.lucene.search.TestExplanations; +import org.apache.lucene.search.spans.SpanQuery; +import org.apache.lucene.util.BytesRef; + +/** + * TestExplanations subclass focusing on payload queries + */ +public class TestPayloadExplanations extends TestExplanations { + private PayloadFunction functions[] = new PayloadFunction[] { + new AveragePayloadFunction(), + new MinPayloadFunction(), + new MaxPayloadFunction(), + }; + + @Override + public void setUp() throws Exception { + super.setUp(); + searcher.setSimilarityProvider(new DefaultSimilarityProvider() { + @Override + public Similarity get(String field) { + return new DefaultSimilarity() { + @Override + public float scorePayload(int doc, int start, int end, BytesRef payload) { + return 1 + (payload.hashCode() % 10); + } + }; + } + }); + } + + /** macro for payloadtermquery */ + private SpanQuery pt(String s, PayloadFunction fn, boolean includeSpanScore) { + return new PayloadTermQuery(new Term(FIELD,s), fn, includeSpanScore); + } + + /* simple PayloadTermQueries */ + + public void testPT1() throws Exception { + for (PayloadFunction fn : functions) { + qtest(pt("w1", fn, false), new int[] {0,1,2,3}); + qtest(pt("w1", fn, true), new int[] {0,1,2,3}); + } + } + + public void testPT2() throws Exception { + for (PayloadFunction fn : functions) { + SpanQuery q = pt("w1", fn, false); + q.setBoost(1000); + qtest(q, new int[] {0,1,2,3}); + q = pt("w1", fn, true); + q.setBoost(1000); + qtest(q, new int[] {0,1,2,3}); + } + } + + public void testPT4() throws Exception { + for (PayloadFunction fn : functions) { + qtest(pt("xx", fn, false), new int[] {2,3}); + qtest(pt("xx", fn, true), new int[] {2,3}); + } + } + + public void testPT5() throws Exception { + for (PayloadFunction fn : functions) { + SpanQuery q = pt("xx", fn, false); + q.setBoost(1000); + qtest(q, new int[] {2,3}); + q = pt("xx", fn, true); + q.setBoost(1000); + qtest(q, new int[] {2,3}); + } + } + + // TODO: test the payloadnear query too! +}