Utilize ToStringUtils.boost() to remove duplication across .toString implementations and added boost comparison in .equals methods where it was missing

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@329381 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2005-10-29 09:26:21 +00:00
parent 9087671cef
commit 4fc1272877
16 changed files with 106 additions and 91 deletions

View File

@ -22,6 +22,7 @@ import java.util.Set;
import java.util.Vector; import java.util.Vector;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
/** A Query that matches documents matching boolean combinations of other /** A Query that matches documents matching boolean combinations of other
* queries, e.g. {@link TermQuery}s, {@link PhraseQuery}s or other * queries, e.g. {@link TermQuery}s, {@link PhraseQuery}s or other
@ -386,25 +387,25 @@ public class BooleanQuery extends Query {
for (int i = 0 ; i < clauses.size(); i++) { for (int i = 0 ; i < clauses.size(); i++) {
BooleanClause c = (BooleanClause)clauses.elementAt(i); BooleanClause c = (BooleanClause)clauses.elementAt(i);
if (c.isProhibited()) if (c.isProhibited())
buffer.append("-"); buffer.append("-");
else if (c.isRequired()) else if (c.isRequired())
buffer.append("+"); buffer.append("+");
Query subQuery = c.getQuery(); Query subQuery = c.getQuery();
if (subQuery instanceof BooleanQuery) { // wrap sub-bools in parens if (subQuery instanceof BooleanQuery) { // wrap sub-bools in parens
buffer.append("("); buffer.append("(");
buffer.append(c.getQuery().toString(field)); buffer.append(c.getQuery().toString(field));
buffer.append(")"); buffer.append(")");
} else } else
buffer.append(c.getQuery().toString(field)); buffer.append(c.getQuery().toString(field));
if (i != clauses.size()-1) if (i != clauses.size()-1)
buffer.append(" "); buffer.append(" ");
} }
if (getBoost() != 1.0) { if (getBoost() != 1.0) {
buffer.append(")^"); buffer.append(")");
buffer.append(getBoost()); buffer.append(ToStringUtils.boost(getBoost()));
} }
return buffer.toString(); return buffer.toString();

View File

@ -17,6 +17,8 @@ package org.apache.lucene.search;
*/ */
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
import java.io.IOException; import java.io.IOException;
import java.util.BitSet; import java.util.BitSet;
import java.util.Set; import java.util.Set;
@ -125,7 +127,13 @@ extends Query {
/** Prints a user-readable version of this query. */ /** Prints a user-readable version of this query. */
public String toString (String s) { public String toString (String s) {
return "filtered("+query.toString(s)+")->"+filter; StringBuffer buffer = new StringBuffer();
buffer.append("filtered(");
buffer.append(query.toString(s));
buffer.append(")->");
buffer.append(filter);
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
} }
/** Returns true iff <code>o</code> is equal to this. */ /** Returns true iff <code>o</code> is equal to this. */

View File

@ -19,6 +19,7 @@ package org.apache.lucene.search;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.util.PriorityQueue; import org.apache.lucene.util.PriorityQueue;
import org.apache.lucene.util.ToStringUtils;
import java.io.IOException; import java.io.IOException;
@ -135,7 +136,17 @@ public final class FuzzyQuery extends MultiTermQuery {
} }
public String toString(String field) { public String toString(String field) {
return super.toString(field) + '~' + Float.toString(minimumSimilarity); StringBuffer buffer = new StringBuffer();
Term term = getTerm();
if (!term.field().equals(field)) {
buffer.append(term.field());
buffer.append(":");
}
buffer.append(term.text());
buffer.append('~');
buffer.append(Float.toString(minimumSimilarity));
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
} }
private static class ScoreTerm{ private static class ScoreTerm{

View File

@ -23,6 +23,7 @@ import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Searcher; import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Similarity; import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.Weight; import org.apache.lucene.search.Weight;
import org.apache.lucene.util.ToStringUtils;
/** /**
* A query that matches all documents. * A query that matches all documents.
@ -130,10 +131,7 @@ public class MatchAllDocsQuery extends Query {
public String toString(String field) { public String toString(String field) {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
buffer.append("MatchAllDocsQuery"); buffer.append("MatchAllDocsQuery");
if (getBoost() != 1.0f) { buffer.append(ToStringUtils.boost(getBoost()));
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
return buffer.toString(); return buffer.toString();
} }

View File

@ -26,6 +26,7 @@ import org.apache.lucene.index.MultipleTermPositions;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermPositions; import org.apache.lucene.index.TermPositions;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.ToStringUtils;
/** /**
* MultiPhraseQuery is a generalized version of PhraseQuery, with an added * MultiPhraseQuery is a generalized version of PhraseQuery, with an added
@ -279,10 +280,7 @@ public class MultiPhraseQuery extends Query {
buffer.append(slop); buffer.append(slop);
} }
if (getBoost() != 1.0f) { buffer.append(ToStringUtils.boost(getBoost()));
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
return buffer.toString(); return buffer.toString();
} }

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.util.ToStringUtils;
/** /**
* A {@link Query} that matches documents containing a subset of terms provided * A {@link Query} that matches documents containing a subset of terms provided
@ -75,10 +76,7 @@ public abstract class MultiTermQuery extends Query {
buffer.append(":"); buffer.append(":");
} }
buffer.append(term.text()); buffer.append(term.text());
if (getBoost() != 1.0f) { buffer.append(ToStringUtils.boost(getBoost()));
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
return buffer.toString(); return buffer.toString();
} }
@ -90,7 +88,7 @@ public abstract class MultiTermQuery extends Query {
if (!term.equals(multiTermQuery.term)) return false; if (!term.equals(multiTermQuery.term)) return false;
return true; return getBoost() == multiTermQuery.getBoost();
} }
public int hashCode() { public int hashCode() {

View File

@ -26,6 +26,7 @@ import org.apache.lucene.index.MultipleTermPositions;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermPositions; import org.apache.lucene.index.TermPositions;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.ToStringUtils;
/** /**
* PhrasePrefixQuery is a generalized version of PhraseQuery, with an added * PhrasePrefixQuery is a generalized version of PhraseQuery, with an added
@ -265,10 +266,7 @@ public class PhrasePrefixQuery extends Query {
buffer.append(slop); buffer.append(slop);
} }
if (getBoost() != 1.0f) { buffer.append(ToStringUtils.boost(getBoost()));
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
return buffer.toString(); return buffer.toString();
} }

View File

@ -23,6 +23,7 @@ import java.util.Vector;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermPositions; import org.apache.lucene.index.TermPositions;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
/** A Query that matches documents containing a particular sequence of terms. /** A Query that matches documents containing a particular sequence of terms.
* A PhraseQuery is built by QueryParser for input like <code>"new york"</code>. * A PhraseQuery is built by QueryParser for input like <code>"new york"</code>.
@ -262,7 +263,7 @@ public class PhraseQuery extends Query {
for (int i = 0; i < terms.size(); i++) { for (int i = 0; i < terms.size(); i++) {
buffer.append(((Term)terms.elementAt(i)).text()); buffer.append(((Term)terms.elementAt(i)).text());
if (i != terms.size()-1) if (i != terms.size()-1)
buffer.append(" "); buffer.append(" ");
} }
buffer.append("\""); buffer.append("\"");
@ -271,10 +272,7 @@ public class PhraseQuery extends Query {
buffer.append(slop); buffer.append(slop);
} }
if (getBoost() != 1.0f) { buffer.append(ToStringUtils.boost(getBoost()));
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
return buffer.toString(); return buffer.toString();
} }

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum; import org.apache.lucene.index.TermEnum;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
/** A Query that matches documents containing terms with a specified prefix. A PrefixQuery /** A Query that matches documents containing terms with a specified prefix. A PrefixQuery
* is built by QueryParser for input like <code>app*</code>. */ * is built by QueryParser for input like <code>app*</code>. */
@ -69,10 +70,7 @@ public class PrefixQuery extends Query {
} }
buffer.append(prefix.text()); buffer.append(prefix.text());
buffer.append('*'); buffer.append('*');
if (getBoost() != 1.0f) { buffer.append(ToStringUtils.boost(getBoost()));
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
return buffer.toString(); return buffer.toString();
} }

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum; import org.apache.lucene.index.TermEnum;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
/** /**
* A Query that matches documents within an exclusive range. A RangeQuery * A Query that matches documents within an exclusive range. A RangeQuery
@ -134,11 +135,7 @@ public class RangeQuery extends Query
buffer.append(" TO "); buffer.append(" TO ");
buffer.append(upperTerm != null ? upperTerm.text() : "null"); buffer.append(upperTerm != null ? upperTerm.text() : "null");
buffer.append(inclusive ? "]" : "}"); buffer.append(inclusive ? "]" : "}");
if (getBoost() != 1.0f) buffer.append(ToStringUtils.boost(getBoost()));
{
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
return buffer.toString(); return buffer.toString();
} }

View File

@ -22,6 +22,7 @@ import java.util.Set;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs; import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
/** A Query that matches documents containing a term. /** A Query that matches documents containing a term.
This may be combined with other terms with a {@link BooleanQuery}. This may be combined with other terms with a {@link BooleanQuery}.
@ -153,10 +154,7 @@ public class TermQuery extends Query {
buffer.append(":"); buffer.append(":");
} }
buffer.append(term.text()); buffer.append(term.text());
if (getBoost() != 1.0f) { buffer.append(ToStringUtils.boost(getBoost()));
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
return buffer.toString(); return buffer.toString();
} }

View File

@ -22,6 +22,7 @@ import java.util.Collection;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.ToStringUtils;
/** Matches spans near the beginning of a field. */ /** Matches spans near the beginning of a field. */
public class SpanFirstQuery extends SpanQuery { public class SpanFirstQuery extends SpanQuery {
@ -52,6 +53,7 @@ public class SpanFirstQuery extends SpanQuery {
buffer.append(", "); buffer.append(", ");
buffer.append(end); buffer.append(end);
buffer.append(")"); buffer.append(")");
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString(); return buffer.toString();
} }

View File

@ -26,6 +26,7 @@ import java.util.Iterator;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.ToStringUtils;
/** Matches spans which are near one another. One can specify <i>slop</i>, the /** Matches spans which are near one another. One can specify <i>slop</i>, the
* maximum number of intervening unmatched positions, as well as whether * maximum number of intervening unmatched positions, as well as whether
@ -98,6 +99,7 @@ public class SpanNearQuery extends SpanQuery {
buffer.append(", "); buffer.append(", ");
buffer.append(inOrder); buffer.append(inOrder);
buffer.append(")"); buffer.append(")");
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString(); return buffer.toString();
} }
@ -141,7 +143,7 @@ public class SpanNearQuery extends SpanQuery {
if (!clauses.equals(spanNearQuery.clauses)) return false; if (!clauses.equals(spanNearQuery.clauses)) return false;
if (!field.equals(spanNearQuery.field)) return false; if (!field.equals(spanNearQuery.field)) return false;
return true; return getBoost() == spanNearQuery.getBoost();
} }
public int hashCode() { public int hashCode() {

View File

@ -22,6 +22,7 @@ import java.util.Collection;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.ToStringUtils;
/** Removes matches which overlap with another SpanQuery. */ /** Removes matches which overlap with another SpanQuery. */
public class SpanNotQuery extends SpanQuery { public class SpanNotQuery extends SpanQuery {
@ -55,6 +56,7 @@ public class SpanNotQuery extends SpanQuery {
buffer.append(", "); buffer.append(", ");
buffer.append(exclude.toString(field)); buffer.append(exclude.toString(field));
buffer.append(")"); buffer.append(")");
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString(); return buffer.toString();
} }

View File

@ -25,6 +25,7 @@ import java.util.Iterator;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.PriorityQueue; import org.apache.lucene.util.PriorityQueue;
import org.apache.lucene.util.ToStringUtils;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
/** Matches the union of its clauses.*/ /** Matches the union of its clauses.*/
@ -95,6 +96,7 @@ public class SpanOrQuery extends SpanQuery {
} }
} }
buffer.append("])"); buffer.append("])");
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString(); return buffer.toString();
} }
@ -107,7 +109,7 @@ public class SpanOrQuery extends SpanQuery {
if (!clauses.equals(that.clauses)) return false; if (!clauses.equals(that.clauses)) return false;
if (!field.equals(that.field)) return false; if (!field.equals(that.field)) return false;
return true; return getBoost() == that.getBoost();
} }
public int hashCode() { public int hashCode() {

View File

@ -24,6 +24,7 @@ import java.util.ArrayList;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermPositions; import org.apache.lucene.index.TermPositions;
import org.apache.lucene.util.ToStringUtils;
/** Matches spans containing a term. */ /** Matches spans containing a term. */
public class SpanTermQuery extends SpanQuery { public class SpanTermQuery extends SpanQuery {
@ -44,10 +45,13 @@ public class SpanTermQuery extends SpanQuery {
} }
public String toString(String field) { public String toString(String field) {
StringBuffer buffer = new StringBuffer();
if (term.field().equals(field)) if (term.field().equals(field))
return term.text(); buffer.append(term.text());
else else
return term.toString(); buffer.append(term.toString());
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
} }
/** Returns true iff <code>o</code> is equal to this. */ /** Returns true iff <code>o</code> is equal to this. */