public virtual void TestExplain()
{
Directory dir = NewDirectory();
RandomIndexWriter w = new RandomIndexWriter(
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
this,
#endif
Random, dir);
Document doc = new Document();
doc.Add(NewStringField("id", "0", Field.Store.YES));
doc.Add(NewTextField("field", "wizard the the the the the oz", Field.Store.NO));
w.AddDocument(doc);
doc = new Document();
doc.Add(NewStringField("id", "1", Field.Store.YES));
// 1 extra token, but wizard and oz are close;
doc.Add(NewTextField("field", "wizard oz the the the the the the", Field.Store.NO));
w.AddDocument(doc);
IndexReader r = w.GetReader();
w.Dispose();
// Do ordinary BooleanQuery:
BooleanQuery bq = new BooleanQuery();
bq.Add(new TermQuery(new Term("field", "wizard")), Occur.SHOULD);
bq.Add(new TermQuery(new Term("field", "oz")), Occur.SHOULD);
IndexSearcher searcher = GetSearcher(r);
TopDocs hits = searcher.Search(bq, 10);
Assert.AreEqual(2, hits.TotalHits);
Assert.AreEqual("0", searcher.Doc(hits.ScoreDocs[0].Doc).Get("id"));
Assert.AreEqual("1", searcher.Doc(hits.ScoreDocs[1].Doc).Get("id"));
// Now, resort using PhraseQuery:
PhraseQuery pq = new PhraseQuery();
pq.Add(new Term("field", "wizard"));
pq.Add(new Term("field", "oz"));
Rescorer rescorer = new QueryRescorerAnonymousInnerClassHelper2(this, pq);
TopDocs hits2 = rescorer.Rescore(searcher, hits, 10);
// Resorting changed the order:
Assert.AreEqual(2, hits2.TotalHits);
Assert.AreEqual("1", searcher.Doc(hits2.ScoreDocs[0].Doc).Get("id"));
Assert.AreEqual("0", searcher.Doc(hits2.ScoreDocs[1].Doc).Get("id"));
int docID = hits2.ScoreDocs[0].Doc;
Explanation explain = rescorer.Explain(searcher, searcher.Explain(bq, docID), docID);
string s = explain.ToString();
Assert.IsTrue(s.Contains("TestQueryRescorer+"));
Assert.IsTrue(s.Contains("combined first and second pass score"));
Assert.IsTrue(s.Contains("first pass score"));
Assert.IsTrue(s.Contains("= second pass score"));
Assert.AreEqual(hits2.ScoreDocs[0].Score, explain.Value, 0.0f);
docID = hits2.ScoreDocs[1].Doc;
explain = rescorer.Explain(searcher, searcher.Explain(bq, docID), docID);
s = explain.ToString();
Assert.IsTrue(s.Contains("TestQueryRescorer+"));
Assert.IsTrue(s.Contains("combined first and second pass score"));
Assert.IsTrue(s.Contains("first pass score"));
Assert.IsTrue(s.Contains("no second pass score"));
Assert.IsFalse(s.Contains("= second pass score"));
Assert.IsTrue(s.Contains("NON-MATCH"));
Assert.IsTrue(Math.Abs(hits2.ScoreDocs[1].Score - explain.Value) < 0.0000001f);
r.Dispose();
dir.Dispose();
}