Skip to content

Apache Lucene open-source search software

Contrib queries package Query implementations do not override equals()

Mark Robert Miller413cabef1f57
diff --git a/contrib/queries/src/java/org/apache/lucene/search/BoostingQuery.java b/contrib/queries/src/java/org/apache/lucene/search/BoostingQuery.java
index deaac5152cc..e0ef0994ba5 100644
--- a/contrib/queries/src/java/org/apache/lucene/search/BoostingQuery.java
+++ b/contrib/queries/src/java/org/apache/lucene/search/BoostingQuery.java
@@ -83,6 +83,38 @@ public class BoostingQuery extends Query {
return result;
}
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + Float.floatToIntBits(boost);
+ result = prime * result + ((context == null) ? 0 : context.hashCode());
+ result = prime * result + ((match == null) ? 0 : match.hashCode());
+ return result;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ BoostingQuery other = (BoostingQuery) obj;
+ if (Float.floatToIntBits(boost) != Float.floatToIntBits(other.boost))
+ return false;
+ if (context == null) {
+ if (other.context != null)
+ return false;
+ } else if (!context.equals(other.context))
+ return false;
+ if (match == null) {
+ if (other.match != null)
+ return false;
+ } else if (!match.equals(other.match))
+ return false;
+ return true;
+ }
+
public String toString(String field) {
return match.toString(field) + "/" + context.toString(field);
}
diff --git a/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java b/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java
index 6e7840dd3ec..7c74aea2112 100644
--- a/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java
+++ b/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java
@@ -57,8 +57,45 @@ public class FuzzyLikeThisQuery extends Query
ScoreTermQueue q;
int MAX_VARIANTS_PER_TERM=50;
boolean ignoreTF=false;
+ private int maxNumTerms;
+
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((analyzer == null) ? 0 : analyzer.hashCode());
+ result = prime * result
+ + ((fieldVals == null) ? 0 : fieldVals.hashCode());
+ result = prime * result + (ignoreTF ? 1231 : 1237);
+ result = prime * result + maxNumTerms;
+ return result;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ FuzzyLikeThisQuery other = (FuzzyLikeThisQuery) obj;
+ if (analyzer == null) {
+ if (other.analyzer != null)
+ return false;
+ } else if (!analyzer.equals(other.analyzer))
+ return false;
+ if (fieldVals == null) {
+ if (other.fieldVals != null)
+ return false;
+ } else if (!fieldVals.equals(other.fieldVals))
+ return false;
+ if (ignoreTF != other.ignoreTF)
+ return false;
+ if (maxNumTerms != other.maxNumTerms)
+ return false;
+ return true;
+ }
+
-
/**
*
* @param maxNumTerms The total number of terms clauses that will appear once rewritten as a BooleanQuery
@@ -68,6 +105,7 @@ public class FuzzyLikeThisQuery extends Query
{
q=new ScoreTermQueue(maxNumTerms);
this.analyzer=analyzer;
+ this.maxNumTerms = maxNumTerms;
}
class FieldVals
@@ -83,6 +121,46 @@ public class FuzzyLikeThisQuery extends Query
prefixLength = length;
this.queryString = queryString;
}
+
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result
+ + ((fieldName == null) ? 0 : fieldName.hashCode());
+ result = prime * result + Float.floatToIntBits(minSimilarity);
+ result = prime * result + prefixLength;
+ result = prime * result
+ + ((queryString == null) ? 0 : queryString.hashCode());
+ return result;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ FieldVals other = (FieldVals) obj;
+ if (fieldName == null) {
+ if (other.fieldName != null)
+ return false;
+ } else if (!fieldName.equals(other.fieldName))
+ return false;
+ if (Float.floatToIntBits(minSimilarity) != Float
+ .floatToIntBits(other.minSimilarity))
+ return false;
+ if (prefixLength != other.prefixLength)
+ return false;
+ if (queryString == null) {
+ if (other.queryString != null)
+ return false;
+ } else if (!queryString.equals(other.queryString))
+ return false;
+ return true;
+ }
+
+
}
diff --git a/contrib/queries/src/test/org/apache/lucene/search/BoostingQueryTest.java b/contrib/queries/src/test/org/apache/lucene/search/BoostingQueryTest.java
new file mode 100644
index 00000000000..b6dfccc6cb4
--- /dev/null
+++ b/contrib/queries/src/test/org/apache/lucene/search/BoostingQueryTest.java
@@ -0,0 +1,16 @@
+package org.apache.lucene.search;
+
+import org.apache.lucene.index.Term;
+
+import junit.framework.TestCase;
+
+public class BoostingQueryTest extends TestCase {
+ 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);
+ BoostingQuery bq2 = new BoostingQuery(q1, q2, 0.1f);
+ assertEquals("BoostingQuery with same attributes is not equal", bq1, bq2);
+ }
+}
diff --git a/contrib/queries/src/test/org/apache/lucene/search/FuzzyLikeThisQueryTest.java b/contrib/queries/src/test/org/apache/lucene/search/FuzzyLikeThisQueryTest.java
index c608cd71cc3..d8ba756b8a3 100644
--- a/contrib/queries/src/test/org/apache/lucene/search/FuzzyLikeThisQueryTest.java
+++ b/contrib/queries/src/test/org/apache/lucene/search/FuzzyLikeThisQueryTest.java
@@ -111,4 +111,14 @@ public class FuzzyLikeThisQueryTest extends TestCase
Document doc=searcher.doc(sd[0].doc);
assertEquals("Should match most similar when using 2 words", "2",doc.get("id"));
}
+
+ public void testFuzzyLikeThisQueryEquals() {
+ Analyzer analyzer = new WhitespaceAnalyzer();
+ FuzzyLikeThisQuery fltq1 = new FuzzyLikeThisQuery(10, analyzer);
+ fltq1.addTerms("javi", "subject", 0.5f, 2);
+ FuzzyLikeThisQuery fltq2 = new FuzzyLikeThisQuery(10, analyzer);
+ fltq2.addTerms("javi", "subject", 0.5f, 2);
+ assertEquals("FuzzyLikeThisQuery with same attributes is not equal", fltq1,
+ fltq2);
+ }
}