Search in sources :

Example 1 with SimpleSearcher

use of io.anserini.search.SimpleSearcher in project Anserini by castorini.

the class IndexReaderUtilsTest method testComputeQueryDocumentScore.

@Test
public void testComputeQueryDocumentScore() throws Exception {
    SimpleSearcher searcher = new SimpleSearcher(tempDir1.toString());
    Directory dir = FSDirectory.open(tempDir1);
    IndexReader reader = DirectoryReader.open(dir);
    Similarity similarity = new BM25Similarity(0.9f, 0.4f);
    // A bunch of test queries...
    String[] queries = { "text city", "text", "city" };
    for (String query : queries) {
        SimpleSearcher.Result[] results = searcher.search(query);
        // Strategy is to loop over the results, compute query-document score individually, and compare.
        for (int i = 0; i < results.length; i++) {
            float score = IndexReaderUtils.computeQueryDocumentScoreWithSimilarity(reader, results[i].docid, query, similarity);
            assertEquals(score, results[i].score, 10e-5);
        }
        // This is hard coded - doc3 isn't retrieved by any of the queries.
        assertEquals(0.0f, IndexReaderUtils.computeQueryDocumentScoreWithSimilarity(reader, "doc3", query, similarity), 10e-6);
    }
    reader.close();
    dir.close();
}
Also used : SimpleSearcher(io.anserini.search.SimpleSearcher) Similarity(org.apache.lucene.search.similarities.Similarity) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) IndexReader(org.apache.lucene.index.IndexReader) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) Test(org.junit.Test)

Aggregations

SimpleSearcher (io.anserini.search.SimpleSearcher)1 IndexReader (org.apache.lucene.index.IndexReader)1 BM25Similarity (org.apache.lucene.search.similarities.BM25Similarity)1 Similarity (org.apache.lucene.search.similarities.Similarity)1 Directory (org.apache.lucene.store.Directory)1 FSDirectory (org.apache.lucene.store.FSDirectory)1 Test (org.junit.Test)1