You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@lucene.apache.org by ho...@apache.org on 2006/06/20 03:13:14 UTC
svn commit: r415476 - in /lucene/java/trunk: ./
src/java/org/apache/lucene/search/ src/test/org/apache/lucene/search/
Author: hossman
Date: Mon Jun 19 18:13:13 2006
New Revision: 415476
URL: http://svn.apache.org/viewvc?rev=415476&view=rev
Log:
LUCENE-557: Lots of new Explanation test cases (and utilities for writing future test cases) as well as fixes for blatent bugs in the Explanation methods of BooleanQuery and FilteredQuery
Added:
lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanations.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanationsOfNonMatches.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestExplanations.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanations.java
lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanationsOfNonMatches.java
Modified:
lucene/java/trunk/CHANGES.txt
lucene/java/trunk/src/java/org/apache/lucene/search/BooleanQuery.java
lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java
lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java
Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=415476&r1=415475&r2=415476&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Mon Jun 19 18:13:13 2006
@@ -40,7 +40,11 @@
6. LUCENE-601: RAMDirectory and RAMFile made Serializable
(Karl Wettin via Otis Gospodnetic)
-
+
+ 7. LUCENE-557: Fixes to BooleanQuery and FilteredQuery so that the score
+ Explanations match up with the real scores.
+ (Chris Hostetter)
+
Optimizations
1. LUCENE-586: TermDocs.skipTo() is now more efficient for multi-segment
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/BooleanQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/BooleanQuery.java?rev=415476&r1=415475&r2=415476&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/BooleanQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/BooleanQuery.java Mon Jun 19 18:13:13 2006
@@ -18,6 +18,7 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
+import org.apache.lucene.search.BooleanClause.Occur;
import java.io.IOException;
import java.util.Iterator;
@@ -188,8 +189,11 @@
for (int i = 0 ; i < weights.size(); i++) {
BooleanClause c = (BooleanClause)clauses.elementAt(i);
Weight w = (Weight)weights.elementAt(i);
+ // call sumOfSquaredWeights for all clauses in case of side effects
+ float s = w.sumOfSquaredWeights(); // sum sub weights
if (!c.isProhibited())
- sum += w.sumOfSquaredWeights(); // sum sub weights
+ // only add to sum for non-prohibited clauses
+ sum += s;
}
sum *= getBoost() * getBoost(); // boost each sub-weight
@@ -203,8 +207,8 @@
for (int i = 0 ; i < weights.size(); i++) {
BooleanClause c = (BooleanClause)clauses.elementAt(i);
Weight w = (Weight)weights.elementAt(i);
- if (!c.isProhibited())
- w.normalize(norm);
+ // normalize all clauses, (even if prohibited in case of side affects)
+ w.normalize(norm);
}
}
@@ -257,11 +261,15 @@
public Explanation explain(IndexReader reader, int doc)
throws IOException {
+ final int minShouldMatch =
+ BooleanQuery.this.getMinimumNumberShouldMatch();
Explanation sumExpl = new Explanation();
sumExpl.setDescription("sum of:");
int coord = 0;
int maxCoord = 0;
float sum = 0.0f;
+ boolean fail = false;
+ int shouldMatchCount = 0;
for (int i = 0 ; i < weights.size(); i++) {
BooleanClause c = (BooleanClause)clauses.elementAt(i);
Weight w = (Weight)weights.elementAt(i);
@@ -273,17 +281,35 @@
sum += e.getValue();
coord++;
} else {
- return new Explanation(0.0f, "match prohibited");
+ Explanation r =
+ new Explanation(0.0f, "match on prohibited clause");
+ r.addDetail(e);
+ sumExpl.addDetail(r);
+ fail = true;
}
+ if (c.getOccur().equals(Occur.SHOULD))
+ shouldMatchCount++;
} else if (c.isRequired()) {
- return new Explanation(0.0f, "match required");
+ Explanation r = new Explanation(0.0f, "no match on required clause");
+ r.addDetail(e);
+ sumExpl.addDetail(r);
+ fail = true;
}
}
+ if (fail) {
+ sumExpl.setValue(0.0f);
+ sumExpl.setDescription
+ ("Failure to meet condition(s) of required/prohibited clause(s)");
+ return sumExpl;
+ } else if (shouldMatchCount < minShouldMatch) {
+ sumExpl.setValue(0.0f);
+ sumExpl.setDescription("Failure to match minimum number "+
+ "of optional clauses: " + minShouldMatch);
+ return sumExpl;
+ }
+
sumExpl.setValue(sum);
-
- if (coord == 1) // only one clause matched
- sumExpl = sumExpl.getDetails()[0]; // eliminate wrapper
-
+
float coordFactor = similarity.coord(coord, maxCoord);
if (coordFactor == 1.0f) // coord is no-op
return sumExpl; // eliminate wrapper
Modified: lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java?rev=415476&r1=415475&r2=415476&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/FilteredQuery.java Mon Jun 19 18:13:13 2006
@@ -70,7 +70,17 @@
public float getValue() { return weight.getValue(); }
public float sumOfSquaredWeights() throws IOException { return weight.sumOfSquaredWeights(); }
public void normalize (float v) { weight.normalize(v); }
- public Explanation explain (IndexReader ir, int i) throws IOException { return weight.explain (ir, i); }
+ public Explanation explain (IndexReader ir, int i) throws IOException {
+ Explanation inner = weight.explain (ir, i);
+ Filter f = FilteredQuery.this.filter;
+ BitSet matches = f.bits(ir);
+ if (matches.get(i))
+ return inner;
+ Explanation result = new Explanation
+ (0.0f, "failure to match filter: " + f.toString());
+ result.addDetail(inner);
+ return result;
+ }
// return this query
public Query getQuery() { return FilteredQuery.this; }
Modified: lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java?rev=415476&r1=415475&r2=415476&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/CheckHits.java Mon Jun 19 18:13:13 2006
@@ -16,6 +16,9 @@
* limitations under the License.
*/
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.index.IndexReader;
+
import junit.framework.TestCase;
import java.io.IOException;
@@ -23,7 +26,43 @@
import java.util.TreeSet;
public class CheckHits {
- /** Tests that a query has expected document number results.
+
+ /**
+ * Tests that all documents up to maxDoc which are *not* in the
+ * expected result set, have an explanation which indicates no match
+ * (ie: Explanation value of 0.0f)
+ */
+ public static void checkNoMatchExplanations(Query q, String defaultFieldName,
+ Searcher searcher, int[] results)
+ throws IOException {
+
+ String d = q.toString(defaultFieldName);
+ Set ignore = new TreeSet();
+ for (int i = 0; i < results.length; i++) {
+ ignore.add(new Integer(results[i]));
+ }
+
+ int maxDoc = searcher.maxDoc();
+ for (int doc = 0; doc < maxDoc; doc++) {
+ if (ignore.contains(new Integer(doc))) continue;
+
+ Explanation exp = searcher.explain(q, doc);
+ TestCase.assertNotNull("Explanation of [["+d+"]] for #"+doc+" is null",
+ exp);
+ TestCase.assertEquals("Explanation of [["+d+"]] for #"+doc+
+ " doesn't indicate non-match: " + exp.toString(),
+ 0.0f, exp.getValue(), 0.0f);
+ }
+
+ }
+
+ /**
+ * Tests that a query matches the an expected set of documents
+ *
+ * @param query the query to test
+ * @param searcher the searcher to test the query against
+ * @param defaultFieldName used for displaing the query in assertion messages
+ * @param results a list of documentIds that must match the query
*/
public static void checkHits(
Query query,
@@ -138,6 +177,121 @@
return sb.toString();
}
+ /**
+ * Asserts that the score explanation for every document matching a
+ * query corrisponds with the true score.
+ *
+ * @see ExplanationAsserter
+ * @param query the query to test
+ * @param searcher the searcher to test the query against
+ * @param defaultFieldName used for displaing the query in assertion messages
+ */
+ public static void checkExplanations(Query query,
+ String defaultFieldName,
+ Searcher searcher) throws IOException {
+
+ searcher.search(query,
+ new ExplanationAsserter
+ (query, defaultFieldName, searcher));
+
+ }
+
+ /**
+ * an IndexSearcher that implicitly checks hte explanation of every match
+ * whenever it executes a search
+ */
+ public static class ExplanationAssertingSearcher extends IndexSearcher {
+ public ExplanationAssertingSearcher(Directory d) throws IOException {
+ super(d);
+ }
+ public ExplanationAssertingSearcher(IndexReader r) throws IOException {
+ super(r);
+ }
+ protected void checkExplanations(Query q) throws IOException {
+ super.search(q, null,
+ new ExplanationAsserter
+ (q, null, this));
+ }
+ public Hits search(Query query, Filter filter) throws IOException {
+ checkExplanations(query);
+ return super.search(query,filter);
+ }
+ public Hits search(Query query, Sort sort) throws IOException {
+ checkExplanations(query);
+ return super.search(query,sort);
+ }
+ public Hits search(Query query, Filter filter,
+ Sort sort) throws IOException {
+ checkExplanations(query);
+ return super.search(query,filter,sort);
+ }
+ public TopFieldDocs search(Query query,
+ Filter filter,
+ int n,
+ Sort sort) throws IOException {
+
+ checkExplanations(query);
+ return super.search(query,filter,n,sort);
+ }
+ public void search(Query query, HitCollector results) throws IOException {
+ checkExplanations(query);
+ super.search(query,results);
+ }
+ public void search(Query query, Filter filter,
+ HitCollector results) throws IOException {
+ checkExplanations(query);
+ super.search(query,filter, results);
+ }
+ public TopDocs search(Query query, Filter filter,
+ int n) throws IOException {
+
+ checkExplanations(query);
+ return super.search(query,filter, n);
+ }
+ }
+
+ /**
+ * Asserts that the score explanation for every document matching a
+ * query corrisponds with the true score.
+ *
+ * NOTE: this HitCollector should only be used with the Query and Searcher
+ * specified at when it is constructed.
+ */
+ public static class ExplanationAsserter extends HitCollector {
+
+ /**
+ * Some explains methods calculate their vlaues though a slightly
+ * differnet order of operations from the acctaul scoring method ...
+ * this allows for a small amount of variation
+ */
+ public static float SCORE_TOLERANCE_DELTA = 0.00005f;
+
+ Query q;
+ Searcher s;
+ String d;
+ public ExplanationAsserter(Query q, String defaultFieldName, Searcher s) {
+ this.q=q;
+ this.s=s;
+ this.d = q.toString(defaultFieldName);
+ }
+ public void collect(int doc, float score) {
+ Explanation exp = null;
+
+ try {
+ exp = s.explain(q, doc);
+ } catch (IOException e) {
+ throw new RuntimeException
+ ("exception in hitcollector of [["+d+"]] for #"+doc, e);
+ }
+
+ TestCase.assertNotNull("Explanation of [["+d+"]] for #"+doc+" is null",
+ exp);
+ TestCase.assertEquals("Score of [["+d+"]] for #"+doc+
+ " does not match explanation: " + exp.toString(),
+ score, exp.getValue(), SCORE_TOLERANCE_DELTA);
+ }
+
+ }
}
Added: lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanations.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanations.java?rev=415476&view=auto
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanations.java (added)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanations.java Mon Jun 19 18:13:13 2006
@@ -0,0 +1,97 @@
+package org.apache.lucene.search;
+
+/**
+ * Copyright 2006 Apache Software Foundation
+ *
+ * Licensed 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.search.BooleanClause.Occur;
+import org.apache.lucene.store.RAMDirectory;
+
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+
+import org.apache.lucene.analysis.WhitespaceAnalyzer;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.queryParser.ParseException;
+
+import junit.framework.TestCase;
+
+import java.util.Random;
+import java.util.BitSet;
+
+/**
+ * TestExplanations subclass that builds up super crazy complex queries
+ * on the assumption that if the explanations work out right for them,
+ * they should work for anything.
+ */
+public class TestComplexExplanations extends TestExplanations {
+
+ public void test1() throws Exception {
+
+ BooleanQuery q = new BooleanQuery();
+
+ q.add(qp.parse("\"w1 w2\"~1"), Occur.MUST);
+ q.add(snear(st("w2"),
+ sor("w5","zz"),
+ 4, true),
+ Occur.SHOULD);
+ q.add(snear(sf("w3",2), st("w2"), st("w3"), 5, true),
+ Occur.SHOULD);
+
+ Query t = new FilteredQuery(qp.parse("xx"),
+ new ItemizedFilter(new int[] {1,3}));
+ t.setBoost(1000);
+ q.add(t, Occur.SHOULD);
+
+ t = new ConstantScoreQuery(new ItemizedFilter(new int[] {0,2}));
+ t.setBoost(30);
+ q.add(t, Occur.SHOULD);
+
+ DisjunctionMaxQuery dm = new DisjunctionMaxQuery(0.2f);
+ dm.add(snear(st("w2"),
+ sor("w5","zz"),
+ 4, true));
+ dm.add(qp.parse("QQ"));
+ dm.add(qp.parse("xx yy -zz"));
+ dm.add(qp.parse("-xx -w1"));
+
+ DisjunctionMaxQuery dm2 = new DisjunctionMaxQuery(0.5f);
+ dm2.add(qp.parse("w1"));
+ dm2.add(qp.parse("w2"));
+ dm2.add(qp.parse("w3"));
+ dm.add(dm2);
+
+ q.add(dm, Occur.SHOULD);
+
+ BooleanQuery b = new BooleanQuery();
+ b.setMinimumNumberShouldMatch(2);
+ b.add(snear("w1","w2",1,true), Occur.SHOULD);
+ b.add(snear("w2","w3",1,true), Occur.SHOULD);
+ b.add(snear("w1","w3",3,true), Occur.SHOULD);
+
+ q.add(b, Occur.SHOULD);
+
+ qtest(q, new int[] { 0,1,2 });
+
+ }
+
+ // :TODO: we really need more crazy complex cases.
+
+}
Added: lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanationsOfNonMatches.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanationsOfNonMatches.java?rev=415476&view=auto
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanationsOfNonMatches.java (added)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestComplexExplanationsOfNonMatches.java Mon Jun 19 18:13:13 2006
@@ -0,0 +1,54 @@
+package org.apache.lucene.search;
+
+/**
+ * Copyright 2006 Apache Software Foundation
+ *
+ * Licensed 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.store.RAMDirectory;
+
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+
+import org.apache.lucene.analysis.WhitespaceAnalyzer;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.queryParser.ParseException;
+
+import junit.framework.TestCase;
+
+import java.util.Random;
+import java.util.BitSet;
+
+/**
+ * subclass of TestSimpleExplanations that verifies non matches.
+ */
+public class TestComplexExplanationsOfNonMatches
+ extends TestComplexExplanations {
+
+ /**
+ * Overrides superclass to ignore matches and focus on non-matches
+ *
+ * @see CheckHits#checkNoMatchExplanations
+ */
+ public void qtest(Query q, int[] expDocNrs) throws Exception {
+ CheckHits.checkNoMatchExplanations(q, FIELD, searcher, expDocNrs);
+ }
+
+}
Added: lucene/java/trunk/src/test/org/apache/lucene/search/TestExplanations.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestExplanations.java?rev=415476&view=auto
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestExplanations.java (added)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestExplanations.java Mon Jun 19 18:13:13 2006
@@ -0,0 +1,236 @@
+package org.apache.lucene.search;
+
+/**
+ * Copyright 2006 Apache Software Foundation
+ *
+ * Licensed 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.search.spans.*;
+import org.apache.lucene.store.RAMDirectory;
+
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+
+import org.apache.lucene.analysis.WhitespaceAnalyzer;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.queryParser.ParseException;
+
+import junit.framework.TestCase;
+
+import java.util.Random;
+import java.util.BitSet;
+
+/**
+ * Tests primative queries (ie: that rewrite to themselves) to
+ * insure they match the expected set of docs, and that the score of each
+ * match is equal to the value of the scores explanation.
+ *
+ * <p>
+ * The assumption is that if all of the "primative" queries work well,
+ * then anythingthat rewrites to a primative will work well also.
+ * </p>
+ *
+ * @see "Subclasses for actual tests"
+ */
+public class TestExplanations extends TestCase {
+ protected IndexSearcher searcher;
+
+ public static final String FIELD = "field";
+ public static final QueryParser qp =
+ new QueryParser(FIELD, new WhitespaceAnalyzer());
+
+ public void tearDown() throws Exception {
+ searcher.close();
+ }
+
+ public void setUp() throws Exception {
+ RAMDirectory directory = new RAMDirectory();
+ IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true);
+ for (int i = 0; i < docFields.length; i++) {
+ Document doc = new Document();
+ doc.add(new Field(FIELD, docFields[i], Field.Store.NO, Field.Index.TOKENIZED));
+ writer.addDocument(doc);
+ }
+ writer.close();
+ searcher = new CheckHits.ExplanationAssertingSearcher(directory);
+ //searcher = new IndexSearcher(directory);
+ }
+
+ protected String[] docFields = {
+ "w1 w2 w3 w4 w5",
+ "w1 w3 w2 w3 zz",
+ "w1 xx w2 yy w3",
+ "w1 w3 xx w2 yy w3 zz"
+ };
+
+ public Query makeQuery(String queryText) throws ParseException {
+ return qp.parse(queryText);
+ }
+
+ public void qtest(String queryText, int[] expDocNrs) throws Exception {
+ qtest(makeQuery(queryText), expDocNrs);
+ }
+ public void qtest(Query q, int[] expDocNrs) throws Exception {
+ CheckHits.checkHits(q, FIELD, searcher, expDocNrs);
+ }
+
+ /**
+ * Tests a query using qtest after wrapping it with both optB and reqB
+ * @see #qtest
+ * @see #reqB
+ * @see #optB
+ */
+ public void bqtest(Query q, int[] expDocNrs) throws Exception {
+ qtest(reqB(q), expDocNrs);
+ qtest(optB(q), expDocNrs);
+ }
+ /**
+ * Tests a query using qtest after wrapping it with both optB and reqB
+ * @see #qtest
+ * @see #reqB
+ * @see #optB
+ */
+ public void bqtest(String queryText, int[] expDocNrs) throws Exception {
+ bqtest(makeQuery(queryText), expDocNrs);
+ }
+
+ /** A filter that only lets the specified document numbers pass */
+ public static class ItemizedFilter extends Filter {
+ int[] docs;
+ public ItemizedFilter(int[] docs) {
+ this.docs = docs;
+ }
+ public BitSet bits(IndexReader r) {
+ BitSet b = new BitSet(r.maxDoc());
+ for (int i = 0; i < docs.length; i++) {
+ b.set(docs[i]);
+ }
+ return b;
+ }
+ }
+
+ /** helper for generating MultiPhraseQueries */
+ public static Term[] ta(String[] s) {
+ Term[] t = new Term[s.length];
+ for (int i = 0; i < s.length; i++) {
+ t[i] = new Term(FIELD, s[i]);
+ }
+ return t;
+ }
+
+ /** MACRO for SpanTermQuery */
+ public SpanTermQuery st(String s) {
+ return new SpanTermQuery(new Term(FIELD,s));
+ }
+
+ /** MACRO for SpanNotQuery */
+ public SpanNotQuery snot(SpanQuery i, SpanQuery e) {
+ return new SpanNotQuery(i,e);
+ }
+
+ /** MACRO for SpanOrQuery containing two SpanTerm queries */
+ public SpanOrQuery sor(String s, String e) {
+ return sor(st(s), st(e));
+ }
+ /** MACRO for SpanOrQuery containing two SpanQueries */
+ public SpanOrQuery sor(SpanQuery s, SpanQuery e) {
+ return new SpanOrQuery(new SpanQuery[] { s, e });
+ }
+
+ /** MACRO for SpanOrQuery containing three SpanTerm queries */
+ public SpanOrQuery sor(String s, String m, String e) {
+ return sor(st(s), st(m), st(e));
+ }
+ /** MACRO for SpanOrQuery containing two SpanQueries */
+ public SpanOrQuery sor(SpanQuery s, SpanQuery m, SpanQuery e) {
+ return new SpanOrQuery(new SpanQuery[] { s, m, e });
+ }
+
+ /** MACRO for SpanNearQuery containing two SpanTerm queries */
+ public SpanNearQuery snear(String s, String e, int slop, boolean inOrder) {
+ return snear(st(s), st(e), slop, inOrder);
+ }
+ /** MACRO for SpanNearQuery containing two SpanQueries */
+ public SpanNearQuery snear(SpanQuery s, SpanQuery e,
+ int slop, boolean inOrder) {
+ return new SpanNearQuery(new SpanQuery[] { s, e }, slop, inOrder);
+ }
+
+
+ /** MACRO for SpanNearQuery containing three SpanTerm queries */
+ public SpanNearQuery snear(String s, String m, String e,
+ int slop, boolean inOrder) {
+ return snear(st(s), st(m), st(e), slop, inOrder);
+ }
+ /** MACRO for SpanNearQuery containing three SpanQueries */
+ public SpanNearQuery snear(SpanQuery s, SpanQuery m, SpanQuery e,
+ int slop, boolean inOrder) {
+ return new SpanNearQuery(new SpanQuery[] { s, m, e }, slop, inOrder);
+ }
+
+ /** MACRO for SpanFirst(SpanTermQuery) */
+ public SpanFirstQuery sf(String s, int b) {
+ return new SpanFirstQuery(st(s), b);
+ }
+
+ /**
+ * MACRO: Wraps a Query in a BooleanQuery so that it is optional, along
+ * with a second clause which will never match anything
+ */
+ public Query optB(String q) throws Exception {
+ return optB(makeQuery(q));
+ }
+ /**
+ * MACRO: Wraps a Query in a BooleanQuery so that it is optional, along
+ * with a second clause which will never match anything
+ */
+ public Query optB(Query q) throws Exception {
+ return buildWrappingB(q, BooleanClause.Occur.SHOULD);
+ }
+
+ /**
+ * MACRO: Wraps a Query in a BooleanQuery so that it is required, along
+ * with a second clause which will never match anything
+ */
+ public Query reqB(String q) throws Exception {
+ return reqB(makeQuery(q));
+ }
+ /**
+ * MACRO: Wraps a Query in a BooleanQuery so that it is required, along
+ * with a second clause which will never match anything
+ */
+ public Query reqB(Query q) throws Exception {
+ return buildWrappingB(q, BooleanClause.Occur.MUST);
+ }
+
+ private Query buildWrappingB(Query q, BooleanClause.Occur o) {
+ BooleanQuery bq = new BooleanQuery(true);
+ bq.add(q, o);
+ bq.add(new TermQuery(new Term("NEVER","MATCH")), BooleanClause.Occur.MUST_NOT);
+ return bq;
+ }
+
+ /**
+ * Placeholder: JUnit freaks if you don't have one test ... making
+ * class abstract doesn't help
+ */
+ public void testNoop() {
+ /* NOOP */
+ }
+}
Added: lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanations.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanations.java?rev=415476&view=auto
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanations.java (added)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanations.java Mon Jun 19 18:13:13 2006
@@ -0,0 +1,319 @@
+package org.apache.lucene.search;
+
+/**
+ * Copyright 2006 Apache Software Foundation
+ *
+ * Licensed 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.store.RAMDirectory;
+
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+
+import org.apache.lucene.analysis.WhitespaceAnalyzer;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.queryParser.ParseException;
+
+import junit.framework.TestCase;
+
+import java.util.Random;
+import java.util.BitSet;
+
+/**
+ * TestExplanations subclass focusing on basic query types
+ */
+public class TestSimpleExplanations extends TestExplanations {
+
+ // we focus on queries that don't rewrite to other queries.
+ // if we get those covered well, then the ones that rewrite should
+ // also be covered.
+
+
+ /* simple term tests */
+
+ public void testT1() throws Exception {
+ qtest("w1", new int[] { 0,1,2,3 });
+ }
+ public void testT2() throws Exception {
+ qtest("w1^1000", new int[] { 0,1,2,3 });
+ }
+
+ /* MatchAllDocs */
+
+ public void testMA1() throws Exception {
+ qtest(new MatchAllDocsQuery(), new int[] { 0,1,2,3 });
+ }
+ public void testMA2() throws Exception {
+ Query q=new MatchAllDocsQuery();
+ q.setBoost(1000);
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+
+ /* some simple phrase tests */
+
+ public void testP1() throws Exception {
+ qtest("\"w1 w2\"", new int[] { 0 });
+ }
+ public void testP2() throws Exception {
+ qtest("\"w1 w3\"", new int[] { 1,3 });
+ }
+ public void testP3() throws Exception {
+ qtest("\"w1 w2\"~1", new int[] { 0,1,2 });
+ }
+ public void testP4() throws Exception {
+ qtest("\"w2 w3\"~1", new int[] { 0,1,2,3 });
+ }
+ public void testP5() throws Exception {
+ qtest("\"w3 w2\"~1", new int[] { 1,3 });
+ }
+ public void testP6() throws Exception {
+ qtest("\"w3 w2\"~2", new int[] { 0,1,3 });
+ }
+ public void testP7() throws Exception {
+ qtest("\"w3 w2\"~3", new int[] { 0,1,2,3 });
+ }
+
+ /* some simple filtered query tests */
+
+ public void testFQ1() throws Exception {
+ qtest(new FilteredQuery(qp.parse("w1"),
+ new ItemizedFilter(new int[] {0,1,2,3})),
+ new int[] {0,1,2,3});
+ }
+ public void testFQ2() throws Exception {
+ qtest(new FilteredQuery(qp.parse("w1"),
+ new ItemizedFilter(new int[] {0,2,3})),
+ new int[] {0,2,3});
+ }
+ public void testFQ3() throws Exception {
+ qtest(new FilteredQuery(qp.parse("xx"),
+ new ItemizedFilter(new int[] {1,3})),
+ new int[] {3});
+ }
+ public void testFQ4() throws Exception {
+ qtest(new FilteredQuery(qp.parse("xx^1000"),
+ new ItemizedFilter(new int[] {1,3})),
+ new int[] {3});
+ }
+ public void testFQ6() throws Exception {
+ Query q = new FilteredQuery(qp.parse("xx"),
+ new ItemizedFilter(new int[] {1,3}));
+ q.setBoost(1000);
+ qtest(q, new int[] {3});
+ }
+ public void testFQ7() throws Exception {
+ Query q = new FilteredQuery(qp.parse("xx"),
+ new ItemizedFilter(new int[] {1,3}));
+ q.setBoost(0);
+ qtest(q, new int[] {3});
+ }
+
+ /* ConstantScoreQueries */
+
+ public void testCSQ1() throws Exception {
+ Query q = new ConstantScoreQuery(new ItemizedFilter(new int[] {0,1,2,3}));
+ qtest(q, new int[] {0,1,2,3});
+ }
+ public void testCSQ2() throws Exception {
+ Query q = new ConstantScoreQuery(new ItemizedFilter(new int[] {1,3}));
+ qtest(q, new int[] {1,3});
+ }
+ public void testCSQ3() throws Exception {
+ Query q = new ConstantScoreQuery(new ItemizedFilter(new int[] {0,2}));
+ q.setBoost(1000);
+ qtest(q, new int[] {0,2});
+ }
+
+ /* DisjunctionMaxQuery */
+
+ public void testDMQ1() throws Exception {
+ DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.0f);
+ q.add(qp.parse("w1"));
+ q.add(qp.parse("w5"));
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+ public void testDMQ2() throws Exception {
+ DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
+ q.add(qp.parse("w1"));
+ q.add(qp.parse("w5"));
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+ public void testDMQ3() throws Exception {
+ DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
+ q.add(qp.parse("QQ"));
+ q.add(qp.parse("w5"));
+ qtest(q, new int[] { 0 });
+ }
+ public void testDMQ4() throws Exception {
+ DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
+ q.add(qp.parse("QQ"));
+ q.add(qp.parse("xx"));
+ qtest(q, new int[] { 2,3 });
+ }
+ public void testDMQ5() throws Exception {
+ DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
+ q.add(qp.parse("yy -QQ"));
+ q.add(qp.parse("xx"));
+ qtest(q, new int[] { 2,3 });
+ }
+ public void testDMQ6() throws Exception {
+ DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
+ q.add(qp.parse("-yy w3"));
+ q.add(qp.parse("xx"));
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+ public void testDMQ7() throws Exception {
+ DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
+ q.add(qp.parse("-yy w3"));
+ q.add(qp.parse("w2"));
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+ public void testDMQ8() throws Exception {
+ DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
+ q.add(qp.parse("yy w5^100"));
+ q.add(qp.parse("xx^100000"));
+ qtest(q, new int[] { 0,2,3 });
+ }
+ public void testDMQ9() throws Exception {
+ DisjunctionMaxQuery q = new DisjunctionMaxQuery(0.5f);
+ q.add(qp.parse("yy w5^100"));
+ q.add(qp.parse("xx^0"));
+ qtest(q, new int[] { 0,2,3 });
+ }
+
+ /* MultiPhraseQuery */
+
+ public void testMPQ1() throws Exception {
+ MultiPhraseQuery q = new MultiPhraseQuery();
+ q.add(ta(new String[] {"w1"}));
+ q.add(ta(new String[] {"w2","w3", "xx"}));
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+ public void testMPQ2() throws Exception {
+ MultiPhraseQuery q = new MultiPhraseQuery();
+ q.add(ta(new String[] {"w1"}));
+ q.add(ta(new String[] {"w2","w3"}));
+ qtest(q, new int[] { 0,1,3 });
+ }
+ public void testMPQ3() throws Exception {
+ MultiPhraseQuery q = new MultiPhraseQuery();
+ q.add(ta(new String[] {"w1","xx"}));
+ q.add(ta(new String[] {"w2","w3"}));
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+ public void testMPQ4() throws Exception {
+ MultiPhraseQuery q = new MultiPhraseQuery();
+ q.add(ta(new String[] {"w1"}));
+ q.add(ta(new String[] {"w2"}));
+ qtest(q, new int[] { 0 });
+ }
+ public void testMPQ5() throws Exception {
+ MultiPhraseQuery q = new MultiPhraseQuery();
+ q.add(ta(new String[] {"w1"}));
+ q.add(ta(new String[] {"w2"}));
+ q.setSlop(1);
+ qtest(q, new int[] { 0,1,2 });
+ }
+ public void testMPQ6() throws Exception {
+ MultiPhraseQuery q = new MultiPhraseQuery();
+ q.add(ta(new String[] {"w1","w3"}));
+ q.add(ta(new String[] {"w2"}));
+ q.setSlop(1);
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+
+ /* some simple tests of boolean queries containing term queries */
+
+ public void testBQ1() throws Exception {
+ qtest("+w1 +w2", new int[] { 0,1,2,3 });
+ }
+ public void testBQ2() throws Exception {
+ qtest("+yy +w3", new int[] { 2,3 });
+ }
+ public void testBQ3() throws Exception {
+ qtest("yy +w3", new int[] { 0,1,2,3 });
+ }
+ public void testBQ4() throws Exception {
+ qtest("w1 (-xx w2)", new int[] { 0,1,2,3 });
+ }
+ public void testBQ5() throws Exception {
+ qtest("w1 (+qq w2)", new int[] { 0,1,2,3 });
+ }
+ public void testBQ6() throws Exception {
+ qtest("w1 -(-qq w5)", new int[] { 1,2,3 });
+ }
+ public void testBQ7() throws Exception {
+ qtest("+w1 +(qq (xx -w2) (+w3 +w4))", new int[] { 0 });
+ }
+ public void testBQ8() throws Exception {
+ qtest("+w1 (qq (xx -w2) (+w3 +w4))", new int[] { 0,1,2,3 });
+ }
+ public void testBQ9() throws Exception {
+ qtest("+w1 (qq (-xx w2) -(+w3 +w4))", new int[] { 0,1,2,3 });
+ }
+ public void testBQ10() throws Exception {
+ qtest("+w1 +(qq (-xx w2) -(+w3 +w4))", new int[] { 1 });
+ }
+ public void testBQ11() throws Exception {
+ qtest("w1 w2^1000.0", new int[] { 0,1,2,3 });
+ }
+ public void testBQ14() throws Exception {
+ BooleanQuery q = new BooleanQuery(true);
+ q.add(qp.parse("QQQQQ"), BooleanClause.Occur.SHOULD);
+ q.add(qp.parse("w1"), BooleanClause.Occur.SHOULD);
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+ public void testBQ15() throws Exception {
+ BooleanQuery q = new BooleanQuery(true);
+ q.add(qp.parse("QQQQQ"), BooleanClause.Occur.MUST_NOT);
+ q.add(qp.parse("w1"), BooleanClause.Occur.SHOULD);
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+ public void testBQ16() throws Exception {
+ BooleanQuery q = new BooleanQuery(true);
+ q.add(qp.parse("QQQQQ"), BooleanClause.Occur.SHOULD);
+ q.add(qp.parse("w1 -xx"), BooleanClause.Occur.SHOULD);
+ qtest(q, new int[] { 0,1 });
+ }
+ public void testBQ17() throws Exception {
+ BooleanQuery q = new BooleanQuery(true);
+ q.add(qp.parse("w2"), BooleanClause.Occur.SHOULD);
+ q.add(qp.parse("w1 -xx"), BooleanClause.Occur.SHOULD);
+ qtest(q, new int[] { 0,1,2,3 });
+ }
+ public void testBQ19() throws Exception {
+ qtest("-yy w3", new int[] { 0,1 });
+ }
+
+ public void testBQ20() throws Exception {
+ BooleanQuery q = new BooleanQuery();
+ q.setMinimumNumberShouldMatch(2);
+ q.add(qp.parse("QQQQQ"), BooleanClause.Occur.SHOULD);
+ q.add(qp.parse("yy"), BooleanClause.Occur.SHOULD);
+ q.add(qp.parse("zz"), BooleanClause.Occur.SHOULD);
+ q.add(qp.parse("w5"), BooleanClause.Occur.SHOULD);
+ q.add(qp.parse("w4"), BooleanClause.Occur.SHOULD);
+
+ qtest(q, new int[] { 0,3 });
+
+ }
+
+
+}
Added: lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanationsOfNonMatches.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanationsOfNonMatches.java?rev=415476&view=auto
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanationsOfNonMatches.java (added)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestSimpleExplanationsOfNonMatches.java Mon Jun 19 18:13:13 2006
@@ -0,0 +1,54 @@
+package org.apache.lucene.search;
+
+/**
+ * Copyright 2006 Apache Software Foundation
+ *
+ * Licensed 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.store.RAMDirectory;
+
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+
+import org.apache.lucene.analysis.WhitespaceAnalyzer;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.queryParser.ParseException;
+
+import junit.framework.TestCase;
+
+import java.util.Random;
+import java.util.BitSet;
+
+/**
+ * subclass of TestSimpleExplanations that verifies non matches.
+ */
+public class TestSimpleExplanationsOfNonMatches
+ extends TestSimpleExplanations {
+
+ /**
+ * Overrides superclass to ignore matches and focus on non-matches
+ *
+ * @see CheckHits#checkNoMatchExplanations
+ */
+ public void qtest(Query q, int[] expDocNrs) throws Exception {
+ CheckHits.checkNoMatchExplanations(q, FIELD, searcher, expDocNrs);
+ }
+
+}