use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.
the class TestMemoryIndex method testSimilarities.
@Test
public void testSimilarities() throws IOException {
MemoryIndex mi = new MemoryIndex();
mi.addField("f1", "a long text field that contains many many terms", analyzer);
IndexSearcher searcher = mi.createSearcher();
LeafReader reader = (LeafReader) searcher.getIndexReader();
NumericDocValues norms = reader.getNormValues("f1");
assertEquals(0, norms.nextDoc());
float n1 = norms.longValue();
// Norms are re-computed when we change the Similarity
mi.setSimilarity(new Similarity() {
@Override
public long computeNorm(FieldInvertState state) {
return 74;
}
@Override
public SimWeight computeWeight(float boost, CollectionStatistics collectionStats, TermStatistics... termStats) {
throw new UnsupportedOperationException();
}
@Override
public SimScorer simScorer(SimWeight weight, LeafReaderContext context) throws IOException {
throw new UnsupportedOperationException();
}
});
norms = reader.getNormValues("f1");
assertEquals(0, norms.nextDoc());
float n2 = norms.longValue();
assertTrue(n1 != n2);
TestUtil.checkReader(reader);
}
use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.
the class IndexSchema method readSimilarity.
static SimilarityFactory readSimilarity(SolrResourceLoader loader, Node node) {
if (node == null) {
return null;
} else {
SimilarityFactory similarityFactory;
final String classArg = ((Element) node).getAttribute(SimilarityFactory.CLASS_NAME);
final Object obj = loader.newInstance(classArg, Object.class, "search.similarities.");
if (obj instanceof SimilarityFactory) {
// configure a factory, get a similarity back
final NamedList<Object> namedList = DOMUtil.childNodesToNamedList(node);
namedList.add(SimilarityFactory.CLASS_NAME, classArg);
SolrParams params = SolrParams.toSolrParams(namedList);
similarityFactory = (SimilarityFactory) obj;
similarityFactory.init(params);
} else {
// just like always, assume it's a Similarity and get a ClassCastException - reasonable error handling
similarityFactory = new SimilarityFactory() {
@Override
public Similarity getSimilarity() {
return (Similarity) obj;
}
};
}
return similarityFactory;
}
}
use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.
the class TestSpans method testSpanScorerZeroSloppyFreq.
public void testSpanScorerZeroSloppyFreq() throws Exception {
IndexReaderContext topReaderContext = searcher.getTopReaderContext();
List<LeafReaderContext> leaves = topReaderContext.leaves();
int subIndex = ReaderUtil.subIndex(11, leaves);
for (int i = 0, c = leaves.size(); i < c; i++) {
final LeafReaderContext ctx = leaves.get(i);
final Similarity sim = new ClassicSimilarity() {
@Override
public float sloppyFreq(int distance) {
return 0.0f;
}
};
final Similarity oldSim = searcher.getSimilarity(true);
Scorer spanScorer;
try {
searcher.setSimilarity(sim);
SpanQuery snq = spanNearOrderedQuery(field, 1, "t1", "t2");
spanScorer = searcher.createNormalizedWeight(snq, true).scorer(ctx);
} finally {
searcher.setSimilarity(oldSim);
}
if (i == subIndex) {
assertTrue("first doc", spanScorer.iterator().nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
assertEquals("first doc number", spanScorer.docID() + ctx.docBase, 11);
float score = spanScorer.score();
assertTrue("first doc score should be zero, " + score, score == 0.0f);
} else {
assertTrue("no second doc", spanScorer == null || spanScorer.iterator().nextDoc() == DocIdSetIterator.NO_MORE_DOCS);
}
}
}
use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.
the class TestBlockJoin method testScoreMode.
public void testScoreMode() throws IOException {
Similarity sim = new SimilarityBase() {
@Override
public String toString() {
return "TestSim";
}
@Override
protected float score(BasicStats stats, float freq, float docLen) {
return freq;
}
};
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig().setSimilarity(sim));
w.addDocuments(Arrays.asList(Collections.singleton(newTextField("foo", "bar bar", Store.NO)), Collections.singleton(newTextField("foo", "bar", Store.NO)), Collections.emptyList(), Collections.singleton(newStringField("type", new BytesRef("parent"), Store.NO))));
DirectoryReader reader = w.getReader();
w.close();
IndexSearcher searcher = newSearcher(reader);
searcher.setSimilarity(sim);
BitSetProducer parents = new QueryBitSetProducer(new TermQuery(new Term("type", "parent")));
for (ScoreMode scoreMode : ScoreMode.values()) {
Query query = new ToParentBlockJoinQuery(new TermQuery(new Term("foo", "bar")), parents, scoreMode);
TopDocs topDocs = searcher.search(query, 10);
assertEquals(1, topDocs.totalHits);
assertEquals(3, topDocs.scoreDocs[0].doc);
float expectedScore;
switch(scoreMode) {
case Avg:
expectedScore = 1.5f;
break;
case Max:
expectedScore = 2f;
break;
case Min:
expectedScore = 1f;
break;
case None:
expectedScore = 0f;
break;
case Total:
expectedScore = 3f;
break;
default:
throw new AssertionError();
}
assertEquals(expectedScore, topDocs.scoreDocs[0].score, 0f);
}
reader.close();
dir.close();
}
use of org.apache.lucene.search.similarities.Similarity in project lucene-solr by apache.
the class SchemaSimilarityFactory method getSimilarity.
@Override
public Similarity getSimilarity() {
if (null == core) {
throw new IllegalStateException("SchemaSimilarityFactory can not be used until SolrCoreAware.inform has been called");
}
if (null == similarity) {
// Need to instantiate lazily, can't do this in inform(SolrCore) because of chicken/egg
// circular initialization hell with core.getLatestSchema() to lookup defaultSimFromFieldType
Similarity defaultSim = null;
if (null == defaultSimFromFieldType) {
// nothing configured, choose a sensible implicit default...
defaultSim = this.core.getSolrConfig().luceneMatchVersion.onOrAfter(Version.LUCENE_6_0_0) ? new BM25Similarity() : new ClassicSimilarity();
} else {
FieldType defSimFT = core.getLatestSchema().getFieldTypeByName(defaultSimFromFieldType);
if (null == defSimFT) {
throw new SolrException(ErrorCode.SERVER_ERROR, "SchemaSimilarityFactory configured with " + INIT_OPT + "='" + defaultSimFromFieldType + "' but that <fieldType> does not exist");
}
defaultSim = defSimFT.getSimilarity();
if (null == defaultSim) {
throw new SolrException(ErrorCode.SERVER_ERROR, "SchemaSimilarityFactory configured with " + INIT_OPT + "='" + defaultSimFromFieldType + "' but that <fieldType> does not define a <similarity>");
}
}
similarity = new SchemaSimilarity(defaultSim);
}
return similarity;
}
Aggregations