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,13 +22,14 @@ import java.util.Set;
import java.util.Vector;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
/** A Query that matches documents matching boolean combinations of other
* queries, e.g. {@link TermQuery}s, {@link PhraseQuery}s or other
* BooleanQuerys.
*/
public class BooleanQuery extends Query {
/**
* @deprecated use {@link #setMaxClauseCount(int)} instead
*/
@ -314,15 +315,15 @@ public class BooleanQuery extends Query {
/** Indicates whether to use good old 1.4 BooleanScorer. */
private static boolean useScorer14 = false;
public static void setUseScorer14(boolean use14) {
useScorer14 = use14;
}
public static boolean getUseScorer14() {
return useScorer14;
}
protected Weight createWeight(Searcher searcher) throws IOException {
return getUseScorer14() ? (Weight) new BooleanWeight(searcher)
: (Weight) new BooleanWeight2(searcher);
@ -386,25 +387,25 @@ public class BooleanQuery extends Query {
for (int i = 0 ; i < clauses.size(); i++) {
BooleanClause c = (BooleanClause)clauses.elementAt(i);
if (c.isProhibited())
buffer.append("-");
buffer.append("-");
else if (c.isRequired())
buffer.append("+");
buffer.append("+");
Query subQuery = c.getQuery();
if (subQuery instanceof BooleanQuery) { // wrap sub-bools in parens
buffer.append("(");
buffer.append(c.getQuery().toString(field));
buffer.append(")");
buffer.append("(");
buffer.append(c.getQuery().toString(field));
buffer.append(")");
} else
buffer.append(c.getQuery().toString(field));
buffer.append(c.getQuery().toString(field));
if (i != clauses.size()-1)
buffer.append(" ");
buffer.append(" ");
}
if (getBoost() != 1.0) {
buffer.append(")^");
buffer.append(getBoost());
buffer.append(")");
buffer.append(ToStringUtils.boost(getBoost()));
}
return buffer.toString();

View File

@ -17,6 +17,8 @@ package org.apache.lucene.search;
*/
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
import java.io.IOException;
import java.util.BitSet;
import java.util.Set;
@ -125,7 +127,13 @@ extends Query {
/** Prints a user-readable version of this query. */
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. */

View File

@ -19,6 +19,7 @@ package org.apache.lucene.search;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.util.PriorityQueue;
import org.apache.lucene.util.ToStringUtils;
import java.io.IOException;
@ -135,7 +136,17 @@ public final class FuzzyQuery extends MultiTermQuery {
}
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{

View File

@ -23,6 +23,7 @@ import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.Weight;
import org.apache.lucene.util.ToStringUtils;
/**
* A query that matches all documents.
@ -130,10 +131,7 @@ public class MatchAllDocsQuery extends Query {
public String toString(String field) {
StringBuffer buffer = new StringBuffer();
buffer.append("MatchAllDocsQuery");
if (getBoost() != 1.0f) {
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
buffer.append(ToStringUtils.boost(getBoost()));
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.TermPositions;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.ToStringUtils;
/**
* MultiPhraseQuery is a generalized version of PhraseQuery, with an added
@ -72,7 +73,7 @@ public class MultiPhraseQuery extends Query {
add(terms, position);
}
/**
* Allows to specify the relative position of terms within the phrase.
*
@ -95,7 +96,7 @@ public class MultiPhraseQuery extends Query {
termArrays.add(terms);
positions.addElement(new Integer(position));
}
/**
* Returns the relative positions of terms in this phrase.
*/
@ -144,23 +145,23 @@ public class MultiPhraseQuery extends Query {
public Scorer scorer(IndexReader reader) throws IOException {
if (termArrays.size() == 0) // optimize zero-term case
return null;
TermPositions[] tps = new TermPositions[termArrays.size()];
for (int i=0; i<tps.length; i++) {
Term[] terms = (Term[])termArrays.get(i);
TermPositions p;
if (terms.length > 1)
p = new MultipleTermPositions(reader, terms);
else
p = reader.termPositions(terms[0]);
if (p == null)
return null;
tps[i] = p;
}
if (slop == 0)
return new ExactPhraseScorer(this, tps, getPositions(), similarity,
reader.norms(field));
@ -168,14 +169,14 @@ public class MultiPhraseQuery extends Query {
return new SloppyPhraseScorer(this, tps, getPositions(), similarity,
slop, reader.norms(field));
}
public Explanation explain(IndexReader reader, int doc)
throws IOException {
Explanation result = new Explanation();
result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");
Explanation idfExpl = new Explanation(idf, "idf("+getQuery()+")");
// explain query weight
Explanation queryExpl = new Explanation();
queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");
@ -185,16 +186,16 @@ public class MultiPhraseQuery extends Query {
queryExpl.addDetail(boostExpl);
queryExpl.addDetail(idfExpl);
Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");
queryExpl.addDetail(queryNormExpl);
queryExpl.setValue(boostExpl.getValue() *
idfExpl.getValue() *
queryNormExpl.getValue());
result.addDetail(queryExpl);
// explain field weight
Explanation fieldExpl = new Explanation();
fieldExpl.setDescription("fieldWeight("+getQuery()+" in "+doc+
@ -215,7 +216,7 @@ public class MultiPhraseQuery extends Query {
fieldExpl.setValue(tfExpl.getValue() *
idfExpl.getValue() *
fieldNormExpl.getValue());
result.addDetail(fieldExpl);
// combine them
@ -241,7 +242,7 @@ public class MultiPhraseQuery extends Query {
return this;
}
}
protected Weight createWeight(Searcher searcher) throws IOException {
return new MultiPhraseWeight(searcher);
}
@ -279,10 +280,7 @@ public class MultiPhraseQuery extends Query {
buffer.append(slop);
}
if (getBoost() != 1.0f) {
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
}

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.util.ToStringUtils;
/**
* 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(term.text());
if (getBoost() != 1.0f) {
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
}
@ -90,7 +88,7 @@ public abstract class MultiTermQuery extends Query {
if (!term.equals(multiTermQuery.term)) return false;
return true;
return getBoost() == multiTermQuery.getBoost();
}
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.TermPositions;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.ToStringUtils;
/**
* PhrasePrefixQuery is a generalized version of PhraseQuery, with an added
@ -73,7 +74,7 @@ public class PhrasePrefixQuery extends Query {
add(terms, position);
}
/**
* Allows to specify the relative position of terms within the phrase.
*
@ -96,7 +97,7 @@ public class PhrasePrefixQuery extends Query {
termArrays.add(terms);
positions.addElement(new Integer(position));
}
/**
* Returns the relative positions of terms in this phrase.
*/
@ -145,23 +146,23 @@ public class PhrasePrefixQuery extends Query {
public Scorer scorer(IndexReader reader) throws IOException {
if (termArrays.size() == 0) // optimize zero-term case
return null;
TermPositions[] tps = new TermPositions[termArrays.size()];
for (int i=0; i<tps.length; i++) {
Term[] terms = (Term[])termArrays.get(i);
TermPositions p;
if (terms.length > 1)
p = new MultipleTermPositions(reader, terms);
else
p = reader.termPositions(terms[0]);
if (p == null)
return null;
tps[i] = p;
}
if (slop == 0)
return new ExactPhraseScorer(this, tps, getPositions(), similarity,
reader.norms(field));
@ -169,14 +170,14 @@ public class PhrasePrefixQuery extends Query {
return new SloppyPhraseScorer(this, tps, getPositions(), similarity,
slop, reader.norms(field));
}
public Explanation explain(IndexReader reader, int doc)
throws IOException {
Explanation result = new Explanation();
result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");
Explanation idfExpl = new Explanation(idf, "idf("+getQuery()+")");
// explain query weight
Explanation queryExpl = new Explanation();
queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");
@ -186,16 +187,16 @@ public class PhrasePrefixQuery extends Query {
queryExpl.addDetail(boostExpl);
queryExpl.addDetail(idfExpl);
Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");
queryExpl.addDetail(queryNormExpl);
queryExpl.setValue(boostExpl.getValue() *
idfExpl.getValue() *
queryNormExpl.getValue());
result.addDetail(queryExpl);
// explain field weight
Explanation fieldExpl = new Explanation();
fieldExpl.setDescription("fieldWeight("+getQuery()+" in "+doc+
@ -216,7 +217,7 @@ public class PhrasePrefixQuery extends Query {
fieldExpl.setValue(tfExpl.getValue() *
idfExpl.getValue() *
fieldNormExpl.getValue());
result.addDetail(fieldExpl);
// combine them
@ -265,10 +266,7 @@ public class PhrasePrefixQuery extends Query {
buffer.append(slop);
}
if (getBoost() != 1.0f) {
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
}

View File

@ -23,6 +23,7 @@ import java.util.Vector;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermPositions;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
/** A Query that matches documents containing a particular sequence of terms.
* A PhraseQuery is built by QueryParser for input like <code>"new york"</code>.
@ -64,10 +65,10 @@ public class PhraseQuery extends Query {
int position = 0;
if(positions.size() > 0)
position = ((Integer) positions.lastElement()).intValue() + 1;
add(term, position);
}
/**
* Adds a term to the end of the query phrase.
* The relative position of the term within the phrase is specified explicitly.
@ -82,7 +83,7 @@ public class PhraseQuery extends Query {
field = term.field();
else if (term.field() != field)
throw new IllegalArgumentException("All phrase terms must be in the same field: " + term);
terms.addElement(term);
positions.addElement(new Integer(position));
}
@ -91,7 +92,7 @@ public class PhraseQuery extends Query {
public Term[] getTerms() {
return (Term[])terms.toArray(new Term[0]);
}
/**
* Returns the relative positions of terms in this phrase.
*/
@ -151,7 +152,7 @@ public class PhraseQuery extends Query {
return
new SloppyPhraseScorer(this, tps, getPositions(), similarity, slop,
reader.norms(field));
}
public Explanation explain(IndexReader reader, int doc)
@ -181,7 +182,7 @@ public class PhraseQuery extends Query {
Explanation idfExpl =
new Explanation(idf, "idf(" + field + ": " + docFreqs + ")");
// explain query weight
Explanation queryExpl = new Explanation();
queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");
@ -190,16 +191,16 @@ public class PhraseQuery extends Query {
if (getBoost() != 1.0f)
queryExpl.addDetail(boostExpl);
queryExpl.addDetail(idfExpl);
Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");
queryExpl.addDetail(queryNormExpl);
queryExpl.setValue(boostExpl.getValue() *
idfExpl.getValue() *
queryNormExpl.getValue());
result.addDetail(queryExpl);
// explain field weight
Explanation fieldExpl = new Explanation();
fieldExpl.setDescription("fieldWeight("+field+":"+query+" in "+doc+
@ -220,7 +221,7 @@ public class PhraseQuery extends Query {
fieldExpl.setValue(tfExpl.getValue() *
idfExpl.getValue() *
fieldNormExpl.getValue());
result.addDetail(fieldExpl);
// combine them
@ -262,7 +263,7 @@ public class PhraseQuery extends Query {
for (int i = 0; i < terms.size(); i++) {
buffer.append(((Term)terms.elementAt(i)).text());
if (i != terms.size()-1)
buffer.append(" ");
buffer.append(" ");
}
buffer.append("\"");
@ -271,10 +272,7 @@ public class PhraseQuery extends Query {
buffer.append(slop);
}
if (getBoost() != 1.0f) {
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
}

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
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
* 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('*');
if (getBoost() != 1.0f) {
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
}

View File

@ -21,6 +21,7 @@ import java.io.IOException;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.ToStringUtils;
/**
* 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(upperTerm != null ? upperTerm.text() : "null");
buffer.append(inclusive ? "]" : "}");
if (getBoost() != 1.0f)
{
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
}

View File

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

View File

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

View File

@ -26,6 +26,7 @@ import java.util.Iterator;
import org.apache.lucene.index.IndexReader;
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
* maximum number of intervening unmatched positions, as well as whether
@ -98,6 +99,7 @@ public class SpanNearQuery extends SpanQuery {
buffer.append(", ");
buffer.append(inOrder);
buffer.append(")");
buffer.append(ToStringUtils.boost(getBoost()));
return buffer.toString();
}
@ -141,7 +143,7 @@ public class SpanNearQuery extends SpanQuery {
if (!clauses.equals(spanNearQuery.clauses)) return false;
if (!field.equals(spanNearQuery.field)) return false;
return true;
return getBoost() == spanNearQuery.getBoost();
}
public int hashCode() {

View File

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

View File

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

View File

@ -24,6 +24,7 @@ import java.util.ArrayList;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermPositions;
import org.apache.lucene.util.ToStringUtils;
/** Matches spans containing a term. */
public class SpanTermQuery extends SpanQuery {
@ -44,10 +45,13 @@ public class SpanTermQuery extends SpanQuery {
}
public String toString(String field) {
StringBuffer buffer = new StringBuffer();
if (term.field().equals(field))
return term.text();
buffer.append(term.text());
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. */