mirror of https://github.com/apache/lucene.git
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:
parent
269b2c5938
commit
0934039ced
|
@ -26,8 +26,10 @@ import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
|
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
|
||||||
import org.apache.lucene.search.Collector;
|
import org.apache.lucene.search.Collector;
|
||||||
import org.apache.lucene.search.DefaultSimilarity;
|
import org.apache.lucene.search.DefaultSimilarity;
|
||||||
|
import org.apache.lucene.search.DefaultSimilarityProvider;
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
import org.apache.lucene.search.Scorer;
|
import org.apache.lucene.search.Scorer;
|
||||||
|
import org.apache.lucene.search.Similarity;
|
||||||
import org.apache.lucene.search.SimilarityProvider;
|
import org.apache.lucene.search.SimilarityProvider;
|
||||||
import org.apache.lucene.search.TermQuery;
|
import org.apache.lucene.search.TermQuery;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
|
@ -42,12 +44,17 @@ public class TestFieldNormModifier extends LuceneTestCase {
|
||||||
public Directory store;
|
public Directory store;
|
||||||
|
|
||||||
/** inverts the normal notion of lengthNorm */
|
/** inverts the normal notion of lengthNorm */
|
||||||
public static SimilarityProvider s = new DefaultSimilarity() {
|
public static SimilarityProvider s = new DefaultSimilarityProvider() {
|
||||||
|
@Override
|
||||||
|
public Similarity get(String field) {
|
||||||
|
return new DefaultSimilarity() {
|
||||||
@Override
|
@Override
|
||||||
public float computeNorm(FieldInvertState state) {
|
public float computeNorm(FieldInvertState state) {
|
||||||
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
|
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
package org.apache.lucene.misc;
|
package org.apache.lucene.misc;
|
||||||
|
|
||||||
import org.apache.lucene.search.DefaultSimilarity;
|
import org.apache.lucene.search.DefaultSimilarity;
|
||||||
|
import org.apache.lucene.search.DefaultSimilarityProvider;
|
||||||
import org.apache.lucene.search.Similarity;
|
import org.apache.lucene.search.Similarity;
|
||||||
import org.apache.lucene.search.SimilarityProvider;
|
import org.apache.lucene.search.SimilarityProvider;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
@ -84,7 +85,7 @@ public class SweetSpotSimilarityTest extends LuceneTestCase {
|
||||||
final SweetSpotSimilarity ssB = new SweetSpotSimilarity();
|
final SweetSpotSimilarity ssB = new SweetSpotSimilarity();
|
||||||
ssB.setLengthNormFactors(5,8,0.1f, false);
|
ssB.setLengthNormFactors(5,8,0.1f, false);
|
||||||
|
|
||||||
SimilarityProvider sp = new SweetSpotSimilarity() {
|
SimilarityProvider sp = new DefaultSimilarityProvider() {
|
||||||
public Similarity get(String field) {
|
public Similarity get(String field) {
|
||||||
if (field.equals("bar"))
|
if (field.equals("bar"))
|
||||||
return ssBar;
|
return ssBar;
|
||||||
|
|
|
@ -31,8 +31,10 @@ import org.apache.lucene.index.MultiNorms;
|
||||||
import org.apache.lucene.index.Term;
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.search.Collector;
|
import org.apache.lucene.search.Collector;
|
||||||
import org.apache.lucene.search.DefaultSimilarity;
|
import org.apache.lucene.search.DefaultSimilarity;
|
||||||
|
import org.apache.lucene.search.DefaultSimilarityProvider;
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
import org.apache.lucene.search.Scorer;
|
import org.apache.lucene.search.Scorer;
|
||||||
|
import org.apache.lucene.search.Similarity;
|
||||||
import org.apache.lucene.search.SimilarityProvider;
|
import org.apache.lucene.search.SimilarityProvider;
|
||||||
import org.apache.lucene.search.TermQuery;
|
import org.apache.lucene.search.TermQuery;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
|
@ -47,12 +49,17 @@ public class TestLengthNormModifier extends LuceneTestCase {
|
||||||
public Directory store;
|
public Directory store;
|
||||||
|
|
||||||
/** inverts the normal notion of lengthNorm */
|
/** inverts the normal notion of lengthNorm */
|
||||||
public static SimilarityProvider s = new DefaultSimilarity() {
|
public static SimilarityProvider s = new DefaultSimilarityProvider() {
|
||||||
|
@Override
|
||||||
|
public Similarity get(String field) {
|
||||||
|
return new DefaultSimilarity() {
|
||||||
@Override
|
@Override
|
||||||
public float computeNorm(FieldInvertState state) {
|
public float computeNorm(FieldInvertState state) {
|
||||||
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
|
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
|
@ -163,12 +170,18 @@ public class TestLengthNormModifier extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
// override the norms to be inverted
|
// override the norms to be inverted
|
||||||
SimilarityProvider s = new DefaultSimilarity() {
|
SimilarityProvider s = new DefaultSimilarityProvider() {
|
||||||
|
@Override
|
||||||
|
public Similarity get(String field) {
|
||||||
|
return new DefaultSimilarity() {
|
||||||
@Override
|
@Override
|
||||||
public float computeNorm(FieldInvertState state) {
|
public float computeNorm(FieldInvertState state) {
|
||||||
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
|
return state.getBoost() * (discountOverlaps ? state.getLength() - state.getNumOverlap() : state.getLength());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
FieldNormModifier fnm = new FieldNormModifier(store, s);
|
FieldNormModifier fnm = new FieldNormModifier(store, s);
|
||||||
fnm.reSetNorms("field");
|
fnm.reSetNorms("field");
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ import org.apache.lucene.index.FieldInvertState;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** Expert: Default scoring implementation. */
|
/** Expert: Default scoring implementation. */
|
||||||
public class DefaultSimilarity extends Similarity implements SimilarityProvider {
|
public class DefaultSimilarity extends Similarity {
|
||||||
|
|
||||||
/** Implemented as
|
/** Implemented as
|
||||||
* <code>state.getBoost()*lengthNorm(numTerms)</code>, where
|
* <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)));
|
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>. */
|
/** Implemented as <code>sqrt(freq)</code>. */
|
||||||
@Override
|
@Override
|
||||||
public float tf(float freq) {
|
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);
|
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
|
// Default true
|
||||||
protected boolean discountOverlaps = true;
|
protected boolean discountOverlaps = true;
|
||||||
|
|
||||||
|
@ -88,12 +78,4 @@ public class DefaultSimilarity extends Similarity implements SimilarityProvider
|
||||||
public boolean getDiscountOverlaps() {
|
public boolean getDiscountOverlaps() {
|
||||||
return discountOverlaps;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -72,7 +72,7 @@ public class IndexSearcher {
|
||||||
private final ExecutorService executor;
|
private final ExecutorService executor;
|
||||||
|
|
||||||
// the default SimilarityProvider
|
// the default SimilarityProvider
|
||||||
private static final SimilarityProvider defaultProvider = new DefaultSimilarity();
|
private static final SimilarityProvider defaultProvider = new DefaultSimilarityProvider();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expert: returns a default SimilarityProvider instance.
|
* Expert: returns a default SimilarityProvider instance.
|
||||||
|
|
|
@ -362,7 +362,7 @@ import org.apache.lucene.util.SmallFloat;
|
||||||
* than another document with fewer query terms.
|
* than another document with fewer query terms.
|
||||||
* This is a search time factor computed in
|
* This is a search time factor computed in
|
||||||
* {@link SimilarityProvider#coord(int, int) coord(q,d)}
|
* {@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> <br>
|
* <br> <br>
|
||||||
* </li>
|
* </li>
|
||||||
*
|
*
|
||||||
|
@ -373,17 +373,17 @@ import org.apache.lucene.util.SmallFloat;
|
||||||
* is a normalizing factor used to make scores between queries comparable.
|
* 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),
|
* 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.
|
* 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
|
* 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>:
|
* produces a <a href="http://en.wikipedia.org/wiki/Euclidean_norm#Euclidean_norm">Euclidean norm</a>:
|
||||||
* <br> <br>
|
* <br> <br>
|
||||||
* <table cellpadding="1" cellspacing="0" border="0" align="center">
|
* <table cellpadding="1" cellspacing="0" border="0" align="center">
|
||||||
* <tr>
|
* <tr>
|
||||||
* <td valign="middle" align="right" rowspan="1">
|
* <td valign="middle" align="right" rowspan="1">
|
||||||
* queryNorm(q) =
|
* queryNorm(q) =
|
||||||
* {@link org.apache.lucene.search.DefaultSimilarity#queryNorm(float) queryNorm(sumOfSquaredWeights)}
|
* {@link org.apache.lucene.search.DefaultSimilarityProvider#queryNorm(float) queryNorm(sumOfSquaredWeights)}
|
||||||
* =
|
* =
|
||||||
* </td>
|
* </td>
|
||||||
* <td valign="middle" align="center" rowspan="1">
|
* <td valign="middle" align="center" rowspan="1">
|
||||||
|
|
|
@ -31,6 +31,7 @@ import org.apache.lucene.document.Field.Store;
|
||||||
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
|
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
|
||||||
import org.apache.lucene.index.SegmentReader.Norm;
|
import org.apache.lucene.index.SegmentReader.Norm;
|
||||||
import org.apache.lucene.search.DefaultSimilarity;
|
import org.apache.lucene.search.DefaultSimilarity;
|
||||||
|
import org.apache.lucene.search.DefaultSimilarityProvider;
|
||||||
import org.apache.lucene.search.Similarity;
|
import org.apache.lucene.search.Similarity;
|
||||||
import org.apache.lucene.search.SimilarityProvider;
|
import org.apache.lucene.search.SimilarityProvider;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
|
@ -41,17 +42,22 @@ import org.apache.lucene.util.LuceneTestCase;
|
||||||
*/
|
*/
|
||||||
public class TestIndexReaderCloneNorms extends LuceneTestCase {
|
public class TestIndexReaderCloneNorms extends LuceneTestCase {
|
||||||
|
|
||||||
private class SimilarityOne extends DefaultSimilarity {
|
private class SimilarityProviderOne extends DefaultSimilarityProvider {
|
||||||
|
@Override
|
||||||
|
public Similarity get(String field) {
|
||||||
|
return new DefaultSimilarity() {
|
||||||
@Override
|
@Override
|
||||||
public float computeNorm(FieldInvertState state) {
|
public float computeNorm(FieldInvertState state) {
|
||||||
// diable length norm
|
// diable length norm
|
||||||
return state.getBoost();
|
return state.getBoost();
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final int NUM_FIELDS = 10;
|
private static final int NUM_FIELDS = 10;
|
||||||
|
|
||||||
private SimilarityProvider similarityOne;
|
private SimilarityProvider similarityProviderOne;
|
||||||
|
|
||||||
private Analyzer anlzr;
|
private Analyzer anlzr;
|
||||||
|
|
||||||
|
@ -68,7 +74,7 @@ public class TestIndexReaderCloneNorms extends LuceneTestCase {
|
||||||
@Override
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
similarityOne = new SimilarityOne();
|
similarityProviderOne = new SimilarityProviderOne();
|
||||||
anlzr = new MockAnalyzer();
|
anlzr = new MockAnalyzer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -249,7 +255,7 @@ public class TestIndexReaderCloneNorms extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
|
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
|
||||||
TEST_VERSION_CURRENT, anlzr).setOpenMode(OpenMode.CREATE)
|
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();
|
LogMergePolicy lmp = (LogMergePolicy) iw.getConfig().getMergePolicy();
|
||||||
lmp.setMergeFactor(3);
|
lmp.setMergeFactor(3);
|
||||||
|
@ -307,7 +313,7 @@ public class TestIndexReaderCloneNorms extends LuceneTestCase {
|
||||||
throws IOException {
|
throws IOException {
|
||||||
IndexWriterConfig conf = newIndexWriterConfig(
|
IndexWriterConfig conf = newIndexWriterConfig(
|
||||||
TEST_VERSION_CURRENT, anlzr).setOpenMode(OpenMode.APPEND)
|
TEST_VERSION_CURRENT, anlzr).setOpenMode(OpenMode.APPEND)
|
||||||
.setMaxBufferedDocs(5).setSimilarityProvider(similarityOne).setMergePolicy(newLogMergePolicy());
|
.setMaxBufferedDocs(5).setSimilarityProvider(similarityProviderOne).setMergePolicy(newLogMergePolicy());
|
||||||
LogMergePolicy lmp = (LogMergePolicy) conf.getMergePolicy();
|
LogMergePolicy lmp = (LogMergePolicy) conf.getMergePolicy();
|
||||||
lmp.setMergeFactor(3);
|
lmp.setMergeFactor(3);
|
||||||
lmp.setUseCompoundFile(compound);
|
lmp.setUseCompoundFile(compound);
|
||||||
|
|
|
@ -26,14 +26,14 @@ import java.util.Set;
|
||||||
import org.apache.lucene.analysis.MockAnalyzer;
|
import org.apache.lucene.analysis.MockAnalyzer;
|
||||||
import org.apache.lucene.index.DocumentsWriter.IndexingChain;
|
import org.apache.lucene.index.DocumentsWriter.IndexingChain;
|
||||||
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
|
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.search.IndexSearcher;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class TestIndexWriterConfig extends LuceneTestCase {
|
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.
|
// Does not implement anything - used only for type checking on IndexWriterConfig.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,8 +177,8 @@ public class TestIndexWriterConfig extends LuceneTestCase {
|
||||||
// Test Similarity:
|
// Test Similarity:
|
||||||
// we shouldnt assert what the default is, just that its not null.
|
// we shouldnt assert what the default is, just that its not null.
|
||||||
assertTrue(IndexSearcher.getDefaultSimilarityProvider() == conf.getSimilarityProvider());
|
assertTrue(IndexSearcher.getDefaultSimilarityProvider() == conf.getSimilarityProvider());
|
||||||
conf.setSimilarityProvider(new MySimilarity());
|
conf.setSimilarityProvider(new MySimilarityProvider());
|
||||||
assertEquals(MySimilarity.class, conf.getSimilarityProvider().getClass());
|
assertEquals(MySimilarityProvider.class, conf.getSimilarityProvider().getClass());
|
||||||
conf.setSimilarityProvider(null);
|
conf.setSimilarityProvider(null);
|
||||||
assertTrue(IndexSearcher.getDefaultSimilarityProvider() == conf.getSimilarityProvider());
|
assertTrue(IndexSearcher.getDefaultSimilarityProvider() == conf.getSimilarityProvider());
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,8 @@ import org.apache.lucene.analysis.MockTokenizer;
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.document.Field;
|
import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.search.DefaultSimilarity;
|
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.store.Directory;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
import org.apache.lucene.util._TestUtil;
|
import org.apache.lucene.util._TestUtil;
|
||||||
|
@ -46,7 +48,12 @@ public class TestMaxTermFrequency extends LuceneTestCase {
|
||||||
dir = newDirectory();
|
dir = newDirectory();
|
||||||
IndexWriterConfig config = newIndexWriterConfig(TEST_VERSION_CURRENT,
|
IndexWriterConfig config = newIndexWriterConfig(TEST_VERSION_CURRENT,
|
||||||
new MockAnalyzer(MockTokenizer.SIMPLE, true)).setMergePolicy(newInOrderLogMergePolicy());
|
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);
|
RandomIndexWriter writer = new RandomIndexWriter(random, dir, config);
|
||||||
Document doc = new Document();
|
Document doc = new Document();
|
||||||
Field foo = newField("foo", "", Field.Store.NO, Field.Index.ANALYZED);
|
Field foo = newField("foo", "", Field.Store.NO, Field.Index.ANALYZED);
|
||||||
|
|
|
@ -29,6 +29,7 @@ import org.apache.lucene.document.Field.Index;
|
||||||
import org.apache.lucene.document.Field.Store;
|
import org.apache.lucene.document.Field.Store;
|
||||||
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
|
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
|
||||||
import org.apache.lucene.search.DefaultSimilarity;
|
import org.apache.lucene.search.DefaultSimilarity;
|
||||||
|
import org.apache.lucene.search.DefaultSimilarityProvider;
|
||||||
import org.apache.lucene.search.Similarity;
|
import org.apache.lucene.search.Similarity;
|
||||||
import org.apache.lucene.search.SimilarityProvider;
|
import org.apache.lucene.search.SimilarityProvider;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
|
@ -40,17 +41,22 @@ import org.apache.lucene.util.LuceneTestCase;
|
||||||
*/
|
*/
|
||||||
public class TestNorms extends LuceneTestCase {
|
public class TestNorms extends LuceneTestCase {
|
||||||
|
|
||||||
private class SimilarityOne extends DefaultSimilarity {
|
private class SimilarityProviderOne extends DefaultSimilarityProvider {
|
||||||
|
@Override
|
||||||
|
public Similarity get(String field) {
|
||||||
|
return new DefaultSimilarity() {
|
||||||
@Override
|
@Override
|
||||||
public float computeNorm(FieldInvertState state) {
|
public float computeNorm(FieldInvertState state) {
|
||||||
// Disable length norm
|
// diable length norm
|
||||||
return state.getBoost();
|
return state.getBoost();
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final int NUM_FIELDS = 10;
|
private static final int NUM_FIELDS = 10;
|
||||||
|
|
||||||
private SimilarityProvider similarityOne;
|
private SimilarityProvider similarityProviderOne;
|
||||||
private Analyzer anlzr;
|
private Analyzer anlzr;
|
||||||
private int numDocNorms;
|
private int numDocNorms;
|
||||||
private ArrayList<Float> norms;
|
private ArrayList<Float> norms;
|
||||||
|
@ -61,7 +67,7 @@ public class TestNorms extends LuceneTestCase {
|
||||||
@Override
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
similarityOne = new SimilarityOne();
|
similarityProviderOne = new SimilarityProviderOne();
|
||||||
anlzr = new MockAnalyzer();
|
anlzr = new MockAnalyzer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,7 +158,7 @@ public class TestNorms extends LuceneTestCase {
|
||||||
private void createIndex(Random random, Directory dir) throws IOException {
|
private void createIndex(Random random, Directory dir) throws IOException {
|
||||||
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
|
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
|
||||||
TEST_VERSION_CURRENT, anlzr).setOpenMode(OpenMode.CREATE)
|
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();
|
LogMergePolicy lmp = (LogMergePolicy) iw.getConfig().getMergePolicy();
|
||||||
lmp.setMergeFactor(3);
|
lmp.setMergeFactor(3);
|
||||||
lmp.setUseCompoundFile(true);
|
lmp.setUseCompoundFile(true);
|
||||||
|
@ -186,7 +192,7 @@ public class TestNorms extends LuceneTestCase {
|
||||||
assertEquals("number of norms mismatches",numDocNorms,b.length);
|
assertEquals("number of norms mismatches",numDocNorms,b.length);
|
||||||
ArrayList<Float> storedNorms = (i==1 ? modifiedNorms : norms);
|
ArrayList<Float> storedNorms = (i==1 ? modifiedNorms : norms);
|
||||||
for (int j = 0; j < b.length; j++) {
|
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();
|
float norm1 = storedNorms.get(j).floatValue();
|
||||||
assertEquals("stored norm value of "+field+" for doc "+j+" is "+norm+" - a mismatch!", norm, norm1, 0.000001);
|
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 {
|
private void addDocs(Random random, Directory dir, int ndocs, boolean compound) throws IOException {
|
||||||
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
|
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
|
||||||
TEST_VERSION_CURRENT, anlzr).setOpenMode(OpenMode.APPEND)
|
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();
|
LogMergePolicy lmp = (LogMergePolicy) iw.getConfig().getMergePolicy();
|
||||||
lmp.setMergeFactor(3);
|
lmp.setMergeFactor(3);
|
||||||
lmp.setUseCompoundFile(compound);
|
lmp.setUseCompoundFile(compound);
|
||||||
|
@ -222,7 +228,7 @@ public class TestNorms extends LuceneTestCase {
|
||||||
// return unique norm values that are unchanged by encoding/decoding
|
// return unique norm values that are unchanged by encoding/decoding
|
||||||
private float nextNorm(String fname) {
|
private float nextNorm(String fname) {
|
||||||
float norm = lastNorm + normDelta;
|
float norm = lastNorm + normDelta;
|
||||||
Similarity similarity = similarityOne.get(fname);
|
Similarity similarity = similarityProviderOne.get(fname);
|
||||||
do {
|
do {
|
||||||
float norm1 = similarity.decodeNormValue(similarity.encodeNormValue(norm));
|
float norm1 = similarity.decodeNormValue(similarity.encodeNormValue(norm));
|
||||||
if (norm1 > lastNorm) {
|
if (norm1 > lastNorm) {
|
||||||
|
@ -261,7 +267,12 @@ public class TestNorms extends LuceneTestCase {
|
||||||
public void testCustomEncoder() throws Exception {
|
public void testCustomEncoder() throws Exception {
|
||||||
Directory dir = newDirectory();
|
Directory dir = newDirectory();
|
||||||
IndexWriterConfig config = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer());
|
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);
|
RandomIndexWriter writer = new RandomIndexWriter(random, dir, config);
|
||||||
Document doc = new Document();
|
Document doc = new Document();
|
||||||
Field foo = newField("foo", "", Field.Store.NO, Field.Index.ANALYZED);
|
Field foo = newField("foo", "", Field.Store.NO, Field.Index.ANALYZED);
|
||||||
|
|
|
@ -35,7 +35,12 @@ import org.apache.lucene.search.Explanation.IDFExplanation;
|
||||||
|
|
||||||
public class TestOmitTf extends LuceneTestCase {
|
public class TestOmitTf extends LuceneTestCase {
|
||||||
|
|
||||||
public static class SimpleSimilarity extends Similarity implements SimilarityProvider {
|
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 new Similarity() {
|
||||||
|
|
||||||
@Override public float computeNorm(FieldInvertState state) { return state.getBoost(); }
|
@Override public float computeNorm(FieldInvertState state) { return state.getBoost(); }
|
||||||
@Override public float tf(float freq) { return freq; }
|
@Override public float tf(float freq) { return freq; }
|
||||||
@Override public float sloppyFreq(int distance) { return 2.0f; }
|
@Override public float sloppyFreq(int distance) { return 2.0f; }
|
||||||
|
@ -52,10 +57,7 @@ public class TestOmitTf extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -254,7 +256,7 @@ public class TestOmitTf extends LuceneTestCase {
|
||||||
dir,
|
dir,
|
||||||
newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).
|
newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).
|
||||||
setMaxBufferedDocs(2).
|
setMaxBufferedDocs(2).
|
||||||
setSimilarityProvider(new SimpleSimilarity()).
|
setSimilarityProvider(new SimpleSimilarityProvider()).
|
||||||
setMergePolicy(newInOrderLogMergePolicy(2))
|
setMergePolicy(newInOrderLogMergePolicy(2))
|
||||||
);
|
);
|
||||||
writer.setInfoStream(VERBOSE ? System.out : null);
|
writer.setInfoStream(VERBOSE ? System.out : null);
|
||||||
|
@ -285,7 +287,7 @@ public class TestOmitTf extends LuceneTestCase {
|
||||||
* Verify the index
|
* Verify the index
|
||||||
*/
|
*/
|
||||||
IndexSearcher searcher = new IndexSearcher(dir, true);
|
IndexSearcher searcher = new IndexSearcher(dir, true);
|
||||||
searcher.setSimilarityProvider(new SimpleSimilarity());
|
searcher.setSimilarityProvider(new SimpleSimilarityProvider());
|
||||||
|
|
||||||
Term a = new Term("noTf", term);
|
Term a = new Term("noTf", term);
|
||||||
Term b = new Term("tf", term);
|
Term b = new Term("tf", term);
|
||||||
|
|
|
@ -210,7 +210,7 @@ public class TestBoolean2 extends LuceneTestCase {
|
||||||
int[] expDocNrs = {2, 3};
|
int[] expDocNrs = {2, 3};
|
||||||
SimilarityProvider oldSimilarity = searcher.getSimilarityProvider();
|
SimilarityProvider oldSimilarity = searcher.getSimilarityProvider();
|
||||||
try {
|
try {
|
||||||
searcher.setSimilarityProvider(new DefaultSimilarity(){
|
searcher.setSimilarityProvider(new DefaultSimilarityProvider(){
|
||||||
@Override
|
@Override
|
||||||
public float coord(int overlap, int maxOverlap) {
|
public float coord(int overlap, int maxOverlap) {
|
||||||
return overlap / ((float)maxOverlap - 1);
|
return overlap / ((float)maxOverlap - 1);
|
||||||
|
|
|
@ -44,8 +44,8 @@ public class TestComplexExplanations extends TestExplanations {
|
||||||
}
|
}
|
||||||
|
|
||||||
// must be static for weight serialization tests
|
// must be static for weight serialization tests
|
||||||
private static DefaultSimilarity createQnorm1Similarity() {
|
private static DefaultSimilarityProvider createQnorm1Similarity() {
|
||||||
return new DefaultSimilarity() {
|
return new DefaultSimilarityProvider() {
|
||||||
@Override
|
@Override
|
||||||
public float queryNorm(float sumOfSquaredWeights) {
|
public float queryNorm(float sumOfSquaredWeights) {
|
||||||
return 1.0f; // / (float) Math.sqrt(1.0f + sumOfSquaredWeights);
|
return 1.0f; // / (float) Math.sqrt(1.0f + sumOfSquaredWeights);
|
||||||
|
|
|
@ -97,7 +97,7 @@ public class TestConstantScoreQuery extends LuceneTestCase {
|
||||||
searcher = newSearcher(reader);
|
searcher = newSearcher(reader);
|
||||||
|
|
||||||
// set a similarity that does not normalize our boost away
|
// set a similarity that does not normalize our boost away
|
||||||
searcher.setSimilarityProvider(new DefaultSimilarity() {
|
searcher.setSimilarityProvider(new DefaultSimilarityProvider() {
|
||||||
@Override
|
@Override
|
||||||
public float queryNorm(float sumOfSquaredWeights) {
|
public float queryNorm(float sumOfSquaredWeights) {
|
||||||
return 1.0f;
|
return 1.0f;
|
||||||
|
|
|
@ -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 Directory index;
|
||||||
public IndexReader r;
|
public IndexReader r;
|
||||||
public IndexSearcher s;
|
public IndexSearcher s;
|
||||||
|
|
|
@ -296,7 +296,10 @@ public class TestMultiPhraseQuery extends LuceneTestCase {
|
||||||
|
|
||||||
IndexReader reader = writer.getReader();
|
IndexReader reader = writer.getReader();
|
||||||
IndexSearcher searcher = newSearcher(reader);
|
IndexSearcher searcher = newSearcher(reader);
|
||||||
searcher.setSimilarityProvider(new DefaultSimilarity() {
|
searcher.setSimilarityProvider(new DefaultSimilarityProvider() {
|
||||||
|
@Override
|
||||||
|
public Similarity get(String field) {
|
||||||
|
return new DefaultSimilarity() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IDFExplanation idfExplain(Collection<Term> terms,
|
public IDFExplanation idfExplain(Collection<Term> terms,
|
||||||
|
@ -315,6 +318,8 @@ public class TestMultiPhraseQuery extends LuceneTestCase {
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
MultiPhraseQuery query = new MultiPhraseQuery();
|
MultiPhraseQuery query = new MultiPhraseQuery();
|
||||||
|
|
|
@ -39,7 +39,11 @@ import org.apache.lucene.search.Explanation.IDFExplanation;
|
||||||
*/
|
*/
|
||||||
public class TestSimilarity extends LuceneTestCase {
|
public class TestSimilarity extends LuceneTestCase {
|
||||||
|
|
||||||
public static class SimpleSimilarity extends Similarity implements SimilarityProvider {
|
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 new Similarity() {
|
||||||
@Override public float computeNorm(FieldInvertState state) { return state.getBoost(); }
|
@Override public float computeNorm(FieldInvertState state) { return state.getBoost(); }
|
||||||
@Override public float tf(float freq) { return freq; }
|
@Override public float tf(float freq) { return freq; }
|
||||||
@Override public float sloppyFreq(int distance) { return 2.0f; }
|
@Override public float sloppyFreq(int distance) { return 2.0f; }
|
||||||
|
@ -56,10 +60,7 @@ public class TestSimilarity extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +68,7 @@ public class TestSimilarity extends LuceneTestCase {
|
||||||
Directory store = newDirectory();
|
Directory store = newDirectory();
|
||||||
RandomIndexWriter writer = new RandomIndexWriter(random, store,
|
RandomIndexWriter writer = new RandomIndexWriter(random, store,
|
||||||
newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer())
|
newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer())
|
||||||
.setSimilarityProvider(new SimpleSimilarity()));
|
.setSimilarityProvider(new SimpleSimilarityProvider()));
|
||||||
|
|
||||||
Document d1 = new Document();
|
Document d1 = new Document();
|
||||||
d1.add(newField("field", "a c", Field.Store.YES, Field.Index.ANALYZED));
|
d1.add(newField("field", "a c", Field.Store.YES, Field.Index.ANALYZED));
|
||||||
|
@ -81,7 +82,7 @@ public class TestSimilarity extends LuceneTestCase {
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|
||||||
IndexSearcher searcher = newSearcher(reader);
|
IndexSearcher searcher = newSearcher(reader);
|
||||||
searcher.setSimilarityProvider(new SimpleSimilarity());
|
searcher.setSimilarityProvider(new SimpleSimilarityProvider());
|
||||||
|
|
||||||
Term a = new Term("field", "a");
|
Term a = new Term("field", "a");
|
||||||
Term b = new Term("field", "b");
|
Term b = new Term("field", "b");
|
||||||
|
|
|
@ -36,6 +36,8 @@ import org.apache.lucene.search.Explanation;
|
||||||
import org.apache.lucene.search.QueryUtils;
|
import org.apache.lucene.search.QueryUtils;
|
||||||
import org.apache.lucene.search.ScoreDoc;
|
import org.apache.lucene.search.ScoreDoc;
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
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.TopDocs;
|
||||||
import org.apache.lucene.search.spans.SpanQuery;
|
import org.apache.lucene.search.spans.SpanQuery;
|
||||||
import org.apache.lucene.search.spans.SpanNearQuery;
|
import org.apache.lucene.search.spans.SpanNearQuery;
|
||||||
|
@ -50,7 +52,7 @@ public class TestPayloadNearQuery extends LuceneTestCase {
|
||||||
private IndexSearcher searcher;
|
private IndexSearcher searcher;
|
||||||
private IndexReader reader;
|
private IndexReader reader;
|
||||||
private Directory directory;
|
private Directory directory;
|
||||||
private BoostingSimilarity similarity = new BoostingSimilarity();
|
private BoostingSimilarityProvider similarityProvider = new BoostingSimilarityProvider();
|
||||||
private byte[] payload2 = new byte[]{2};
|
private byte[] payload2 = new byte[]{2};
|
||||||
private byte[] payload4 = new byte[]{4};
|
private byte[] payload4 = new byte[]{4};
|
||||||
|
|
||||||
|
@ -105,7 +107,7 @@ public class TestPayloadNearQuery extends LuceneTestCase {
|
||||||
directory = newDirectory();
|
directory = newDirectory();
|
||||||
RandomIndexWriter writer = new RandomIndexWriter(random, directory,
|
RandomIndexWriter writer = new RandomIndexWriter(random, directory,
|
||||||
newIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer())
|
newIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer())
|
||||||
.setSimilarityProvider(similarity));
|
.setSimilarityProvider(similarityProvider));
|
||||||
//writer.infoStream = System.out;
|
//writer.infoStream = System.out;
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
Document doc = new Document();
|
Document doc = new Document();
|
||||||
|
@ -118,7 +120,7 @@ public class TestPayloadNearQuery extends LuceneTestCase {
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|
||||||
searcher = newSearcher(reader);
|
searcher = newSearcher(reader);
|
||||||
searcher.setSimilarityProvider(similarity);
|
searcher.setSimilarityProvider(similarityProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -297,45 +299,59 @@ public class TestPayloadNearQuery extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
// must be static for weight serialization tests
|
// 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) {
|
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
|
//we know it is size 4 here, so ignore the offset/length
|
||||||
return payload[offset];
|
return payload[offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
//Make everything else 1 so we see the effect of the payload
|
//Make everything else 1 so we see the effect of the payload
|
||||||
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
@Override public float computeNorm(FieldInvertState state) {
|
@Override
|
||||||
|
public float computeNorm(FieldInvertState state) {
|
||||||
return state.getBoost();
|
return state.getBoost();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public float queryNorm(float sumOfSquaredWeights) {
|
@Override
|
||||||
|
public float sloppyFreq(int distance) {
|
||||||
return 1.0f;
|
return 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public float sloppyFreq(int distance) {
|
@Override
|
||||||
|
public float tf(float freq) {
|
||||||
return 1.0f;
|
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
|
// idf used for phrase queries
|
||||||
@Override public IDFExplanation idfExplain(Collection<Term> terms, IndexSearcher searcher) throws IOException {
|
@Override
|
||||||
|
public IDFExplanation idfExplain(Collection<Term> terms, IndexSearcher searcher) throws IOException {
|
||||||
return new IDFExplanation() {
|
return new IDFExplanation() {
|
||||||
@Override
|
@Override
|
||||||
public float getIdf() {
|
public float getIdf() {
|
||||||
return 1.0f;
|
return 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String explain() {
|
public String explain() {
|
||||||
return "Inexplicable";
|
return "Inexplicable";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,11 @@ package org.apache.lucene.search.payloads;
|
||||||
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
import org.apache.lucene.util.English;
|
import org.apache.lucene.util.English;
|
||||||
|
import org.apache.lucene.search.DefaultSimilarityProvider;
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
import org.apache.lucene.search.QueryUtils;
|
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.TopDocs;
|
||||||
import org.apache.lucene.search.ScoreDoc;
|
import org.apache.lucene.search.ScoreDoc;
|
||||||
import org.apache.lucene.search.CheckHits;
|
import org.apache.lucene.search.CheckHits;
|
||||||
|
@ -54,7 +57,7 @@ import java.io.IOException;
|
||||||
public class TestPayloadTermQuery extends LuceneTestCase {
|
public class TestPayloadTermQuery extends LuceneTestCase {
|
||||||
private IndexSearcher searcher;
|
private IndexSearcher searcher;
|
||||||
private IndexReader reader;
|
private IndexReader reader;
|
||||||
private BoostingSimilarity similarity = new BoostingSimilarity();
|
private SimilarityProvider similarityProvider = new BoostingSimilarityProvider();
|
||||||
private byte[] payloadField = new byte[]{1};
|
private byte[] payloadField = new byte[]{1};
|
||||||
private byte[] payloadMultiField1 = new byte[]{2};
|
private byte[] payloadMultiField1 = new byte[]{2};
|
||||||
private byte[] payloadMultiField2 = new byte[]{4};
|
private byte[] payloadMultiField2 = new byte[]{4};
|
||||||
|
@ -110,7 +113,7 @@ public class TestPayloadTermQuery extends LuceneTestCase {
|
||||||
directory = newDirectory();
|
directory = newDirectory();
|
||||||
RandomIndexWriter writer = new RandomIndexWriter(random, directory,
|
RandomIndexWriter writer = new RandomIndexWriter(random, directory,
|
||||||
newIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer())
|
newIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer())
|
||||||
.setSimilarityProvider(similarity).setMergePolicy(newInOrderLogMergePolicy()));
|
.setSimilarityProvider(similarityProvider).setMergePolicy(newInOrderLogMergePolicy()));
|
||||||
//writer.infoStream = System.out;
|
//writer.infoStream = System.out;
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
Document doc = new Document();
|
Document doc = new Document();
|
||||||
|
@ -125,7 +128,7 @@ public class TestPayloadTermQuery extends LuceneTestCase {
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|
||||||
searcher = newSearcher(reader);
|
searcher = newSearcher(reader);
|
||||||
searcher.setSimilarityProvider(similarity);
|
searcher.setSimilarityProvider(similarityProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -220,7 +223,12 @@ public class TestPayloadTermQuery extends LuceneTestCase {
|
||||||
new MaxPayloadFunction(), false);
|
new MaxPayloadFunction(), false);
|
||||||
|
|
||||||
IndexSearcher theSearcher = new IndexSearcher(directory, true);
|
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);
|
TopDocs hits = searcher.search(query, null, 100);
|
||||||
assertTrue("hits is null and it shouldn't be", hits != null);
|
assertTrue("hits is null and it shouldn't be", hits != null);
|
||||||
assertTrue("hits Size: " + hits.totalHits + " is not: " + 100, hits.totalHits == 100);
|
assertTrue("hits Size: " + hits.totalHits + " is not: " + 100, hits.totalHits == 100);
|
||||||
|
@ -283,7 +291,18 @@ public class TestPayloadTermQuery extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
// must be static for weight serialization tests
|
// must be static for weight serialization tests
|
||||||
static class BoostingSimilarity extends DefaultSimilarity {
|
static class BoostingSimilarityProvider implements SimilarityProvider {
|
||||||
|
|
||||||
|
public float queryNorm(float sumOfSquaredWeights) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float coord(int overlap, int maxOverlap) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Similarity get(String field) {
|
||||||
|
return new DefaultSimilarity() {
|
||||||
|
|
||||||
// TODO: Remove warning after API has been finalized
|
// TODO: Remove warning after API has been finalized
|
||||||
@Override
|
@Override
|
||||||
|
@ -300,21 +319,11 @@ public class TestPayloadTermQuery extends LuceneTestCase {
|
||||||
return state.getBoost();
|
return state.getBoost();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public float queryNorm(float sumOfSquaredWeights) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float sloppyFreq(int distance) {
|
public float sloppyFreq(int distance) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public float coord(int overlap, int maxOverlap) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float idf(int docFreq, int numDocs) {
|
public float idf(int docFreq, int numDocs) {
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -324,6 +333,8 @@ public class TestPayloadTermQuery extends LuceneTestCase {
|
||||||
public float tf(float freq) {
|
public float tf(float freq) {
|
||||||
return freq == 0 ? 0 : 1;
|
return freq == 0 ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class FullSimilarity extends DefaultSimilarity{
|
static class FullSimilarity extends DefaultSimilarity{
|
||||||
|
|
|
@ -37,7 +37,7 @@ import org.apache.lucene.index.RandomIndexWriter;
|
||||||
import org.apache.lucene.index.IndexReader;
|
import org.apache.lucene.index.IndexReader;
|
||||||
import org.apache.lucene.index.Payload;
|
import org.apache.lucene.index.Payload;
|
||||||
import org.apache.lucene.index.Term;
|
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.IndexSearcher;
|
||||||
import org.apache.lucene.search.SimilarityProvider;
|
import org.apache.lucene.search.SimilarityProvider;
|
||||||
import org.apache.lucene.search.TermQuery;
|
import org.apache.lucene.search.TermQuery;
|
||||||
|
@ -50,7 +50,7 @@ import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
|
||||||
public class TestPayloadSpans extends LuceneTestCase {
|
public class TestPayloadSpans extends LuceneTestCase {
|
||||||
private IndexSearcher searcher;
|
private IndexSearcher searcher;
|
||||||
private SimilarityProvider similarity = new DefaultSimilarity();
|
private SimilarityProvider similarity = new DefaultSimilarityProvider();
|
||||||
protected IndexReader indexReader;
|
protected IndexReader indexReader;
|
||||||
private IndexReader closeIndexReader;
|
private IndexReader closeIndexReader;
|
||||||
private Directory directory;
|
private Directory directory;
|
||||||
|
|
|
@ -17,11 +17,13 @@ package org.apache.lucene.search.spans;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.search.DefaultSimilarityProvider;
|
||||||
import org.apache.lucene.search.DocIdSetIterator;
|
import org.apache.lucene.search.DocIdSetIterator;
|
||||||
import org.apache.lucene.search.Query;
|
import org.apache.lucene.search.Query;
|
||||||
import org.apache.lucene.search.CheckHits;
|
import org.apache.lucene.search.CheckHits;
|
||||||
import org.apache.lucene.search.DefaultSimilarity;
|
import org.apache.lucene.search.DefaultSimilarity;
|
||||||
import org.apache.lucene.search.Scorer;
|
import org.apache.lucene.search.Scorer;
|
||||||
|
import org.apache.lucene.search.Similarity;
|
||||||
import org.apache.lucene.search.SimilarityProvider;
|
import org.apache.lucene.search.SimilarityProvider;
|
||||||
import org.apache.lucene.search.TermQuery;
|
import org.apache.lucene.search.TermQuery;
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
|
@ -410,12 +412,16 @@ public class TestSpans extends LuceneTestCase {
|
||||||
for (int i = 0; i < leaves.length; i++) {
|
for (int i = 0; i < leaves.length; i++) {
|
||||||
|
|
||||||
|
|
||||||
final SimilarityProvider sim = new DefaultSimilarity() {
|
final SimilarityProvider sim = new DefaultSimilarityProvider() {
|
||||||
|
public Similarity get(String field) {
|
||||||
|
return new DefaultSimilarity() {
|
||||||
@Override
|
@Override
|
||||||
public float sloppyFreq(int distance) {
|
public float sloppyFreq(int distance) {
|
||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
final SimilarityProvider oldSim = searcher.getSimilarityProvider();
|
final SimilarityProvider oldSim = searcher.getSimilarityProvider();
|
||||||
Scorer spanScorer;
|
Scorer spanScorer;
|
||||||
|
|
|
@ -16,9 +16,9 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.solr.schema;
|
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;
|
private String passthrough;
|
||||||
|
|
||||||
public MockConfigurableSimilarity(String passthrough) {
|
public MockConfigurableSimilarity(String passthrough) {
|
||||||
|
|
Loading…
Reference in New Issue