use of org.apache.lucene.search.similarities.BasicStats 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();
}
Aggregations