Apache Solr open-source search software
LUCENE-4934: AssertingIndexSearcher should do basic QueryUtils/etc checks on every query
Robert Muire8279926a2d7
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 8cf32e91431..872e342ce56 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -299,6 +299,10 @@ Bug Fixesthe number of documents per chunk has been added to fix this issue.(Robert Muir, Adrien Grand)+* LUCENE-4934: Fix minor equals/hashcode problems in facet/DrillDownQuery,+ BoostingQuery, MoreLikeThisQuery, FuzzyLikeThisQuery, and block join queries.+ (Robert Muir, Uwe Schindler)+Documentation* LUCENE-4841: Added example SimpleSortedSetFacetsExample to show howdiff --git a/lucene/core/src/java/org/apache/lucene/index/Term.java b/lucene/core/src/java/org/apache/lucene/index/Term.javaindex db43e0c48ea..30d89126d50 100644--- a/lucene/core/src/java/org/apache/lucene/index/Term.java+++ b/lucene/core/src/java/org/apache/lucene/index/Term.java@@ -17,7 +17,13 @@ package org.apache.lucene.index;* limitations under the License.*/+import java.nio.ByteBuffer;+import java.nio.charset.CharacterCodingException;+import java.nio.charset.CharsetDecoder;+import java.nio.charset.CodingErrorAction;+import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.IOUtils;/**A Term represents a word from text. This is the unit of search. It is@@ -69,7 +75,23 @@ public final class Term implements Comparable<Term> {/** Returns the text of this term. In the case of words, this is simply thetext of the word. In the case of dates and other types, this is anencoding of the object as a string. */- public final String text() { return bytes.utf8ToString(); }+ public final String text() {+ return toString(bytes);+ }++ /** Returns human-readable form of the term text. If the term is not unicode,+ * the raw bytes will be printed instead. */+ public static final String toString(BytesRef termText) {+ // the term might not be text, but usually is. so we make a best effort+ CharsetDecoder decoder = IOUtils.CHARSET_UTF_8.newDecoder()+ .onMalformedInput(CodingErrorAction.REPORT)+ .onUnmappableCharacter(CodingErrorAction.REPORT);+ try {+ return decoder.decode(ByteBuffer.wrap(termText.bytes, termText.offset, termText.length)).toString();+ } catch (CharacterCodingException e) {+ return termText.toString();+ }+ }/** Returns the bytes of this term. */public final BytesRef bytes() { return bytes; }@@ -132,5 +154,5 @@ public final class Term implements Comparable<Term> {}@Override- public final String toString() { return field + ":" + bytes.utf8ToString(); }+ public final String toString() { return field + ":" + text(); }}diff --git a/lucene/core/src/java/org/apache/lucene/search/TermRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/TermRangeQuery.javaindex 895dd003fae..2f744ffd38f 100644--- a/lucene/core/src/java/org/apache/lucene/search/TermRangeQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/TermRangeQuery.java@@ -19,6 +19,7 @@ package org.apache.lucene.search;import java.io.IOException;+import org.apache.lucene.index.Term;import org.apache.lucene.index.Terms;import org.apache.lucene.index.TermsEnum;import org.apache.lucene.util.AttributeSource;@@ -122,9 +123,9 @@ public class TermRangeQuery extends MultiTermQuery {}buffer.append(includeLower ? '[' : '{');// TODO: all these toStrings for queries should just output the bytes, it might not be UTF-8!- buffer.append(lowerTerm != null ? ("*".equals(lowerTerm.utf8ToString()) ? "\\*" : lowerTerm.utf8ToString()) : "*");+ buffer.append(lowerTerm != null ? ("*".equals(Term.toString(lowerTerm)) ? "\\*" : Term.toString(lowerTerm)) : "*");buffer.append(" TO ");- buffer.append(upperTerm != null ? ("*".equals(upperTerm.utf8ToString()) ? "\\*" : upperTerm.utf8ToString()) : "*");+ buffer.append(upperTerm != null ? ("*".equals(Term.toString(upperTerm)) ? "\\*" : Term.toString(upperTerm)) : "*");buffer.append(includeUpper ? ']' : '}');buffer.append(ToStringUtils.boost(getBoost()));return buffer.toString();diff --git a/lucene/core/src/test/org/apache/lucene/index/TestBinaryTerms.java b/lucene/core/src/test/org/apache/lucene/index/TestBinaryTerms.javaindex 3930b29cf48..9953fc7a877 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestBinaryTerms.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestBinaryTerms.java@@ -69,4 +69,9 @@ public class TestBinaryTerms extends LuceneTestCase {ir.close();dir.close();}++ public void testToString() {+ Term term = new Term("foo", new BytesRef(new byte[] { (byte) 0xff, (byte) 0xfe }));+ assertEquals("foo:[ff fe]", term.toString());+ }}diff --git a/lucene/facet/src/java/org/apache/lucene/facet/search/DrillDownQuery.java b/lucene/facet/src/java/org/apache/lucene/facet/search/DrillDownQuery.javaindex 235a1b47046..6d6adbe0d72 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/search/DrillDownQuery.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/search/DrillDownQuery.java@@ -181,7 +181,9 @@ public final class DrillDownQuery extends Query {@Overridepublic int hashCode() {- return query.hashCode();+ final int prime = 31;+ int result = super.hashCode();+ return prime * result + query.hashCode();}@Override@@ -191,7 +193,7 @@ public final class DrillDownQuery extends Query {}DrillDownQuery other = (DrillDownQuery) obj;- return query.equals(other.query);+ return query.equals(other.query) && super.equals(other);}@Overridediff --git a/lucene/facet/src/java/org/apache/lucene/facet/search/DrillSidewaysQuery.java b/lucene/facet/src/java/org/apache/lucene/facet/search/DrillSidewaysQuery.javaindex 80b12a693d0..a3f3de01751 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/search/DrillSidewaysQuery.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/search/DrillSidewaysQuery.java@@ -157,13 +157,34 @@ class DrillSidewaysQuery extends Query {};}+ // TODO: these should do "deeper" equals/hash on the 2-D drillDownTerms array+@Overridepublic int hashCode() {- throw new UnsupportedOperationException();+ final int prime = 31;+ int result = super.hashCode();+ result = prime * result + ((baseQuery == null) ? 0 : baseQuery.hashCode());+ result = prime * result+ + ((drillDownCollector == null) ? 0 : drillDownCollector.hashCode());+ result = prime * result + Arrays.hashCode(drillDownTerms);+ result = prime * result + Arrays.hashCode(drillSidewaysCollectors);+ return result;}@Overridepublic boolean equals(Object obj) {- throw new UnsupportedOperationException();+ if (this == obj) return true;+ if (!super.equals(obj)) return false;+ if (getClass() != obj.getClass()) return false;+ DrillSidewaysQuery other = (DrillSidewaysQuery) obj;+ if (baseQuery == null) {+ if (other.baseQuery != null) return false;+ } else if (!baseQuery.equals(other.baseQuery)) return false;+ if (drillDownCollector == null) {+ if (other.drillDownCollector != null) return false;+ } else if (!drillDownCollector.equals(other.drillDownCollector)) return false;+ if (!Arrays.equals(drillDownTerms, other.drillDownTerms)) return false;+ if (!Arrays.equals(drillSidewaysCollectors, other.drillSidewaysCollectors)) return false;+ return true;}}diff --git a/lucene/facet/src/test/org/apache/lucene/facet/search/DrillDownQueryTest.java b/lucene/facet/src/test/org/apache/lucene/facet/search/DrillDownQueryTest.javaindex c1eecca3405..019f035ff40 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/search/DrillDownQueryTest.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/search/DrillDownQueryTest.java@@ -43,6 +43,7 @@ import org.apache.lucene.index.Term;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.MatchAllDocsQuery;import org.apache.lucene.search.Query;+import org.apache.lucene.search.QueryUtils;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TermQuery;import org.apache.lucene.search.TopDocs;@@ -148,6 +149,7 @@ public class DrillDownQueryTest extends FacetTestCase {// Making sure the query yields 25 documents with the facet "a"DrillDownQuery q = new DrillDownQuery(defaultParams);q.add(new CategoryPath("a"));+ QueryUtils.check(q);TopDocs docs = searcher.search(q, 100);assertEquals(25, docs.totalHits);diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.javaindex d04a65a9189..3d48c70ddc3 100644--- a/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java+++ b/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java@@ -339,7 +339,8 @@ public class ToChildBlockJoinQuery extends Query {final ToChildBlockJoinQuery other = (ToChildBlockJoinQuery) _other;return origParentQuery.equals(other.origParentQuery) &&parentsFilter.equals(other.parentsFilter) &&- doScores == other.doScores;+ doScores == other.doScores &&+ super.equals(other);} else {return false;}@@ -348,7 +349,7 @@ public class ToChildBlockJoinQuery extends Query {@Overridepublic int hashCode() {final int prime = 31;- int hash = 1;+ int hash = super.hashCode();hash = prime * hash + origParentQuery.hashCode();hash = prime * hash + new Boolean(doScores).hashCode();hash = prime * hash + parentsFilter.hashCode();diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.javaindex 71fd6c196e5..277e8d768c5 100644--- a/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java+++ b/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java@@ -446,7 +446,8 @@ public class ToParentBlockJoinQuery extends Query {final ToParentBlockJoinQuery other = (ToParentBlockJoinQuery) _other;return origChildQuery.equals(other.origChildQuery) &&parentsFilter.equals(other.parentsFilter) &&- scoreMode == other.scoreMode;+ scoreMode == other.scoreMode &&+ super.equals(other);} else {return false;}@@ -455,17 +456,10 @@ public class ToParentBlockJoinQuery extends Query {@Overridepublic int hashCode() {final int prime = 31;- int hash = 1;+ int hash = super.hashCode();hash = prime * hash + origChildQuery.hashCode();hash = prime * hash + scoreMode.hashCode();hash = prime * hash + parentsFilter.hashCode();return hash;}-- @Override- public ToParentBlockJoinQuery clone() {- return new ToParentBlockJoinQuery(origChildQuery.clone(),- parentsFilter,- scoreMode);- }}diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.javaindex ab9fb420d90..a34c89402b5 100644--- a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java@@ -263,6 +263,7 @@ public class TestBlockJoin extends LuceneTestCase {IndexSearcher s = newSearcher(r);ToParentBlockJoinQuery q = new ToParentBlockJoinQuery(new MatchAllDocsQuery(), new QueryWrapperFilter(new MatchAllDocsQuery()), ScoreMode.Avg);+ QueryUtils.check(random(), q, s);s.search(q, 10);BooleanQuery bq = new BooleanQuery();bq.setBoost(2f); // we boost the BQdiff --git a/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.javaindex 037879ee7ab..404b7a13d69 100644--- a/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java+++ b/lucene/queries/src/java/org/apache/lucene/queries/BoostingQuery.java@@ -84,7 +84,7 @@ public class BoostingQuery extends Query {@Overridepublic int hashCode() {final int prime = 31;- int result = 1;+ int result = super.hashCode();result = prime * result + Float.floatToIntBits(boost);result = prime * result + ((context == null) ? 0 : context.hashCode());result = prime * result + ((match == null) ? 0 : match.hashCode());@@ -102,6 +102,10 @@ public class BoostingQuery extends Query {if (getClass() != obj.getClass()) {return false;}++ if (!super.equals(obj)) {+ return false;+ }BoostingQuery other = (BoostingQuery) obj;if (Float.floatToIntBits(boost) != Float.floatToIntBits(other.boost)) {diff --git a/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThisQuery.java b/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThisQuery.javaindex 1931e721eea..e38a8d4a424 100644--- a/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThisQuery.java+++ b/lucene/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThisQuery.java@@ -29,6 +29,7 @@ import org.apache.lucene.search.Query;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.StringReader;+import java.util.Arrays;import java.util.Set;/**@@ -148,4 +149,47 @@ public class MoreLikeThisQuery extends Query {public void setMinDocFreq(int minDocFreq) {this.minDocFreq = minDocFreq;}++ @Override+ public int hashCode() {+ final int prime = 31;+ int result = super.hashCode();+ result = prime * result + ((analyzer == null) ? 0 : analyzer.hashCode());+ result = prime * result + ((fieldName == null) ? 0 : fieldName.hashCode());+ result = prime * result + ((likeText == null) ? 0 : likeText.hashCode());+ result = prime * result + maxQueryTerms;+ result = prime * result + minDocFreq;+ result = prime * result + minTermFrequency;+ result = prime * result + Arrays.hashCode(moreLikeFields);+ result = prime * result + Float.floatToIntBits(percentTermsToMatch);+ result = prime * result + ((stopWords == null) ? 0 : stopWords.hashCode());+ return result;+ }++ @Override+ public boolean equals(Object obj) {+ if (this == obj) return true;+ if (!super.equals(obj)) return false;+ if (getClass() != obj.getClass()) return false;+ MoreLikeThisQuery other = (MoreLikeThisQuery) obj;+ if (analyzer == null) {+ if (other.analyzer != null) return false;+ } else if (!analyzer.equals(other.analyzer)) return false;+ if (fieldName == null) {+ if (other.fieldName != null) return false;+ } else if (!fieldName.equals(other.fieldName)) return false;+ if (likeText == null) {+ if (other.likeText != null) return false;+ } else if (!likeText.equals(other.likeText)) return false;+ if (maxQueryTerms != other.maxQueryTerms) return false;+ if (minDocFreq != other.minDocFreq) return false;+ if (minTermFrequency != other.minTermFrequency) return false;+ if (!Arrays.equals(moreLikeFields, other.moreLikeFields)) return false;+ if (Float.floatToIntBits(percentTermsToMatch) != Float+ .floatToIntBits(other.percentTermsToMatch)) return false;+ if (stopWords == null) {+ if (other.stopWords != null) return false;+ } else if (!stopWords.equals(other.stopWords)) return false;+ return true;+ }}diff --git a/lucene/queries/src/test/org/apache/lucene/queries/BoostingQueryTest.java b/lucene/queries/src/test/org/apache/lucene/queries/BoostingQueryTest.javaindex fc9a97385f6..f8faf128858 100644--- a/lucene/queries/src/test/org/apache/lucene/queries/BoostingQueryTest.java+++ b/lucene/queries/src/test/org/apache/lucene/queries/BoostingQueryTest.java@@ -18,15 +18,20 @@ package org.apache.lucene.queries;*/import org.apache.lucene.index.Term;+import org.apache.lucene.search.QueryUtils;import org.apache.lucene.search.TermQuery;import org.apache.lucene.util.LuceneTestCase;public class BoostingQueryTest extends LuceneTestCase {+ // TODO: this suite desperately needs more tests!+ // ... like ones that actually run the query+public void testBoostingQueryEquals() {TermQuery q1 = new TermQuery(new Term("subject:", "java"));TermQuery q2 = new TermQuery(new Term("subject:", "java"));assertEquals("Two TermQueries with same attributes should be equal", q1, q2);BoostingQuery bq1 = new BoostingQuery(q1, q2, 0.1f);+ QueryUtils.check(bq1);BoostingQuery bq2 = new BoostingQuery(q1, q2, 0.1f);assertEquals("BoostingQuery with same attributes is not equal", bq1, bq2);}diff --git a/lucene/queries/src/test/org/apache/lucene/queries/mlt/TestMoreLikeThis.java b/lucene/queries/src/test/org/apache/lucene/queries/mlt/TestMoreLikeThis.javaindex 09293855b28..524f7df5cb7 100644--- a/lucene/queries/src/test/org/apache/lucene/queries/mlt/TestMoreLikeThis.java+++ b/lucene/queries/src/test/org/apache/lucene/queries/mlt/TestMoreLikeThis.java@@ -23,6 +23,7 @@ import java.util.HashMap;import java.util.List;import java.util.Map;+import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.analysis.MockTokenizer;import org.apache.lucene.document.Document;@@ -32,6 +33,8 @@ import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.search.BooleanClause;import org.apache.lucene.search.BooleanQuery;import org.apache.lucene.search.IndexSearcher;+import org.apache.lucene.search.Query;+import org.apache.lucene.search.QueryUtils;import org.apache.lucene.search.TermQuery;import org.apache.lucene.store.Directory;import org.apache.lucene.util.LuceneTestCase;@@ -134,4 +137,11 @@ public class TestMoreLikeThis extends LuceneTestCase {mlt.setFieldNames(new String[] {"text", "foobar"});mlt.like(new StringReader("this is a test"), "foobar");}++ // just basic equals/hashcode etc+ public void testMoreLikeThisQuery() throws Exception {+ Query query = new MoreLikeThisQuery("this is a test", new String[] { "text" }, new MockAnalyzer(random()), "text");+ QueryUtils.check(random(), query, searcher);+ }+ // TODO: add tests for the MoreLikeThisQuery}diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/queries/FuzzyLikeThisQuery.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/queries/FuzzyLikeThisQuery.javaindex a8a08d9bf7a..5ee775ad677 100644--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/queries/FuzzyLikeThisQuery.java+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/queries/FuzzyLikeThisQuery.java@@ -72,7 +72,7 @@ public class FuzzyLikeThisQuery extends Query@Overridepublic int hashCode() {final int prime = 31;- int result = 1;+ int result = super.hashCode();result = prime * result + ((analyzer == null) ? 0 : analyzer.hashCode());result = prime * result+ ((fieldVals == null) ? 0 : fieldVals.hashCode());@@ -89,6 +89,9 @@ public class FuzzyLikeThisQuery extends Queryreturn false;if (getClass() != obj.getClass())return false;+ if (!super.equals(obj)) {+ return false;+ }FuzzyLikeThisQuery other = (FuzzyLikeThisQuery) obj;if (analyzer == null) {if (other.analyzer != null)diff --git a/lucene/test-framework/src/java/org/apache/lucene/search/AssertingIndexSearcher.java b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingIndexSearcher.javaindex 3f80d36fb30..2f1c53f3164 100644--- a/lucene/test-framework/src/java/org/apache/lucene/search/AssertingIndexSearcher.java+++ b/lucene/test-framework/src/java/org/apache/lucene/search/AssertingIndexSearcher.java@@ -81,6 +81,15 @@ public class AssertingIndexSearcher extends IndexSearcher {};}+ @Override+ public Query rewrite(Query original) throws IOException {+ // TODO: use the more sophisticated QueryUtils.check sometimes!+ QueryUtils.check(original);+ Query rewritten = super.rewrite(original);+ QueryUtils.check(rewritten);+ return rewritten;+ }+@Overrideprotected Query wrapFilter(Query query, Filter filter) {if (random.nextBoolean())