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"));
}
Aggregations