Apache Lucene open-source search software
LUCENE-1415: MultiPhraseQuery has incorrect hashCode() and equals()
Yonik Seeley00f98b1fd777
diff --git a/CHANGES.txt b/CHANGES.txtindex 327e29b142c..c245765feef 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -9,6 +9,10 @@ API ChangesBug fixes+1. LUCENE-1415: MultiPhraseQuery has incorrect hashCode() and equals()+ implementation - Leads to Solr Cache misses.+ (Todd Feak, Mark Miller via yonik)+New featuresOptimizationsdiff --git a/src/java/org/apache/lucene/search/MultiPhraseQuery.java b/src/java/org/apache/lucene/search/MultiPhraseQuery.javaindex 5355b545752..2947ba3dfbc 100644--- a/src/java/org/apache/lucene/search/MultiPhraseQuery.java+++ b/src/java/org/apache/lucene/search/MultiPhraseQuery.java@@ -311,7 +311,7 @@ public class MultiPhraseQuery extends Query {MultiPhraseQuery other = (MultiPhraseQuery)o;return this.getBoost() == other.getBoost()&& this.slop == other.slop- && this.termArrays.equals(other.termArrays)+ && termArraysEquals(this.termArrays, other.termArrays)&& this.positions.equals(other.positions);}@@ -319,8 +319,52 @@ public class MultiPhraseQuery extends Query {public int hashCode() {return Float.floatToIntBits(getBoost())^ slop- ^ termArrays.hashCode()+ ^ termArraysHashCode()^ positions.hashCode()^ 0x4AC65113;}++ // Breakout calculation of the termArrays hashcode+ private int termArraysHashCode() {+ int hashCode = 1;+ Iterator iterator = termArrays.iterator();+ while (iterator.hasNext()) {+ Term[] termArray = (Term[]) iterator.next();+ hashCode = 31 * hashCode+ + (termArray == null ? 0 : arraysHashCode(termArray));+ }+ return hashCode;+ }++ private int arraysHashCode(Term[] termArray) {+ if (termArray == null)+ return 0;++ int result = 1;++ for (int i = 0; i < termArray.length; i++) {+ Term term = termArray[i];+ result = 31 * result + (term == null ? 0 : term.hashCode());+ }++ return result;+ }++ // Breakout calculation of the termArrays equals+ private boolean termArraysEquals(List termArrays1, List termArrays2) {+ if (termArrays1.size() != termArrays2.size()) {+ return false;+ }+ ListIterator iterator1 = termArrays1.listIterator();+ ListIterator iterator2 = termArrays2.listIterator();+ while (iterator1.hasNext()) {+ Term[] termArray1 = (Term[]) iterator1.next();+ Term[] termArray2 = (Term[]) iterator2.next();+ if (!(termArray1 == null ? termArray2 == null : Arrays.equals(termArray1,+ termArray2))) {+ return false;+ }+ }+ return true;+ }}diff --git a/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java b/src/test/org/apache/lucene/search/TestMultiPhraseQuery.javaindex 166f2274ffc..403bdb1dd31 100644--- a/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java+++ b/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java@@ -191,6 +191,35 @@ public class TestMultiPhraseQuery extends LuceneTestCasesearcher.close();}+ public void testHashCodeAndEquals(){+ MultiPhraseQuery query1 = new MultiPhraseQuery();+ MultiPhraseQuery query2 = new MultiPhraseQuery();++ assertEquals(query1.hashCode(), query2.hashCode());+ assertEquals(query1,query2);++ Term term1= new Term("someField","someText");++ query1.add(term1);+ query2.add(term1);++ assertEquals(query1.hashCode(), query2.hashCode());+ assertEquals(query1,query2);++ Term term2= new Term("someField","someMoreText");++ query1.add(term2);++ assertFalse(query1.hashCode()==query2.hashCode());+ assertFalse(query1.equals(query2));++ query2.add(term2);++ assertEquals(query1.hashCode(), query2.hashCode());+ assertEquals(query1,query2);+ }++private void add(String s, String type, IndexWriter writer) throws IOException {Document doc = new Document();doc.add(new Field("body", s, Field.Store.YES, Field.Index.ANALYZED));