LUCENE-2986: split DefaultSimilarity's provider methods into DefaultSimilarityProvider; clean up uses of sim in tests

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1085374 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2011-03-25 14:00:42 +00:00
parent 269b2c5938
commit 0934039ced
23 changed files with 318 additions and 204 deletions

View File

@ -26,8 +26,10 @@ import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.DefaultSimilarityProvider;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
@ -42,10 +44,15 @@ public class TestFieldNormModifier extends LuceneTestCase {
public Directory store;
/** inverts the normal notion of lengthNorm */
public static SimilarityProvider s = new DefaultSimilarity() {
public static SimilarityProvider s = new DefaultSimilarityProvider() {
@Override
public float computeNorm(FieldInvertState state) {
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
public Similarity get(String field) {
return new DefaultSimilarity() {
@Override
public float computeNorm(FieldInvertState state) {
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
}
};
}
};

View File

@ -19,6 +19,7 @@
package org.apache.lucene.misc;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.DefaultSimilarityProvider;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.util.LuceneTestCase;
@ -84,7 +85,7 @@ public class SweetSpotSimilarityTest extends LuceneTestCase {
final SweetSpotSimilarity ssB = new SweetSpotSimilarity();
ssB.setLengthNormFactors(5,8,0.1f, false);
SimilarityProvider sp = new SweetSpotSimilarity() {
SimilarityProvider sp = new DefaultSimilarityProvider() {
public Similarity get(String field) {
if (field.equals("bar"))
return ssBar;

View File

@ -31,8 +31,10 @@ import org.apache.lucene.index.MultiNorms;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.DefaultSimilarityProvider;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
@ -47,12 +49,17 @@ public class TestLengthNormModifier extends LuceneTestCase {
public Directory store;
/** inverts the normal notion of lengthNorm */
public static SimilarityProvider s = new DefaultSimilarity() {
@Override
public float computeNorm(FieldInvertState state) {
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
}
};
public static SimilarityProvider s = new DefaultSimilarityProvider() {
@Override
public Similarity get(String field) {
return new DefaultSimilarity() {
@Override
public float computeNorm(FieldInvertState state) {
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
}
};
}
};
@Override
public void setUp() throws Exception {
@ -163,12 +170,18 @@ public class TestLengthNormModifier extends LuceneTestCase {
}
// override the norms to be inverted
SimilarityProvider s = new DefaultSimilarity() {
@Override
public float computeNorm(FieldInvertState state) {
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
}
};
SimilarityProvider s = new DefaultSimilarityProvider() {
@Override
public Similarity get(String field) {
return new DefaultSimilarity() {
@Override
public float computeNorm(FieldInvertState state) {
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
}
};
}
};
FieldNormModifier fnm = new FieldNormModifier(store, s);
fnm.reSetNorms("field");

View File

@ -20,7 +20,7 @@ import org.apache.lucene.index.FieldInvertState;
*/
/** Expert: Default scoring implementation. */
public class DefaultSimilarity extends Similarity implements SimilarityProvider {
public class DefaultSimilarity extends Similarity {
/** Implemented as
* <code>state.getBoost()*lengthNorm(numTerms)</code>, where
@ -40,11 +40,6 @@ public class DefaultSimilarity extends Similarity implements SimilarityProvider
return state.getBoost() * ((float) (1.0 / Math.sqrt(numTerms)));
}
/** Implemented as <code>1/sqrt(sumOfSquaredWeights)</code>. */
public float queryNorm(float sumOfSquaredWeights) {
return (float)(1.0 / Math.sqrt(sumOfSquaredWeights));
}
/** Implemented as <code>sqrt(freq)</code>. */
@Override
public float tf(float freq) {
@ -63,11 +58,6 @@ public class DefaultSimilarity extends Similarity implements SimilarityProvider
return (float)(Math.log(numDocs/(double)(docFreq+1)) + 1.0);
}
/** Implemented as <code>overlap / maxOverlap</code>. */
public float coord(int overlap, int maxOverlap) {
return overlap / (float)maxOverlap;
}
// Default true
protected boolean discountOverlaps = true;
@ -88,12 +78,4 @@ public class DefaultSimilarity extends Similarity implements SimilarityProvider
public boolean getDiscountOverlaps() {
return discountOverlaps;
}
/**
* Returns this default implementation for all fields.
* Override this method to customize scoring on a per-field basis.
*/
public Similarity get(String field) {
return this;
}
}

View File

@ -0,0 +1,41 @@
package org.apache.lucene.search;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Expert: Default scoring provider.
* <p>
* Returns {@link DefaultSimilarity} for every field
*/
public class DefaultSimilarityProvider implements SimilarityProvider {
private static final Similarity impl = new DefaultSimilarity();
/** Implemented as <code>overlap / maxOverlap</code>. */
public float coord(int overlap, int maxOverlap) {
return overlap / (float)maxOverlap;
}
/** Implemented as <code>1/sqrt(sumOfSquaredWeights)</code>. */
public float queryNorm(float sumOfSquaredWeights) {
return (float)(1.0 / Math.sqrt(sumOfSquaredWeights));
}
public Similarity get(String field) {
return impl;
}
}

View File

@ -72,7 +72,7 @@ public class IndexSearcher {
private final ExecutorService executor;
// the default SimilarityProvider
private static final SimilarityProvider defaultProvider = new DefaultSimilarity();
private static final SimilarityProvider defaultProvider = new DefaultSimilarityProvider();
/**
* Expert: returns a default SimilarityProvider instance.

View File

@ -362,7 +362,7 @@ import org.apache.lucene.util.SmallFloat;
* than another document with fewer query terms.
* This is a search time factor computed in
* {@link SimilarityProvider#coord(int, int) coord(q,d)}
* by the Similarity in effect at search time.
* by the SimilarityProvider in effect at search time.
* <br>&nbsp;<br>
* </li>
*
@ -373,17 +373,17 @@ import org.apache.lucene.util.SmallFloat;
* is a normalizing factor used to make scores between queries comparable.
* This factor does not affect document ranking (since all ranked documents are multiplied by the same factor),
* but rather just attempts to make scores from different queries (or even different indexes) comparable.
* This is a search time factor computed by the Similarity in effect at search time.
* This is a search time factor computed by the SimilarityProvider in effect at search time.
*
* The default computation in
* {@link org.apache.lucene.search.DefaultSimilarity#queryNorm(float) DefaultSimilarity}
* {@link org.apache.lucene.search.DefaultSimilarityProvider#queryNorm(float) DefaultSimilarityProvider}
* produces a <a href="http://en.wikipedia.org/wiki/Euclidean_norm#Euclidean_norm">Euclidean norm</a>:
* <br>&nbsp;<br>
* <table cellpadding="1" cellspacing="0" border="0" align="center">
* <tr>
* <td valign="middle" align="right" rowspan="1">
* queryNorm(q) &nbsp; = &nbsp;
* {@link org.apache.lucene.search.DefaultSimilarity#queryNorm(float) queryNorm(sumOfSquaredWeights)}
* {@link org.apache.lucene.search.DefaultSimilarityProvider#queryNorm(float) queryNorm(sumOfSquaredWeights)}
* &nbsp; = &nbsp;
* </td>
* <td valign="middle" align="center" rowspan="1">

View File

@ -31,6 +31,7 @@ import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.SegmentReader.Norm;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.DefaultSimilarityProvider;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.store.Directory;
@ -41,17 +42,22 @@ import org.apache.lucene.util.LuceneTestCase;
*/
public class TestIndexReaderCloneNorms extends LuceneTestCase {
private class SimilarityOne extends DefaultSimilarity {
private class SimilarityProviderOne extends DefaultSimilarityProvider {
@Override
public float computeNorm(FieldInvertState state) {
// diable length norm
return state.getBoost();
public Similarity get(String field) {
return new DefaultSimilarity() {
@Override
public float computeNorm(FieldInvertState state) {
// diable length norm
return state.getBoost();
}
};
}
}
private static final int NUM_FIELDS = 10;
private SimilarityProvider similarityOne;
private SimilarityProvider similarityProviderOne;
private Analyzer anlzr;
@ -68,7 +74,7 @@ public class TestIndexReaderCloneNorms extends LuceneTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
similarityOne = new SimilarityOne();
similarityProviderOne = new SimilarityProviderOne();
anlzr = new MockAnalyzer();
}
@ -249,7 +255,7 @@ public class TestIndexReaderCloneNorms extends LuceneTestCase {
}
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
TEST_VERSION_CURRENT, anlzr).setOpenMode(OpenMode.CREATE)
.setMaxBufferedDocs(5).setSimilarityProvider(similarityOne).setMergePolicy(newLogMergePolicy()));
.setMaxBufferedDocs(5).setSimilarityProvider(similarityProviderOne).setMergePolicy(newLogMergePolicy()));
LogMergePolicy lmp = (LogMergePolicy) iw.getConfig().getMergePolicy();
lmp.setMergeFactor(3);
@ -307,7 +313,7 @@ public class TestIndexReaderCloneNorms extends LuceneTestCase {
throws IOException {
IndexWriterConfig conf = newIndexWriterConfig(
TEST_VERSION_CURRENT, anlzr).setOpenMode(OpenMode.APPEND)
.setMaxBufferedDocs(5).setSimilarityProvider(similarityOne).setMergePolicy(newLogMergePolicy());
.setMaxBufferedDocs(5).setSimilarityProvider(similarityProviderOne).setMergePolicy(newLogMergePolicy());
LogMergePolicy lmp = (LogMergePolicy) conf.getMergePolicy();
lmp.setMergeFactor(3);
lmp.setUseCompoundFile(compound);

View File

@ -26,14 +26,14 @@ import java.util.Set;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.index.DocumentsWriter.IndexingChain;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.DefaultSimilarityProvider;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.Test;
public class TestIndexWriterConfig extends LuceneTestCase {
private static final class MySimilarity extends DefaultSimilarity {
private static final class MySimilarityProvider extends DefaultSimilarityProvider {
// Does not implement anything - used only for type checking on IndexWriterConfig.
}
@ -177,8 +177,8 @@ public class TestIndexWriterConfig extends LuceneTestCase {
// Test Similarity:
// we shouldnt assert what the default is, just that its not null.
assertTrue(IndexSearcher.getDefaultSimilarityProvider() == conf.getSimilarityProvider());
conf.setSimilarityProvider(new MySimilarity());
assertEquals(MySimilarity.class, conf.getSimilarityProvider().getClass());
conf.setSimilarityProvider(new MySimilarityProvider());
assertEquals(MySimilarityProvider.class, conf.getSimilarityProvider().getClass());
conf.setSimilarityProvider(null);
assertTrue(IndexSearcher.getDefaultSimilarityProvider() == conf.getSimilarityProvider());

View File

@ -27,6 +27,8 @@ import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.DefaultSimilarityProvider;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
@ -46,7 +48,12 @@ public class TestMaxTermFrequency extends LuceneTestCase {
dir = newDirectory();
IndexWriterConfig config = newIndexWriterConfig(TEST_VERSION_CURRENT,
new MockAnalyzer(MockTokenizer.SIMPLE, true)).setMergePolicy(newInOrderLogMergePolicy());
config.setSimilarityProvider(new TestSimilarity());
config.setSimilarityProvider(new DefaultSimilarityProvider() {
@Override
public Similarity get(String field) {
return new TestSimilarity();
}
});
RandomIndexWriter writer = new RandomIndexWriter(random, dir, config);
Document doc = new Document();
Field foo = newField("foo", "", Field.Store.NO, Field.Index.ANALYZED);

View File

@ -29,6 +29,7 @@ import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.DefaultSimilarityProvider;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.store.Directory;
@ -40,17 +41,22 @@ import org.apache.lucene.util.LuceneTestCase;
*/
public class TestNorms extends LuceneTestCase {
private class SimilarityOne extends DefaultSimilarity {
private class SimilarityProviderOne extends DefaultSimilarityProvider {
@Override
public float computeNorm(FieldInvertState state) {
// Disable length norm
return state.getBoost();
public Similarity get(String field) {
return new DefaultSimilarity() {
@Override
public float computeNorm(FieldInvertState state) {
// diable length norm
return state.getBoost();
}
};
}
}
private static final int NUM_FIELDS = 10;
private SimilarityProvider similarityOne;
private SimilarityProvider similarityProviderOne;
private Analyzer anlzr;
private int numDocNorms;
private ArrayList<Float> norms;
@ -61,7 +67,7 @@ public class TestNorms extends LuceneTestCase {
@Override
public void setUp() throws Exception {
super.setUp();
similarityOne = new SimilarityOne();
similarityProviderOne = new SimilarityProviderOne();
anlzr = new MockAnalyzer();
}
@ -152,7 +158,7 @@ public class TestNorms extends LuceneTestCase {
private void createIndex(Random random, Directory dir) throws IOException {
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
TEST_VERSION_CURRENT, anlzr).setOpenMode(OpenMode.CREATE)
.setMaxBufferedDocs(5).setSimilarityProvider(similarityOne).setMergePolicy(newInOrderLogMergePolicy()));
.setMaxBufferedDocs(5).setSimilarityProvider(similarityProviderOne).setMergePolicy(newInOrderLogMergePolicy()));
LogMergePolicy lmp = (LogMergePolicy) iw.getConfig().getMergePolicy();
lmp.setMergeFactor(3);
lmp.setUseCompoundFile(true);
@ -186,7 +192,7 @@ public class TestNorms extends LuceneTestCase {
assertEquals("number of norms mismatches",numDocNorms,b.length);
ArrayList<Float> storedNorms = (i==1 ? modifiedNorms : norms);
for (int j = 0; j < b.length; j++) {
float norm = similarityOne.get(field).decodeNormValue(b[j]);
float norm = similarityProviderOne.get(field).decodeNormValue(b[j]);
float norm1 = storedNorms.get(j).floatValue();
assertEquals("stored norm value of "+field+" for doc "+j+" is "+norm+" - a mismatch!", norm, norm1, 0.000001);
}
@ -197,7 +203,7 @@ public class TestNorms extends LuceneTestCase {
private void addDocs(Random random, Directory dir, int ndocs, boolean compound) throws IOException {
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
TEST_VERSION_CURRENT, anlzr).setOpenMode(OpenMode.APPEND)
.setMaxBufferedDocs(5).setSimilarityProvider(similarityOne).setMergePolicy(newInOrderLogMergePolicy()));
.setMaxBufferedDocs(5).setSimilarityProvider(similarityProviderOne).setMergePolicy(newInOrderLogMergePolicy()));
LogMergePolicy lmp = (LogMergePolicy) iw.getConfig().getMergePolicy();
lmp.setMergeFactor(3);
lmp.setUseCompoundFile(compound);
@ -222,7 +228,7 @@ public class TestNorms extends LuceneTestCase {
// return unique norm values that are unchanged by encoding/decoding
private float nextNorm(String fname) {
float norm = lastNorm + normDelta;
Similarity similarity = similarityOne.get(fname);
Similarity similarity = similarityProviderOne.get(fname);
do {
float norm1 = similarity.decodeNormValue(similarity.encodeNormValue(norm));
if (norm1 > lastNorm) {
@ -261,7 +267,12 @@ public class TestNorms extends LuceneTestCase {
public void testCustomEncoder() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig config = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer());
config.setSimilarityProvider(new CustomNormEncodingSimilarity());
config.setSimilarityProvider(new DefaultSimilarityProvider() {
@Override
public Similarity get(String field) {
return new CustomNormEncodingSimilarity();
}
});
RandomIndexWriter writer = new RandomIndexWriter(random, dir, config);
Document doc = new Document();
Field foo = newField("foo", "", Field.Store.NO, Field.Index.ANALYZED);

View File

@ -35,27 +35,29 @@ import org.apache.lucene.search.Explanation.IDFExplanation;
public class TestOmitTf extends LuceneTestCase {
public static class SimpleSimilarity extends Similarity implements SimilarityProvider {
@Override public float computeNorm(FieldInvertState state) { return state.getBoost(); }
@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 IDFExplanation idfExplain(Collection<Term> terms, IndexSearcher searcher) throws IOException {
return new IDFExplanation() {
@Override
public float getIdf() {
return 1.0f;
}
@Override
public String explain() {
return "Inexplicable";
}
};
}
public static class SimpleSimilarityProvider implements SimilarityProvider {
public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }
public float coord(int overlap, int maxOverlap) { return 1.0f; }
public Similarity get(String field) {
return this;
return new Similarity() {
@Override public float computeNorm(FieldInvertState state) { return state.getBoost(); }
@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 IDFExplanation idfExplain(Collection<Term> terms, IndexSearcher searcher) throws IOException {
return new IDFExplanation() {
@Override
public float getIdf() {
return 1.0f;
}
@Override
public String explain() {
return "Inexplicable";
}
};
}
};
}
}
@ -254,7 +256,7 @@ public class TestOmitTf extends LuceneTestCase {
dir,
newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).
setMaxBufferedDocs(2).
setSimilarityProvider(new SimpleSimilarity()).
setSimilarityProvider(new SimpleSimilarityProvider()).
setMergePolicy(newInOrderLogMergePolicy(2))
);
writer.setInfoStream(VERBOSE ? System.out : null);
@ -285,7 +287,7 @@ public class TestOmitTf extends LuceneTestCase {
* Verify the index
*/
IndexSearcher searcher = new IndexSearcher(dir, true);
searcher.setSimilarityProvider(new SimpleSimilarity());
searcher.setSimilarityProvider(new SimpleSimilarityProvider());
Term a = new Term("noTf", term);
Term b = new Term("tf", term);

View File

@ -210,7 +210,7 @@ public class TestBoolean2 extends LuceneTestCase {
int[] expDocNrs = {2, 3};
SimilarityProvider oldSimilarity = searcher.getSimilarityProvider();
try {
searcher.setSimilarityProvider(new DefaultSimilarity(){
searcher.setSimilarityProvider(new DefaultSimilarityProvider(){
@Override
public float coord(int overlap, int maxOverlap) {
return overlap / ((float)maxOverlap - 1);

View File

@ -44,8 +44,8 @@ public class TestComplexExplanations extends TestExplanations {
}
// must be static for weight serialization tests
private static DefaultSimilarity createQnorm1Similarity() {
return new DefaultSimilarity() {
private static DefaultSimilarityProvider createQnorm1Similarity() {
return new DefaultSimilarityProvider() {
@Override
public float queryNorm(float sumOfSquaredWeights) {
return 1.0f; // / (float) Math.sqrt(1.0f + sumOfSquaredWeights);

View File

@ -97,7 +97,7 @@ public class TestConstantScoreQuery extends LuceneTestCase {
searcher = newSearcher(reader);
// set a similarity that does not normalize our boost away
searcher.setSimilarityProvider(new DefaultSimilarity() {
searcher.setSimilarityProvider(new DefaultSimilarityProvider() {
@Override
public float queryNorm(float sumOfSquaredWeights) {
return 1.0f;

View File

@ -73,7 +73,12 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
}
}
public SimilarityProvider sim = new TestSimilarity();
public SimilarityProvider sim = new DefaultSimilarityProvider() {
@Override
public Similarity get(String field) {
return new TestSimilarity();
}
};
public Directory index;
public IndexReader r;
public IndexSearcher s;

View File

@ -296,23 +296,28 @@ public class TestMultiPhraseQuery extends LuceneTestCase {
IndexReader reader = writer.getReader();
IndexSearcher searcher = newSearcher(reader);
searcher.setSimilarityProvider(new DefaultSimilarity() {
searcher.setSimilarityProvider(new DefaultSimilarityProvider() {
@Override
public IDFExplanation idfExplain(Collection<Term> terms,
IndexSearcher searcher) throws IOException {
return new IDFExplanation() {
public Similarity get(String field) {
return new DefaultSimilarity() {
@Override
public float getIdf() {
return 10f;
}
public IDFExplanation idfExplain(Collection<Term> terms,
IndexSearcher searcher) throws IOException {
return new IDFExplanation() {
@Override
public String explain() {
return "just a test";
}
@Override
public float getIdf() {
return 10f;
}
@Override
public String explain() {
return "just a test";
}
};
}
};
}
});

View File

@ -39,27 +39,28 @@ import org.apache.lucene.search.Explanation.IDFExplanation;
*/
public class TestSimilarity extends LuceneTestCase {
public static class SimpleSimilarity extends Similarity implements SimilarityProvider {
@Override public float computeNorm(FieldInvertState state) { return state.getBoost(); }
@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 IDFExplanation idfExplain(Collection<Term> terms, IndexSearcher searcher) throws IOException {
return new IDFExplanation() {
@Override
public float getIdf() {
return 1.0f;
}
@Override
public String explain() {
return "Inexplicable";
}
};
}
public static class SimpleSimilarityProvider implements SimilarityProvider {
public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }
public float coord(int overlap, int maxOverlap) { return 1.0f; }
public Similarity get(String field) {
return this;
return new Similarity() {
@Override public float computeNorm(FieldInvertState state) { return state.getBoost(); }
@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 IDFExplanation idfExplain(Collection<Term> terms, IndexSearcher searcher) throws IOException {
return new IDFExplanation() {
@Override
public float getIdf() {
return 1.0f;
}
@Override
public String explain() {
return "Inexplicable";
}
};
}
};
}
}
@ -67,7 +68,7 @@ public class TestSimilarity extends LuceneTestCase {
Directory store = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random, store,
newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer())
.setSimilarityProvider(new SimpleSimilarity()));
.setSimilarityProvider(new SimpleSimilarityProvider()));
Document d1 = new Document();
d1.add(newField("field", "a c", Field.Store.YES, Field.Index.ANALYZED));
@ -81,7 +82,7 @@ public class TestSimilarity extends LuceneTestCase {
writer.close();
IndexSearcher searcher = newSearcher(reader);
searcher.setSimilarityProvider(new SimpleSimilarity());
searcher.setSimilarityProvider(new SimpleSimilarityProvider());
Term a = new Term("field", "a");
Term b = new Term("field", "b");

View File

@ -36,6 +36,8 @@ import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.QueryUtils;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanNearQuery;
@ -50,7 +52,7 @@ public class TestPayloadNearQuery extends LuceneTestCase {
private IndexSearcher searcher;
private IndexReader reader;
private Directory directory;
private BoostingSimilarity similarity = new BoostingSimilarity();
private BoostingSimilarityProvider similarityProvider = new BoostingSimilarityProvider();
private byte[] payload2 = new byte[]{2};
private byte[] payload4 = new byte[]{4};
@ -105,7 +107,7 @@ public class TestPayloadNearQuery extends LuceneTestCase {
directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random, directory,
newIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer())
.setSimilarityProvider(similarity));
.setSimilarityProvider(similarityProvider));
//writer.infoStream = System.out;
for (int i = 0; i < 1000; i++) {
Document doc = new Document();
@ -118,7 +120,7 @@ public class TestPayloadNearQuery extends LuceneTestCase {
writer.close();
searcher = newSearcher(reader);
searcher.setSimilarityProvider(similarity);
searcher.setSimilarityProvider(similarityProvider);
}
@Override
@ -297,43 +299,57 @@ public class TestPayloadNearQuery extends LuceneTestCase {
}
// must be static for weight serialization tests
static class BoostingSimilarity extends DefaultSimilarity {
static class BoostingSimilarityProvider implements SimilarityProvider {
@Override public float scorePayload(int docId, int start, int end, byte[] payload, int offset, int length) {
//we know it is size 4 here, so ignore the offset/length
return payload[offset];
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Make everything else 1 so we see the effect of the payload
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@Override public float computeNorm(FieldInvertState state) {
return state.getBoost();
}
@Override public float queryNorm(float sumOfSquaredWeights) {
public float queryNorm(float sumOfSquaredWeights) {
return 1.0f;
}
@Override public float sloppyFreq(int distance) {
public float coord(int overlap, int maxOverlap) {
return 1.0f;
}
@Override public float coord(int overlap, int maxOverlap) {
return 1.0f;
}
@Override public float tf(float freq) {
return 1.0f;
}
// idf used for phrase queries
@Override public IDFExplanation idfExplain(Collection<Term> terms, IndexSearcher searcher) throws IOException {
return new IDFExplanation() {
public Similarity get(String field) {
return new DefaultSimilarity() {
@Override
public float getIdf() {
public float scorePayload(int docId, int start, int end, byte[] payload, int offset, int length) {
//we know it is size 4 here, so ignore the offset/length
return payload[offset];
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Make everything else 1 so we see the effect of the payload
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@Override
public float computeNorm(FieldInvertState state) {
return state.getBoost();
}
@Override
public float sloppyFreq(int distance) {
return 1.0f;
}
@Override
public String explain() {
return "Inexplicable";
public float tf(float freq) {
return 1.0f;
}
// idf used for phrase queries
@Override
public IDFExplanation idfExplain(Collection<Term> terms, IndexSearcher searcher) throws IOException {
return new IDFExplanation() {
@Override
public float getIdf() {
return 1.0f;
}
@Override
public String explain() {
return "Inexplicable";
}
};
}
};
}

View File

@ -18,8 +18,11 @@ package org.apache.lucene.search.payloads;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.English;
import org.apache.lucene.search.DefaultSimilarityProvider;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.QueryUtils;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.CheckHits;
@ -54,7 +57,7 @@ import java.io.IOException;
public class TestPayloadTermQuery extends LuceneTestCase {
private IndexSearcher searcher;
private IndexReader reader;
private BoostingSimilarity similarity = new BoostingSimilarity();
private SimilarityProvider similarityProvider = new BoostingSimilarityProvider();
private byte[] payloadField = new byte[]{1};
private byte[] payloadMultiField1 = new byte[]{2};
private byte[] payloadMultiField2 = new byte[]{4};
@ -110,7 +113,7 @@ public class TestPayloadTermQuery extends LuceneTestCase {
directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random, directory,
newIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer())
.setSimilarityProvider(similarity).setMergePolicy(newInOrderLogMergePolicy()));
.setSimilarityProvider(similarityProvider).setMergePolicy(newInOrderLogMergePolicy()));
//writer.infoStream = System.out;
for (int i = 0; i < 1000; i++) {
Document doc = new Document();
@ -125,7 +128,7 @@ public class TestPayloadTermQuery extends LuceneTestCase {
writer.close();
searcher = newSearcher(reader);
searcher.setSimilarityProvider(similarity);
searcher.setSimilarityProvider(similarityProvider);
}
@Override
@ -220,7 +223,12 @@ public class TestPayloadTermQuery extends LuceneTestCase {
new MaxPayloadFunction(), false);
IndexSearcher theSearcher = new IndexSearcher(directory, true);
theSearcher.setSimilarityProvider(new FullSimilarity());
theSearcher.setSimilarityProvider(new DefaultSimilarityProvider() {
@Override
public Similarity get(String field) {
return new FullSimilarity();
}
});
TopDocs hits = searcher.search(query, null, 100);
assertTrue("hits is null and it shouldn't be", hits != null);
assertTrue("hits Size: " + hits.totalHits + " is not: " + 100, hits.totalHits == 100);
@ -283,46 +291,49 @@ public class TestPayloadTermQuery extends LuceneTestCase {
}
// must be static for weight serialization tests
static class BoostingSimilarity extends DefaultSimilarity {
static class BoostingSimilarityProvider implements SimilarityProvider {
// TODO: Remove warning after API has been finalized
@Override
public float scorePayload(int docId, int start, int end, byte[] payload, int offset, int length) {
//we know it is size 4 here, so ignore the offset/length
return payload[offset];
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Make everything else 1 so we see the effect of the payload
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@Override
public float computeNorm(FieldInvertState state) {
return state.getBoost();
}
@Override
public float queryNorm(float sumOfSquaredWeights) {
return 1;
}
@Override
public float sloppyFreq(int distance) {
return 1;
}
@Override
public float coord(int overlap, int maxOverlap) {
return 1;
}
@Override
public float idf(int docFreq, int numDocs) {
return 1;
}
public Similarity get(String field) {
return new DefaultSimilarity() {
@Override
public float tf(float freq) {
return freq == 0 ? 0 : 1;
// TODO: Remove warning after API has been finalized
@Override
public float scorePayload(int docId, int start, int end, byte[] payload, int offset, int length) {
//we know it is size 4 here, so ignore the offset/length
return payload[offset];
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Make everything else 1 so we see the effect of the payload
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@Override
public float computeNorm(FieldInvertState state) {
return state.getBoost();
}
@Override
public float sloppyFreq(int distance) {
return 1;
}
@Override
public float idf(int docFreq, int numDocs) {
return 1;
}
@Override
public float tf(float freq) {
return freq == 0 ? 0 : 1;
}
};
}
}

View File

@ -37,7 +37,7 @@ import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Payload;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.DefaultSimilarityProvider;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.search.TermQuery;
@ -50,7 +50,7 @@ import org.apache.lucene.util.LuceneTestCase;
public class TestPayloadSpans extends LuceneTestCase {
private IndexSearcher searcher;
private SimilarityProvider similarity = new DefaultSimilarity();
private SimilarityProvider similarity = new DefaultSimilarityProvider();
protected IndexReader indexReader;
private IndexReader closeIndexReader;
private Directory directory;

View File

@ -17,11 +17,13 @@ package org.apache.lucene.search.spans;
* limitations under the License.
*/
import org.apache.lucene.search.DefaultSimilarityProvider;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.CheckHits;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.SimilarityProvider;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.IndexSearcher;
@ -410,10 +412,14 @@ public class TestSpans extends LuceneTestCase {
for (int i = 0; i < leaves.length; i++) {
final SimilarityProvider sim = new DefaultSimilarity() {
@Override
public float sloppyFreq(int distance) {
return 0.0f;
final SimilarityProvider sim = new DefaultSimilarityProvider() {
public Similarity get(String field) {
return new DefaultSimilarity() {
@Override
public float sloppyFreq(int distance) {
return 0.0f;
}
};
}
};

View File

@ -16,9 +16,9 @@
*/
package org.apache.solr.schema;
import org.apache.lucene.search.DefaultSimilarity;
import org.apache.lucene.search.DefaultSimilarityProvider;
public class MockConfigurableSimilarity extends DefaultSimilarity {
public class MockConfigurableSimilarity extends DefaultSimilarityProvider {
private String passthrough;
public MockConfigurableSimilarity(String passthrough) {