From 0934039ced7b888dda28abcd5b9a6397c1f160e8 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Fri, 25 Mar 2011 14:00:42 +0000 Subject: [PATCH] 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 --- .../lucene/index/TestFieldNormModifier.java | 13 ++- .../lucene/misc/SweetSpotSimilarityTest.java | 3 +- .../lucene/misc/TestLengthNormModifier.java | 37 +++++--- .../lucene/search/DefaultSimilarity.java | 20 +---- .../search/DefaultSimilarityProvider.java | 41 +++++++++ .../apache/lucene/search/IndexSearcher.java | 2 +- .../org/apache/lucene/search/Similarity.java | 8 +- .../index/TestIndexReaderCloneNorms.java | 24 ++++-- .../lucene/index/TestIndexWriterConfig.java | 8 +- .../lucene/index/TestMaxTermFrequency.java | 9 +- .../org/apache/lucene/index/TestNorms.java | 35 +++++--- .../org/apache/lucene/index/TestOmitTf.java | 42 +++++----- .../apache/lucene/search/TestBoolean2.java | 2 +- .../search/TestComplexExplanations.java | 4 +- .../lucene/search/TestConstantScoreQuery.java | 2 +- .../search/TestDisjunctionMaxQuery.java | 7 +- .../lucene/search/TestMultiPhraseQuery.java | 37 ++++---- .../apache/lucene/search/TestSimilarity.java | 41 ++++----- .../search/payloads/TestPayloadNearQuery.java | 84 +++++++++++-------- .../search/payloads/TestPayloadTermQuery.java | 81 ++++++++++-------- .../lucene/search/spans/TestPayloadSpans.java | 4 +- .../apache/lucene/search/spans/TestSpans.java | 14 +++- .../schema/MockConfigurableSimilarity.java | 4 +- 23 files changed, 318 insertions(+), 204 deletions(-) create mode 100644 lucene/src/java/org/apache/lucene/search/DefaultSimilarityProvider.java diff --git a/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java b/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java index 33e97be4405..72d186090fc 100644 --- a/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java +++ b/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java @@ -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()); + } + }; } }; diff --git a/lucene/contrib/misc/src/test/org/apache/lucene/misc/SweetSpotSimilarityTest.java b/lucene/contrib/misc/src/test/org/apache/lucene/misc/SweetSpotSimilarityTest.java index dbc76dd3b90..f7f33f3748f 100644 --- a/lucene/contrib/misc/src/test/org/apache/lucene/misc/SweetSpotSimilarityTest.java +++ b/lucene/contrib/misc/src/test/org/apache/lucene/misc/SweetSpotSimilarityTest.java @@ -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; diff --git a/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java b/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java index 6c91a09f288..5bd4ad530f5 100644 --- a/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java +++ b/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java @@ -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"); diff --git a/lucene/src/java/org/apache/lucene/search/DefaultSimilarity.java b/lucene/src/java/org/apache/lucene/search/DefaultSimilarity.java index 758b51ac396..4b89f8e502f 100644 --- a/lucene/src/java/org/apache/lucene/search/DefaultSimilarity.java +++ b/lucene/src/java/org/apache/lucene/search/DefaultSimilarity.java @@ -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 * state.getBoost()*lengthNorm(numTerms), where @@ -40,11 +40,6 @@ public class DefaultSimilarity extends Similarity implements SimilarityProvider return state.getBoost() * ((float) (1.0 / Math.sqrt(numTerms))); } - /** Implemented as 1/sqrt(sumOfSquaredWeights). */ - public float queryNorm(float sumOfSquaredWeights) { - return (float)(1.0 / Math.sqrt(sumOfSquaredWeights)); - } - /** Implemented as sqrt(freq). */ @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 overlap / maxOverlap. */ - 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; - } } diff --git a/lucene/src/java/org/apache/lucene/search/DefaultSimilarityProvider.java b/lucene/src/java/org/apache/lucene/search/DefaultSimilarityProvider.java new file mode 100644 index 00000000000..0b336f7eca1 --- /dev/null +++ b/lucene/src/java/org/apache/lucene/search/DefaultSimilarityProvider.java @@ -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. + *

+ * Returns {@link DefaultSimilarity} for every field + */ +public class DefaultSimilarityProvider implements SimilarityProvider { + private static final Similarity impl = new DefaultSimilarity(); + + /** Implemented as overlap / maxOverlap. */ + public float coord(int overlap, int maxOverlap) { + return overlap / (float)maxOverlap; + } + + /** Implemented as 1/sqrt(sumOfSquaredWeights). */ + public float queryNorm(float sumOfSquaredWeights) { + return (float)(1.0 / Math.sqrt(sumOfSquaredWeights)); + } + + public Similarity get(String field) { + return impl; + } +} diff --git a/lucene/src/java/org/apache/lucene/search/IndexSearcher.java b/lucene/src/java/org/apache/lucene/search/IndexSearcher.java index d63677df8cd..1e319b24cc7 100644 --- a/lucene/src/java/org/apache/lucene/search/IndexSearcher.java +++ b/lucene/src/java/org/apache/lucene/search/IndexSearcher.java @@ -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. diff --git a/lucene/src/java/org/apache/lucene/search/Similarity.java b/lucene/src/java/org/apache/lucene/search/Similarity.java index 36fba6e34a3..e8ae33f6ea1 100644 --- a/lucene/src/java/org/apache/lucene/search/Similarity.java +++ b/lucene/src/java/org/apache/lucene/search/Similarity.java @@ -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. *
 
* * @@ -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 Euclidean norm: *
 
* * * *
* queryNorm(q)   =   - * {@link org.apache.lucene.search.DefaultSimilarity#queryNorm(float) queryNorm(sumOfSquaredWeights)} + * {@link org.apache.lucene.search.DefaultSimilarityProvider#queryNorm(float) queryNorm(sumOfSquaredWeights)} *   =   * diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java b/lucene/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java index 2e4974f3941..9ff5847d3de 100644 --- a/lucene/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java +++ b/lucene/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java @@ -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); diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriterConfig.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriterConfig.java index c8c203bef01..587e8709698 100644 --- a/lucene/src/test/org/apache/lucene/index/TestIndexWriterConfig.java +++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriterConfig.java @@ -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()); diff --git a/lucene/src/test/org/apache/lucene/index/TestMaxTermFrequency.java b/lucene/src/test/org/apache/lucene/index/TestMaxTermFrequency.java index c6b9be60031..c56068c3ec3 100644 --- a/lucene/src/test/org/apache/lucene/index/TestMaxTermFrequency.java +++ b/lucene/src/test/org/apache/lucene/index/TestMaxTermFrequency.java @@ -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); diff --git a/lucene/src/test/org/apache/lucene/index/TestNorms.java b/lucene/src/test/org/apache/lucene/index/TestNorms.java index 997abd4fb7c..b9549be56cb 100755 --- a/lucene/src/test/org/apache/lucene/index/TestNorms.java +++ b/lucene/src/test/org/apache/lucene/index/TestNorms.java @@ -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 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 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); diff --git a/lucene/src/test/org/apache/lucene/index/TestOmitTf.java b/lucene/src/test/org/apache/lucene/index/TestOmitTf.java index d760018a6e3..0741dc66899 100644 --- a/lucene/src/test/org/apache/lucene/index/TestOmitTf.java +++ b/lucene/src/test/org/apache/lucene/index/TestOmitTf.java @@ -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 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 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); diff --git a/lucene/src/test/org/apache/lucene/search/TestBoolean2.java b/lucene/src/test/org/apache/lucene/search/TestBoolean2.java index 13184b0d77a..0602cab1f62 100644 --- a/lucene/src/test/org/apache/lucene/search/TestBoolean2.java +++ b/lucene/src/test/org/apache/lucene/search/TestBoolean2.java @@ -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); diff --git a/lucene/src/test/org/apache/lucene/search/TestComplexExplanations.java b/lucene/src/test/org/apache/lucene/search/TestComplexExplanations.java index 0d3e274a022..a8ed2e4e9b3 100644 --- a/lucene/src/test/org/apache/lucene/search/TestComplexExplanations.java +++ b/lucene/src/test/org/apache/lucene/search/TestComplexExplanations.java @@ -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); diff --git a/lucene/src/test/org/apache/lucene/search/TestConstantScoreQuery.java b/lucene/src/test/org/apache/lucene/search/TestConstantScoreQuery.java index 2c3dc9127b0..35b388e25fc 100644 --- a/lucene/src/test/org/apache/lucene/search/TestConstantScoreQuery.java +++ b/lucene/src/test/org/apache/lucene/search/TestConstantScoreQuery.java @@ -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; diff --git a/lucene/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java b/lucene/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java index 7541dafe94c..0f1cb4d94dd 100644 --- a/lucene/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java +++ b/lucene/src/test/org/apache/lucene/search/TestDisjunctionMaxQuery.java @@ -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; diff --git a/lucene/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java b/lucene/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java index ef860f9737a..6359ce6d27e 100644 --- a/lucene/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java +++ b/lucene/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java @@ -296,25 +296,30 @@ 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 terms, - IndexSearcher searcher) throws IOException { - return new IDFExplanation() { - - @Override - public float getIdf() { - return 10f; - } - - @Override - public String explain() { - return "just a test"; - } + public Similarity get(String field) { + return new DefaultSimilarity() { + @Override + public IDFExplanation idfExplain(Collection terms, + IndexSearcher searcher) throws IOException { + return new IDFExplanation() { + + @Override + public float getIdf() { + return 10f; + } + + @Override + public String explain() { + return "just a test"; + } + + }; + } }; - } + } }); MultiPhraseQuery query = new MultiPhraseQuery(); diff --git a/lucene/src/test/org/apache/lucene/search/TestSimilarity.java b/lucene/src/test/org/apache/lucene/search/TestSimilarity.java index 1e4bc503840..b8d9acaac5b 100644 --- a/lucene/src/test/org/apache/lucene/search/TestSimilarity.java +++ b/lucene/src/test/org/apache/lucene/search/TestSimilarity.java @@ -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 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 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"); diff --git a/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadNearQuery.java b/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadNearQuery.java index 522642bccac..f29d990c52a 100644 --- a/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadNearQuery.java +++ b/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadNearQuery.java @@ -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; } + + public float coord(int overlap, int maxOverlap) { + return 1.0f; + } + + public Similarity get(String field) { + return new DefaultSimilarity() { + + @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.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 terms, IndexSearcher searcher) throws IOException { - return new IDFExplanation() { - @Override - public float getIdf() { + @Override + public float sloppyFreq(int distance) { return 1.0f; } - @Override - public String explain() { - return "Inexplicable"; + + @Override + public float tf(float freq) { + return 1.0f; + } + + // idf used for phrase queries + @Override + public IDFExplanation idfExplain(Collection terms, IndexSearcher searcher) throws IOException { + return new IDFExplanation() { + @Override + public float getIdf() { + return 1.0f; + } + + @Override + public String explain() { + return "Inexplicable"; + } + }; } }; } diff --git a/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadTermQuery.java b/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadTermQuery.java index c82c07907ec..2b8ad636789 100644 --- a/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadTermQuery.java +++ b/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadTermQuery.java @@ -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() { + + // 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]; + } - @Override - public float tf(float freq) { - return freq == 0 ? 0 : 1; + //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + //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; + } + }; } } diff --git a/lucene/src/test/org/apache/lucene/search/spans/TestPayloadSpans.java b/lucene/src/test/org/apache/lucene/search/spans/TestPayloadSpans.java index 2ae7efd63a3..1e1d2ca5e33 100644 --- a/lucene/src/test/org/apache/lucene/search/spans/TestPayloadSpans.java +++ b/lucene/src/test/org/apache/lucene/search/spans/TestPayloadSpans.java @@ -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; diff --git a/lucene/src/test/org/apache/lucene/search/spans/TestSpans.java b/lucene/src/test/org/apache/lucene/search/spans/TestSpans.java index 23cdf4786d9..fc7e0db69d2 100644 --- a/lucene/src/test/org/apache/lucene/search/spans/TestSpans.java +++ b/lucene/src/test/org/apache/lucene/search/spans/TestSpans.java @@ -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; + } + }; } }; diff --git a/solr/src/test/org/apache/solr/schema/MockConfigurableSimilarity.java b/solr/src/test/org/apache/solr/schema/MockConfigurableSimilarity.java index 36021fab4e7..6b8f6c1d362 100644 --- a/solr/src/test/org/apache/solr/schema/MockConfigurableSimilarity.java +++ b/solr/src/test/org/apache/solr/schema/MockConfigurableSimilarity.java @@ -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) {