Search in sources :

Example 1 with Rescorer

use of org.apache.lucene.search.Rescorer in project lucene-solr by apache.

the class TestExpressionRescorer method testBasic.

public void testBasic() throws Exception {
    // create a sort field and sort by it (reverse order)
    Query query = new TermQuery(new Term("body", "contents"));
    IndexReader r = searcher.getIndexReader();
    // Just first pass query
    TopDocs hits = searcher.search(query, 10);
    assertEquals(3, hits.totalHits);
    assertEquals("3", r.document(hits.scoreDocs[0].doc).get("id"));
    assertEquals("1", r.document(hits.scoreDocs[1].doc).get("id"));
    assertEquals("2", r.document(hits.scoreDocs[2].doc).get("id"));
    // Now, rescore:
    Expression e = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)");
    SimpleBindings bindings = new SimpleBindings();
    bindings.add(new SortField("popularity", SortField.Type.INT));
    bindings.add(new SortField("_score", SortField.Type.SCORE));
    Rescorer rescorer = e.getRescorer(bindings);
    hits = rescorer.rescore(searcher, hits, 10);
    assertEquals(3, hits.totalHits);
    assertEquals("2", r.document(hits.scoreDocs[0].doc).get("id"));
    assertEquals("1", r.document(hits.scoreDocs[1].doc).get("id"));
    assertEquals("3", r.document(hits.scoreDocs[2].doc).get("id"));
    String expl = rescorer.explain(searcher, searcher.explain(query, hits.scoreDocs[0].doc), hits.scoreDocs[0].doc).toString();
    // Confirm the explanation breaks out the individual
    // variables:
    assertTrue(expl.contains("= double(popularity)"));
    // Confirm the explanation includes first pass details:
    assertTrue(expl.contains("= first pass score"));
    assertTrue(expl.contains("body:contents in"));
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) IndexReader(org.apache.lucene.index.IndexReader) Rescorer(org.apache.lucene.search.Rescorer) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term)

Aggregations

IndexReader (org.apache.lucene.index.IndexReader)1 Term (org.apache.lucene.index.Term)1 Query (org.apache.lucene.search.Query)1 Rescorer (org.apache.lucene.search.Rescorer)1 SortField (org.apache.lucene.search.SortField)1 TermQuery (org.apache.lucene.search.TermQuery)1 TopDocs (org.apache.lucene.search.TopDocs)1