From 6b3823e6a390f71343c88abf5cc52a023a04d84a Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Sun, 27 Jan 2013 20:14:02 +0000 Subject: [PATCH] LUCENE-4704: Make join queries override hashcode and equals methods. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1439166 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 3 ++ .../apache/lucene/search/join/JoinUtil.java | 2 +- .../search/join/TermsIncludingScoreQuery.java | 29 ++++++++++++++++++ .../apache/lucene/search/join/TermsQuery.java | 30 ++++++++++++++++++- 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 993d8e6452e..fd6439f5e1b 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -104,6 +104,9 @@ Bug Fixes degrees and barely any height, it would generate so many indexed terms (> 500k) that it could even cause an OutOfMemoryError. Fixed. (David Smiley) +* LUCENE-4704: Make join queries override hashcode and equals methods. + (Martijn van Groningen) + ======================= Lucene 4.1.0 ======================= Changes in backwards compatibility policy diff --git a/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java b/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java index 492127858f3..8649f3184e4 100644 --- a/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java +++ b/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java @@ -71,7 +71,7 @@ public final class JoinUtil { case None: TermsCollector termsCollector = TermsCollector.create(fromField, multipleValuesPerDocument); fromSearcher.search(fromQuery, termsCollector); - return new TermsQuery(toField, termsCollector.getCollectorTerms()); + return new TermsQuery(toField, fromQuery, termsCollector.getCollectorTerms()); case Total: case Max: case Avg: diff --git a/lucene/join/src/java/org/apache/lucene/search/join/TermsIncludingScoreQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/TermsIncludingScoreQuery.java index 9318a158874..7be2446f839 100644 --- a/lucene/join/src/java/org/apache/lucene/search/join/TermsIncludingScoreQuery.java +++ b/lucene/join/src/java/org/apache/lucene/search/join/TermsIncludingScoreQuery.java @@ -92,6 +92,35 @@ class TermsIncludingScoreQuery extends Query { } } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } if (!super.equals(obj)) { + return false; + } if (getClass() != obj.getClass()) { + return false; + } + + TermsIncludingScoreQuery other = (TermsIncludingScoreQuery) obj; + if (!field.equals(other.field)) { + return false; + } + if (!unwrittenOriginalQuery.equals(other.unwrittenOriginalQuery)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result += prime * field.hashCode(); + result += prime * unwrittenOriginalQuery.hashCode(); + return result; + } + @Override public Weight createWeight(IndexSearcher searcher) throws IOException { final Weight originalWeight = originalQuery.createWeight(searcher); diff --git a/lucene/join/src/java/org/apache/lucene/search/join/TermsQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/TermsQuery.java index daf5928024c..f4f2eb1bd30 100644 --- a/lucene/join/src/java/org/apache/lucene/search/join/TermsQuery.java +++ b/lucene/join/src/java/org/apache/lucene/search/join/TermsQuery.java @@ -21,6 +21,7 @@ import org.apache.lucene.index.FilteredTermsEnum; import org.apache.lucene.index.Terms; import org.apache.lucene.index.TermsEnum; import org.apache.lucene.search.MultiTermQuery; +import org.apache.lucene.search.Query; import org.apache.lucene.util.AttributeSource; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefHash; @@ -37,13 +38,15 @@ import java.util.Comparator; class TermsQuery extends MultiTermQuery { private final BytesRefHash terms; + private final Query fromQuery; // Used for equals() only /** * @param field The field that should contain terms that are specified in the previous parameter * @param terms The terms that matching documents should have. The terms must be sorted by natural order. */ - TermsQuery(String field, BytesRefHash terms) { + TermsQuery(String field, Query fromQuery, BytesRefHash terms) { super(field); + this.fromQuery = fromQuery; this.terms = terms; } @@ -63,6 +66,31 @@ class TermsQuery extends MultiTermQuery { '}'; } + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } if (!super.equals(obj)) { + return false; + } if (getClass() != obj.getClass()) { + return false; + } + + TermsQuery other = (TermsQuery) obj; + if (!fromQuery.equals(other.fromQuery)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result += prime * fromQuery.hashCode(); + return result; + } + static class SeekingTermSetTermsEnum extends FilteredTermsEnum { private final BytesRefHash terms;