use of org.apache.lucene.search.similarities.ClassicSimilarity in project lucene-solr by apache.
the class TestValueSources method testNorm.
public void testNorm() throws Exception {
Similarity saved = searcher.getSimilarity(true);
try {
// no norm field (so agnostic to indexed similarity)
searcher.setSimilarity(new ClassicSimilarity());
ValueSource vs = new NormValueSource("byte");
assertHits(new FunctionQuery(vs), new float[] { 1f, 1f });
// regardless of whether norms exist, value source exists == 0
assertAllExist(vs);
vs = new NormValueSource("text");
assertAllExist(vs);
} finally {
searcher.setSimilarity(saved);
}
}
use of org.apache.lucene.search.similarities.ClassicSimilarity in project lucene-solr by apache.
the class TestCustomScoreExplanations method testSubExplanations.
public void testSubExplanations() throws IOException {
Query query = new FunctionQuery(new ConstValueSource(5));
IndexSearcher searcher = newSearcher(BaseExplanationTestCase.searcher.getIndexReader());
searcher.setSimilarity(new BM25Similarity());
Explanation expl = searcher.explain(query, 0);
assertEquals(2, expl.getDetails().length);
// function
assertEquals(5f, expl.getDetails()[0].getValue(), 0f);
// boost
assertEquals("boost", expl.getDetails()[1].getDescription());
assertEquals(1f, expl.getDetails()[1].getValue(), 0f);
query = new BoostQuery(query, 2);
expl = searcher.explain(query, 0);
assertEquals(2, expl.getDetails().length);
// function
assertEquals(5f, expl.getDetails()[0].getValue(), 0f);
// boost
assertEquals("boost", expl.getDetails()[1].getDescription());
assertEquals(2f, expl.getDetails()[1].getValue(), 0f);
// in order to have a queryNorm != 1
searcher.setSimilarity(new ClassicSimilarity());
expl = searcher.explain(query, 0);
assertEquals(2, expl.getDetails().length);
// function
assertEquals(5f, expl.getDetails()[0].getValue(), 0f);
// boost
assertEquals("boost", expl.getDetails()[1].getDescription());
assertEquals(2f, expl.getDetails()[1].getValue(), 0f);
}
use of org.apache.lucene.search.similarities.ClassicSimilarity in project lucene-solr by apache.
the class TestFunctionScoreExplanations method testSubExplanations.
public void testSubExplanations() throws IOException {
Query query = new FunctionScoreQuery(new MatchAllDocsQuery(), DoubleValuesSource.constant(5));
IndexSearcher searcher = newSearcher(BaseExplanationTestCase.searcher.getIndexReader());
searcher.setSimilarity(new BM25Similarity());
Explanation expl = searcher.explain(query, 0);
assertEquals("constant(5.0)", expl.getDescription());
assertEquals(0, expl.getDetails().length);
query = new BoostQuery(query, 2);
expl = searcher.explain(query, 0);
assertEquals(2, expl.getDetails().length);
// function
assertEquals(5f, expl.getDetails()[1].getValue(), 0f);
// boost
assertEquals("boost", expl.getDetails()[0].getDescription());
assertEquals(2f, expl.getDetails()[0].getValue(), 0f);
// in order to have a queryNorm != 1
searcher.setSimilarity(new ClassicSimilarity());
expl = searcher.explain(query, 0);
assertEquals(2, expl.getDetails().length);
// function
assertEquals(5f, expl.getDetails()[1].getValue(), 0f);
// boost
assertEquals("boost", expl.getDetails()[0].getDescription());
assertEquals(2f, expl.getDetails()[0].getValue(), 0f);
}
use of org.apache.lucene.search.similarities.ClassicSimilarity in project lucene-solr by apache.
the class TestTermScorer method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()).setSimilarity(new ClassicSimilarity()));
for (int i = 0; i < values.length; i++) {
Document doc = new Document();
doc.add(newTextField(FIELD, values[i], Field.Store.YES));
writer.addDocument(doc);
}
writer.forceMerge(1);
indexReader = getOnlyLeafReader(writer.getReader());
writer.close();
indexSearcher = newSearcher(indexReader, false);
indexSearcher.setSimilarity(new ClassicSimilarity());
}
use of org.apache.lucene.search.similarities.ClassicSimilarity in project lucene-solr by apache.
the class TestExpressionRescorer method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir, newIndexWriterConfig().setSimilarity(new ClassicSimilarity()));
Document doc = new Document();
doc.add(newStringField("id", "1", Field.Store.YES));
doc.add(newTextField("body", "some contents and more contents", Field.Store.NO));
doc.add(new NumericDocValuesField("popularity", 5));
iw.addDocument(doc);
doc = new Document();
doc.add(newStringField("id", "2", Field.Store.YES));
doc.add(newTextField("body", "another document with different contents", Field.Store.NO));
doc.add(new NumericDocValuesField("popularity", 20));
iw.addDocument(doc);
doc = new Document();
doc.add(newStringField("id", "3", Field.Store.YES));
doc.add(newTextField("body", "crappy contents", Field.Store.NO));
doc.add(new NumericDocValuesField("popularity", 2));
iw.addDocument(doc);
reader = iw.getReader();
searcher = new IndexSearcher(reader);
// TODO: fix this test to not be so flaky and use newSearcher
searcher.setSimilarity(new ClassicSimilarity());
iw.close();
}
Aggregations