Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Permit queries to override the Similarity implementation.

Doug Cutting755529c45c03
diff --git a/CHANGES.txt b/CHANGES.txt
index a37fa1a3753..16850350202 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -45,6 +45,11 @@ $Id$
7. Added a new span-based query API. This implements, among other
things, nested phrases. See javadocs for details. (Doug Cutting)
+ 8. Added new method Query.getSimilarity(Searcher), and changed
+ scorers to use it. This permits one to subclass a Query class so
+ that it can specify it's own Similarity implementation, perhaps
+ one that delegates through that of the Searcher.
+
1.3 final
diff --git a/src/java/org/apache/lucene/search/BooleanQuery.java b/src/java/org/apache/lucene/search/BooleanQuery.java
index 8cae4e632ff..1c7e7441379 100644
--- a/src/java/org/apache/lucene/search/BooleanQuery.java
+++ b/src/java/org/apache/lucene/search/BooleanQuery.java
@@ -176,7 +176,7 @@ public class BooleanQuery extends Query {
if (allRequired && noneBoolean) { // ConjunctionScorer is okay
ConjunctionScorer result =
- new ConjunctionScorer(searcher.getSimilarity());
+ new ConjunctionScorer(getSimilarity(searcher));
for (int i = 0 ; i < weights.size(); i++) {
BooleanClause c = (BooleanClause)clauses.elementAt(i);
Weight w = (Weight)weights.elementAt(i);
@@ -189,7 +189,7 @@ public class BooleanQuery extends Query {
}
// Use good-old BooleanScorer instead.
- BooleanScorer result = new BooleanScorer(searcher.getSimilarity());
+ BooleanScorer result = new BooleanScorer(getSimilarity(searcher));
for (int i = 0 ; i < weights.size(); i++) {
BooleanClause c = (BooleanClause)clauses.elementAt(i);
@@ -233,7 +233,7 @@ public class BooleanQuery extends Query {
if (coord == 1) // only one clause matched
sumExpl = sumExpl.getDetails()[0]; // eliminate wrapper
- float coordFactor = searcher.getSimilarity().coord(coord, maxCoord);
+ float coordFactor = getSimilarity(searcher).coord(coord, maxCoord);
if (coordFactor == 1.0f) // coord is no-op
return sumExpl; // eliminate wrapper
else {
diff --git a/src/java/org/apache/lucene/search/PhrasePrefixQuery.java b/src/java/org/apache/lucene/search/PhrasePrefixQuery.java
index 3d481c6df20..3d867b2a4c8 100644
--- a/src/java/org/apache/lucene/search/PhrasePrefixQuery.java
+++ b/src/java/org/apache/lucene/search/PhrasePrefixQuery.java
@@ -135,7 +135,7 @@ public class PhrasePrefixQuery extends Query {
while (i.hasNext()) {
Term[] terms = (Term[])i.next();
for (int j=0; j<terms.length; j++)
- idf += searcher.getSimilarity().idf(terms[j], searcher);
+ idf += getSimilarity(searcher).idf(terms[j], searcher);
}
queryWeight = idf * getBoost(); // compute query weight
@@ -169,10 +169,10 @@ public class PhrasePrefixQuery extends Query {
}
if (slop == 0)
- return new ExactPhraseScorer(this, tps, searcher.getSimilarity(),
+ return new ExactPhraseScorer(this, tps, getSimilarity(searcher),
reader.norms(field));
else
- return new SloppyPhraseScorer(this, tps, searcher.getSimilarity(),
+ return new SloppyPhraseScorer(this, tps, getSimilarity(searcher),
slop, reader.norms(field));
}
diff --git a/src/java/org/apache/lucene/search/PhraseQuery.java b/src/java/org/apache/lucene/search/PhraseQuery.java
index 4b7bdd2be7a..fa0180fe2c3 100644
--- a/src/java/org/apache/lucene/search/PhraseQuery.java
+++ b/src/java/org/apache/lucene/search/PhraseQuery.java
@@ -121,7 +121,7 @@ public class PhraseQuery extends Query {
public float getValue() { return value; }
public float sumOfSquaredWeights() throws IOException {
- idf = searcher.getSimilarity().idf(terms, searcher);
+ idf = getSimilarity(searcher).idf(terms, searcher);
queryWeight = idf * getBoost(); // compute query weight
return queryWeight * queryWeight; // square it
}
@@ -145,11 +145,11 @@ public class PhraseQuery extends Query {
}
if (slop == 0) // optimize exact case
- return new ExactPhraseScorer(this, tps, searcher.getSimilarity(),
+ return new ExactPhraseScorer(this, tps, getSimilarity(searcher),
reader.norms(field));
else
return
- new SloppyPhraseScorer(this, tps, searcher.getSimilarity(), slop,
+ new SloppyPhraseScorer(this, tps, getSimilarity(searcher), slop,
reader.norms(field));
}
diff --git a/src/java/org/apache/lucene/search/Query.java b/src/java/org/apache/lucene/search/Query.java
index 9f695783835..b71717e6cf9 100644
--- a/src/java/org/apache/lucene/search/Query.java
+++ b/src/java/org/apache/lucene/search/Query.java
@@ -120,7 +120,7 @@ public abstract class Query implements java.io.Serializable, Cloneable {
Query query = searcher.rewrite(this);
Weight weight = query.createWeight(searcher);
float sum = weight.sumOfSquaredWeights();
- float norm = searcher.getSimilarity().queryNorm(sum);
+ float norm = getSimilarity(searcher).queryNorm(sum);
weight.normalize(norm);
return weight;
}
@@ -162,6 +162,15 @@ public abstract class Query implements java.io.Serializable, Cloneable {
return result;
}
+ /** Expert: Returns the Similarity implementation to be used for this query.
+ * Subclasses may override this method to specify their own Similarity
+ * implementation, perhaps one that delegates through that of the Searcher.
+ * By default the Searcher's Similarity implementation is returned.*/
+ public Similarity getSimilarity(Searcher searcher) {
+ return searcher.getSimilarity();
+ }
+
+
/** Returns a clone of this query. */
public Object clone() {
diff --git a/src/java/org/apache/lucene/search/TermQuery.java b/src/java/org/apache/lucene/search/TermQuery.java
index 631be64ec2d..9625d91c09d 100644
--- a/src/java/org/apache/lucene/search/TermQuery.java
+++ b/src/java/org/apache/lucene/search/TermQuery.java
@@ -80,7 +80,7 @@ public class TermQuery extends Query {
public float getValue() { return value; }
public float sumOfSquaredWeights() throws IOException {
- idf = searcher.getSimilarity().idf(term, searcher); // compute idf
+ idf = getSimilarity(searcher).idf(term, searcher); // compute idf
queryWeight = idf * getBoost(); // compute query weight
return queryWeight * queryWeight; // square it
}
@@ -97,7 +97,7 @@ public class TermQuery extends Query {
if (termDocs == null)
return null;
- return new TermScorer(this, termDocs, searcher.getSimilarity(),
+ return new TermScorer(this, termDocs, getSimilarity(searcher),
reader.norms(term.field()));
}
diff --git a/src/java/org/apache/lucene/search/spans/SpanWeight.java b/src/java/org/apache/lucene/search/spans/SpanWeight.java
index a1159205745..2a6aba75dc6 100644
--- a/src/java/org/apache/lucene/search/spans/SpanWeight.java
+++ b/src/java/org/apache/lucene/search/spans/SpanWeight.java
@@ -51,7 +51,7 @@ class SpanWeight implements Weight {
public float getValue() { return value; }
public float sumOfSquaredWeights() throws IOException {
- idf = searcher.getSimilarity().idf(terms, searcher);
+ idf = this.query.getSimilarity(searcher).idf(terms, searcher);
queryWeight = idf * query.getBoost(); // compute query weight
return queryWeight * queryWeight; // square it
}
@@ -64,7 +64,7 @@ class SpanWeight implements Weight {
public Scorer scorer(IndexReader reader) throws IOException {
return new SpanScorer(query.getSpans(reader), this,
- searcher.getSimilarity(),
+ query.getSimilarity(searcher),
reader.norms(query.getField()));
}