LUCENE-1973: Remove Similarity deprecations

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@829002 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2009-10-23 11:25:48 +00:00
parent a08a64ddaf
commit 04d00642e4
9 changed files with 77 additions and 173 deletions

View File

@ -92,6 +92,8 @@ API Changes
is no backwards-break, only a change of the super class. Parameter
was deprecated and will be removed in a later version.
(DM Smith, Uwe Schindler)
* LUCENE-1973: Remove deprecated Similarity methods. (Uwe Schindler)
Bug fixes

View File

@ -42,7 +42,7 @@
<property name="Name" value="Lucene"/>
<property name="dev.version" value="3.0-dev"/>
<property name="version" value="${dev.version}"/>
<property name="compatibility.tag" value="lucene_2_9_back_compat_tests_20091022"/>
<property name="compatibility.tag" value="lucene_2_9_back_compat_tests_20091023"/>
<property name="spec.version" value="${version}"/>
<property name="year" value="2000-${current.year}"/>
<property name="final.name" value="lucene-${name}-${version}"/>

View File

@ -134,9 +134,10 @@ public class MultiPhraseQuery extends Query {
this.similarity = getSimilarity(searcher);
// compute idf
final int maxDoc = searcher.maxDoc();
for(final Term[] terms: termArrays) {
for (Term term: terms) {
idf += getSimilarity(searcher).idf(term, searcher);
idf += this.similarity.idf(searcher.docFreq(term), maxDoc);
}
}
}

View File

@ -704,27 +704,6 @@ public abstract class Similarity implements Serializable {
*/
public abstract float tf(float freq);
/** Computes a score factor for a simple term.
*
* <p>The default implementation is:<pre>
* return idf(searcher.docFreq(term), searcher.maxDoc());
* </pre>
*
* Note that {@link Searcher#maxDoc()} is used instead of
* {@link org.apache.lucene.index.IndexReader#numDocs() IndexReader#numDocs()} because also
* {@link Searcher#docFreq(Term)} is used, and when the latter
* is inaccurate, so is {@link Searcher#maxDoc()}, and in the same direction.
* In addition, {@link Searcher#maxDoc()} is more efficient to compute
*
* @param term the term in question
* @param searcher the document collection being searched
* @return a score factor for the term
* @deprecated see {@link #idfExplain(Term, Searcher)}
*/
public float idf(Term term, Searcher searcher) throws IOException {
return idf(searcher.docFreq(term), searcher.maxDoc());
}
/**
* Computes a score factor for a simple term and returns an explanation
* for that score factor.
@ -749,19 +728,6 @@ public abstract class Similarity implements Serializable {
* @throws IOException
*/
public IDFExplanation idfExplain(final Term term, final Searcher searcher) throws IOException {
if(supportedMethods.overridesTermIDF) {
final float idf = idf(term, searcher);
return new IDFExplanation() {
@Override
public float getIdf() {
return idf;
}
@Override
public String explain() {
return "Inexplicable";
}
};
}
final int df = searcher.docFreq(term);
final int max = searcher.maxDoc();
final float idf = idf(df, max);
@ -777,25 +743,6 @@ public abstract class Similarity implements Serializable {
}};
}
/** Computes a score factor for a phrase.
*
* <p>The default implementation sums the {@link #idf(Term,Searcher)} factor
* for each term in the phrase.
*
* @param terms the terms in the phrase
* @param searcher the document collection being searched
* @return idf score factor
* @deprecated see {@link #idfExplain(Collection, Searcher)}
*/
public float idf(Collection<Term> terms, Searcher searcher) throws IOException {
float idf = 0.0f;
for(final Term term: terms) {
idf += idf(term, searcher);
}
return idf;
}
/**
* Computes a score factor for a phrase.
*
@ -811,19 +758,6 @@ public abstract class Similarity implements Serializable {
* @throws IOException
*/
public IDFExplanation idfExplain(Collection<Term> terms, Searcher searcher) throws IOException {
if(supportedMethods.overridesCollectionIDF) {
final float idf = idf(terms, searcher);
return new IDFExplanation() {
@Override
public float getIdf() {
return idf;
}
@Override
public String explain() {
return "Inexplicable";
}
};
}
final int max = searcher.maxDoc();
float idf = 0.0f;
final StringBuilder exp = new StringBuilder();
@ -877,31 +811,6 @@ public abstract class Similarity implements Serializable {
*/
public abstract float coord(int overlap, int maxOverlap);
/**
* Calculate a scoring factor based on the data in the payload. Overriding implementations
* are responsible for interpreting what is in the payload. Lucene makes no assumptions about
* what is in the byte array.
* <p>
* The default implementation returns 1.
*
* @param fieldName The fieldName of the term this payload belongs to
* @param payload The payload byte array to be scored
* @param offset The offset into the payload array
* @param length The length in the array
* @return An implementation dependent float to be used as a scoring factor
*
* @deprecated See {@link #scorePayload(int, String, int, int, byte[], int, int)}
*/
//TODO: When removing this, set the default value below to return 1.
public float scorePayload(String fieldName, byte [] payload, int offset, int length)
{
//Do nothing
return 1;
}
/**
* Calculate a scoring factor based on the data in the payload. Overriding implementations
* are responsible for interpreting what is in the payload. Lucene makes no assumptions about
@ -921,46 +830,7 @@ public abstract class Similarity implements Serializable {
*/
public float scorePayload(int docId, String fieldName, int start, int end, byte [] payload, int offset, int length)
{
//TODO: When removing the deprecated scorePayload above, set this to return 1
return scorePayload(fieldName, payload, offset, length);
}
/** @deprecated Remove this when old API is removed! */
private final MethodSupport supportedMethods = getSupportedMethods(this.getClass());
/** @deprecated Remove this when old API is removed! */
private static final class MethodSupport implements Serializable {
final boolean overridesCollectionIDF, overridesTermIDF;
MethodSupport(Class<? extends Similarity> clazz) {
overridesCollectionIDF = isMethodOverridden(clazz, "idf", Collection.class, Searcher.class);
overridesTermIDF = isMethodOverridden(clazz, "idf", Term.class, Searcher.class);
}
private static boolean isMethodOverridden(Class<?> clazz, String name, Class... params) {
try {
return clazz.getMethod(name, params).getDeclaringClass() != Similarity.class;
} catch (NoSuchMethodException e) {
// should not happen
throw new RuntimeException(e);
}
}
}
/** @deprecated Remove this when old API is removed! */
private static final IdentityHashMap<Class<? extends Similarity>,MethodSupport> knownMethodSupport
= new IdentityHashMap<Class<? extends Similarity>,MethodSupport>();
/** @deprecated Remove this when old API is removed! */
private static MethodSupport getSupportedMethods(Class<? extends Similarity> clazz) {
MethodSupport supportedMethods;
synchronized(knownMethodSupport) {
supportedMethods = (MethodSupport) knownMethodSupport.get(clazz);
if (supportedMethods == null) {
knownMethodSupport.put(clazz, supportedMethods = new MethodSupport(clazz));
}
}
return supportedMethods;
return 1;
}
/** The Similarity implementation used by default.

View File

@ -62,7 +62,7 @@ public class SimilarityDelegator extends Similarity {
return delegee.coord(overlap, maxOverlap);
}
public float scorePayload(String fieldName, byte[] payload, int offset, int length) {
return delegee.scorePayload(fieldName, payload, offset, length);
public float scorePayload(int docId, String fieldName, int start, int end, byte [] payload, int offset, int length) {
return delegee.scorePayload(docId, fieldName, start, end, payload, offset, length);
}
}

View File

@ -36,23 +36,32 @@ import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.MockRAMDirectory;
import org.apache.lucene.search.Explanation.IDFExplanation;
public class TestOmitTf extends LuceneTestCase {
public static class SimpleSimilarity extends Similarity {
public float lengthNorm(String field, int numTerms) { return 1.0f; }
public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }
public float tf(float freq) { return freq; }
public float sloppyFreq(int distance) { return 2.0f; }
public float idf(Collection terms, Searcher searcher) { return 1.0f; }
public float idf(int docFreq, int numDocs) { return 1.0f; }
public float coord(int overlap, int maxOverlap) { return 1.0f; }
@Override public float lengthNorm(String field, int numTerms) { return 1.0f; }
@Override public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }
@Override public float tf(float freq) { return freq; }
@Override public float sloppyFreq(int distance) { return 2.0f; }
@Override public float idf(int docFreq, int numDocs) { return 1.0f; }
@Override public float coord(int overlap, int maxOverlap) { return 1.0f; }
@Override public IDFExplanation idfExplain(Collection<Term> terms, Searcher searcher) throws IOException {
return new IDFExplanation() {
@Override
public float getIdf() {
return 1.0f;
}
@Override
public String explain() {
return "Inexplicable";
}
};
}
}
// Tests whether the DocumentWriter correctly enable the
// omitTermFreqAndPositions bit in the FieldInfo
public void testOmitTermFreqAndPositions() throws Exception {

View File

@ -29,6 +29,7 @@ import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.Explanation.IDFExplanation;
/** Similarity unit test.
*
@ -41,13 +42,24 @@ public class TestSimilarity extends LuceneTestCase {
}
public static class SimpleSimilarity extends Similarity {
public float lengthNorm(String field, int numTerms) { return 1.0f; }
public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }
public float tf(float freq) { return freq; }
public float sloppyFreq(int distance) { return 2.0f; }
public float idf(Collection terms, Searcher searcher) { return 1.0f; }
public float idf(int docFreq, int numDocs) { return 1.0f; }
public float coord(int overlap, int maxOverlap) { return 1.0f; }
@Override public float lengthNorm(String field, int numTerms) { return 1.0f; }
@Override public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }
@Override public float tf(float freq) { return freq; }
@Override public float sloppyFreq(int distance) { return 2.0f; }
@Override public float idf(int docFreq, int numDocs) { return 1.0f; }
@Override public float coord(int overlap, int maxOverlap) { return 1.0f; }
@Override public IDFExplanation idfExplain(Collection<Term> terms, Searcher searcher) throws IOException {
return new IDFExplanation() {
@Override
public float getIdf() {
return 1.0f;
}
@Override
public String explain() {
return "Inexplicable";
}
};
}
}
public void testSimilarity() throws Exception {
@ -80,7 +92,7 @@ public class TestSimilarity extends LuceneTestCase {
this.scorer = scorer;
}
public final void collect(int doc) throws IOException {
assertTrue(scorer.score() == 1.0f);
assertEquals(1.0f, scorer.score());
}
public void setNextReader(IndexReader reader, int docBase) {}
public boolean acceptsDocsOutOfOrder() {
@ -100,7 +112,7 @@ public class TestSimilarity extends LuceneTestCase {
}
public final void collect(int doc) throws IOException {
//System.out.println("Doc=" + doc + " score=" + score);
assertTrue(scorer.score() == (float)doc+base+1);
assertEquals((float)doc+base+1, scorer.score());
}
public void setNextReader(IndexReader reader, int docBase) {
base = docBase;
@ -114,8 +126,7 @@ public class TestSimilarity extends LuceneTestCase {
pq.add(a);
pq.add(c);
//System.out.println(pq.toString("field"));
searcher.search
(pq,
searcher.search(pq,
new Collector() {
private Scorer scorer;
public void setScorer(Scorer scorer) throws IOException {
@ -123,7 +134,7 @@ public class TestSimilarity extends LuceneTestCase {
}
public final void collect(int doc) throws IOException {
//System.out.println("Doc=" + doc + " score=" + score);
assertTrue(scorer.score() == 1.0f);
assertEquals(1.0f, scorer.score());
}
public void setNextReader(IndexReader reader, int docBase) {}
public boolean acceptsDocsOutOfOrder() {
@ -140,7 +151,7 @@ public class TestSimilarity extends LuceneTestCase {
}
public final void collect(int doc) throws IOException {
//System.out.println("Doc=" + doc + " score=" + score);
assertTrue(scorer.score() == 2.0f);
assertEquals(2.0f, scorer.score());
}
public void setNextReader(IndexReader reader, int docBase) {}
public boolean acceptsDocsOutOfOrder() {

View File

@ -258,7 +258,7 @@ public class TestTermVectors extends LuceneTestCase {
//System.out.println("Doc Id: " + docId + " freq " + freq);
TermFreqVector vector = knownSearcher.reader.getTermFreqVector(docId, "field");
float tf = sim.tf(freq);
float idf = sim.idf(term, knownSearcher);
float idf = sim.idf(knownSearcher.docFreq(term), knownSearcher.maxDoc());
//float qNorm = sim.queryNorm()
//This is fine since we don't have stop words
float lNorm = sim.lengthNorm("field", vector.getTerms().length);

View File

@ -41,6 +41,7 @@ import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.English;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.search.Explanation.IDFExplanation;
public class TestPayloadNearQuery extends LuceneTestCase {
@ -223,35 +224,45 @@ public class TestPayloadNearQuery extends LuceneTestCase {
// must be static for weight serialization tests
static class BoostingSimilarity extends DefaultSimilarity {
// TODO: Remove warning after API has been finalized
public float scorePayload(int docId, String fieldName, int start, int end, byte[] payload, int offset, int length) {
@Override public float scorePayload(int docId, String fieldName, int start, int end, byte[] payload, int offset, int length) {
//we know it is size 4 here, so ignore the offset/length
return payload[0];
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Make everything else 1 so we see the effect of the payload
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public float lengthNorm(String fieldName, int numTerms) {
return 1;
@Override public float lengthNorm(String fieldName, int numTerms) {
return 1.0f;
}
public float queryNorm(float sumOfSquaredWeights) {
return 1;
@Override public float queryNorm(float sumOfSquaredWeights) {
return 1.0f;
}
public float sloppyFreq(int distance) {
return 1;
@Override public float sloppyFreq(int distance) {
return 1.0f;
}
public float coord(int overlap, int maxOverlap) {
return 1;
@Override public float coord(int overlap, int maxOverlap) {
return 1.0f;
}
public float tf(float freq) {
return 1;
@Override public float tf(float freq) {
return 1.0f;
}
// idf used for phrase queries
public float idf(Collection terms, Searcher searcher) {
return 1;
@Override public IDFExplanation idfExplain(Collection<Term> terms, Searcher searcher) throws IOException {
return new IDFExplanation() {
@Override
public float getIdf() {
return 1.0f;
}
@Override
public String explain() {
return "Inexplicable";
}
};
}
}
}