Skip to content

Apache Lucene open-source search software

add .equals support for MultiTermQuery and subclasses. this facilitates unit testing comparison of Query instances

Erik Hatcherf673bff9ef80
diff --git a/src/java/org/apache/lucene/search/FuzzyQuery.java b/src/java/org/apache/lucene/search/FuzzyQuery.java
index 3d9efa4f408..263a72aeb22 100644
--- a/src/java/org/apache/lucene/search/FuzzyQuery.java
+++ b/src/java/org/apache/lucene/search/FuzzyQuery.java
@@ -167,4 +167,24 @@ public final class FuzzyQuery extends MultiTermQuery {
}
}
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof FuzzyQuery)) return false;
+ if (!super.equals(o)) return false;
+
+ final FuzzyQuery fuzzyQuery = (FuzzyQuery) o;
+
+ if (minimumSimilarity != fuzzyQuery.minimumSimilarity) return false;
+ if (prefixLength != fuzzyQuery.prefixLength) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result = super.hashCode();
+ result = 29 * result + minimumSimilarity != +0.0f ? Float.floatToIntBits(minimumSimilarity) : 0;
+ result = 29 * result + prefixLength;
+ return result;
+ }
}
diff --git a/src/java/org/apache/lucene/search/MultiTermQuery.java b/src/java/org/apache/lucene/search/MultiTermQuery.java
index ac304963b96..616c40c7f32 100644
--- a/src/java/org/apache/lucene/search/MultiTermQuery.java
+++ b/src/java/org/apache/lucene/search/MultiTermQuery.java
@@ -86,4 +86,19 @@ public abstract class MultiTermQuery extends Query {
}
return buffer.toString();
}
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof MultiTermQuery)) return false;
+
+ final MultiTermQuery multiTermQuery = (MultiTermQuery) o;
+
+ if (!term.equals(multiTermQuery.term)) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ return term.hashCode();
+ }
}
diff --git a/src/java/org/apache/lucene/search/WildcardQuery.java b/src/java/org/apache/lucene/search/WildcardQuery.java
index 03621c0ad4a..9b266e5600f 100644
--- a/src/java/org/apache/lucene/search/WildcardQuery.java
+++ b/src/java/org/apache/lucene/search/WildcardQuery.java
@@ -37,5 +37,11 @@ public class WildcardQuery extends MultiTermQuery {
protected FilteredTermEnum getEnum(IndexReader reader) throws IOException {
return new WildcardTermEnum(reader, getTerm());
}
-
+
+ public boolean equals(Object o) {
+ if (o instanceof WildcardQuery)
+ return super.equals(o);
+
+ return false;
+ }
}
diff --git a/src/test/org/apache/lucene/search/TestWildcard.java b/src/test/org/apache/lucene/search/TestWildcard.java
index ee459fa8dfe..89beda37f01 100644
--- a/src/test/org/apache/lucene/search/TestWildcard.java
+++ b/src/test/org/apache/lucene/search/TestWildcard.java
@@ -16,15 +16,13 @@ package org.apache.lucene.search;
* limitations under the License.
*/
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.index.Term;
-import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.store.RAMDirectory;
+import junit.framework.TestCase;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
-
-import junit.framework.TestCase;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.store.RAMDirectory;
import java.io.IOException;
@@ -34,100 +32,102 @@ import java.io.IOException;
* @author Otis Gospodnetic
*/
public class TestWildcard
- extends TestCase
-{
- /**
- * Creates a new <code>TestWildcard</code> instance.
- *
- * @param name the name of the test
- */
- public TestWildcard(String name)
- {
- super(name);
- }
+ extends TestCase {
+ public void testEquals() {
+ WildcardQuery wq1 = new WildcardQuery(new Term("field", "b*a"));
+ WildcardQuery wq2 = new WildcardQuery(new Term("field", "b*a"));
+ WildcardQuery wq3 = new WildcardQuery(new Term("field", "b*a"));
- /**
- * Tests Wildcard queries with an asterisk.
- *
- */
- public void testAsterisk()
- throws IOException
- {
- RAMDirectory indexStore = getIndexStore("body", new String[]
- { "metal", "metals" }
- );
- IndexSearcher searcher = new IndexSearcher(indexStore);
- Query query1 = new TermQuery(new Term("body", "metal"));
- Query query2 = new WildcardQuery(new Term("body", "metal*"));
- Query query3 = new WildcardQuery(new Term("body", "m*tal"));
- Query query4 = new WildcardQuery(new Term("body", "m*tal*"));
- Query query5 = new WildcardQuery(new Term("body", "m*tals"));
-
- BooleanQuery query6 = new BooleanQuery();
- query6.add(query5, BooleanClause.Occur.SHOULD);
-
- BooleanQuery query7 = new BooleanQuery();
- query7.add(query3, BooleanClause.Occur.SHOULD);
- query7.add(query5, BooleanClause.Occur.SHOULD);
-
- // Queries do not automatically lower-case search terms:
- Query query8 = new WildcardQuery(new Term("body", "M*tal*"));
-
- assertMatches(searcher, query1, 1);
- assertMatches(searcher, query2, 2);
- assertMatches(searcher, query3, 1);
- assertMatches(searcher, query4, 2);
- assertMatches(searcher, query5, 1);
- assertMatches(searcher, query6, 1);
- assertMatches(searcher, query7, 2);
- assertMatches(searcher, query8, 0);
- }
+ // reflexive?
+ assertEquals(wq1, wq2);
+ assertEquals(wq2, wq1);
- /**
- * Tests Wildcard queries with a question mark.
- *
- * @exception IOException if an error occurs
- */
- public void testQuestionmark()
- throws IOException
- {
- RAMDirectory indexStore = getIndexStore("body", new String[]
- { "metal", "metals", "mXtals", "mXtXls" }
- );
- IndexSearcher searcher = new IndexSearcher(indexStore);
- Query query1 = new WildcardQuery(new Term("body", "m?tal"));
- Query query2 = new WildcardQuery(new Term("body", "metal?"));
- Query query3 = new WildcardQuery(new Term("body", "metals?"));
- Query query4 = new WildcardQuery(new Term("body", "m?t?ls"));
- Query query5 = new WildcardQuery(new Term("body", "M?t?ls"));
-
- assertMatches(searcher, query1, 1);
- assertMatches(searcher, query2, 2);
- assertMatches(searcher, query3, 1);
- assertMatches(searcher, query4, 3);
- assertMatches(searcher, query5, 0);
- }
+ // transitive?
+ assertEquals(wq2, wq3);
+ assertEquals(wq1, wq3);
- private RAMDirectory getIndexStore(String field, String[] contents)
- throws IOException
- {
- RAMDirectory indexStore = new RAMDirectory();
- IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
- for (int i = 0; i < contents.length; ++i) {
- Document doc = new Document();
- doc.add(new Field(field, contents[i], Field.Store.YES, Field.Index.TOKENIZED));
- writer.addDocument(doc);
- }
- writer.optimize();
- writer.close();
-
- return indexStore;
- }
+ assertFalse(wq1.equals(null));
+
+ FuzzyQuery fq = new FuzzyQuery(new Term("field", "b*a"));
+ assertFalse(wq1.equals(fq));
+ assertFalse(fq.equals(wq1));
+ }
+
+ /**
+ * Tests Wildcard queries with an asterisk.
+ */
+ public void testAsterisk()
+ throws IOException {
+ RAMDirectory indexStore = getIndexStore("body", new String[]
+ {"metal", "metals"});
+ IndexSearcher searcher = new IndexSearcher(indexStore);
+ Query query1 = new TermQuery(new Term("body", "metal"));
+ Query query2 = new WildcardQuery(new Term("body", "metal*"));
+ Query query3 = new WildcardQuery(new Term("body", "m*tal"));
+ Query query4 = new WildcardQuery(new Term("body", "m*tal*"));
+ Query query5 = new WildcardQuery(new Term("body", "m*tals"));
+
+ BooleanQuery query6 = new BooleanQuery();
+ query6.add(query5, BooleanClause.Occur.SHOULD);
+
+ BooleanQuery query7 = new BooleanQuery();
+ query7.add(query3, BooleanClause.Occur.SHOULD);
+ query7.add(query5, BooleanClause.Occur.SHOULD);
+
+// Queries do not automatically lower-case search terms:
+ Query query8 = new WildcardQuery(new Term("body", "M*tal*"));
- private void assertMatches(IndexSearcher searcher, Query q, int expectedMatches)
- throws IOException
- {
- Hits result = searcher.search(q);
- assertEquals(expectedMatches, result.length());
+ assertMatches(searcher, query1, 1);
+ assertMatches(searcher, query2, 2);
+ assertMatches(searcher, query3, 1);
+ assertMatches(searcher, query4, 2);
+ assertMatches(searcher, query5, 1);
+ assertMatches(searcher, query6, 1);
+ assertMatches(searcher, query7, 2);
+ assertMatches(searcher, query8, 0);
+ }
+
+ /**
+ * Tests Wildcard queries with a question mark.
+ *
+ * @throws IOException if an error occurs
+ */
+ public void testQuestionmark()
+ throws IOException {
+ RAMDirectory indexStore = getIndexStore("body", new String[]
+ {"metal", "metals", "mXtals", "mXtXls"});
+ IndexSearcher searcher = new IndexSearcher(indexStore);
+ Query query1 = new WildcardQuery(new Term("body", "m?tal"));
+ Query query2 = new WildcardQuery(new Term("body", "metal?"));
+ Query query3 = new WildcardQuery(new Term("body", "metals?"));
+ Query query4 = new WildcardQuery(new Term("body", "m?t?ls"));
+ Query query5 = new WildcardQuery(new Term("body", "M?t?ls"));
+
+ assertMatches(searcher, query1, 1);
+ assertMatches(searcher, query2, 2);
+ assertMatches(searcher, query3, 1);
+ assertMatches(searcher, query4, 3);
+ assertMatches(searcher, query5, 0);
+ }
+
+ private RAMDirectory getIndexStore(String field, String[] contents)
+ throws IOException {
+ RAMDirectory indexStore = new RAMDirectory();
+ IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
+ for (int i = 0; i < contents.length; ++i) {
+ Document doc = new Document();
+ doc.add(new Field(field, contents[i], Field.Store.YES, Field.Index.TOKENIZED));
+ writer.addDocument(doc);
}
+ writer.optimize();
+ writer.close();
+
+ return indexStore;
+ }
+
+ private void assertMatches(IndexSearcher searcher, Query q, int expectedMatches)
+ throws IOException {
+ Hits result = searcher.search(q);
+ assertEquals(expectedMatches, result.length());
+ }
}