Search in sources :

Example 1 with BasicStats

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();
}
Also used : Similarity(org.apache.lucene.search.similarities.Similarity) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) SimilarityBase(org.apache.lucene.search.similarities.SimilarityBase) BasicStats(org.apache.lucene.search.similarities.BasicStats) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Aggregations

DirectoryReader (org.apache.lucene.index.DirectoryReader)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 Term (org.apache.lucene.index.Term)1 BasicStats (org.apache.lucene.search.similarities.BasicStats)1 Similarity (org.apache.lucene.search.similarities.Similarity)1 SimilarityBase (org.apache.lucene.search.similarities.SimilarityBase)1 Directory (org.apache.lucene.store.Directory)1 BytesRef (org.apache.lucene.util.BytesRef)1