public static void explainQuery(IndexSearcher searcher, Query query) throws IOException { TopDocs topDocs = searcher.search(query, 10); for (ScoreDoc match : topDocs.scoreDocs) { Explanation explanation = searcher.explain(query, match.doc); System.out.println("---------------"); System.out.println(explanation.toString()); } }
@Override public void transform(SolrDocument doc, int docid) { if (context != null && context.query != null) { try { Explanation exp = context.searcher.explain(context.query, docid); if (style == Style.nl) { doc.setField(name, SolrPluginUtils.explanationToNamedList(exp)); } else if (style == Style.html) { doc.setField(name, exp.toHtml()); } else { doc.setField(name, exp.toString()); } } catch (IOException e) { e.printStackTrace(); } } }
@Override public List<T> collectObjects(SearchResponse rsp) { SearchHits docs = rsp.getHits(); List<T> list = new ArrayList<T>(docs.hits().length); for (SearchHit sd : docs) { if (sd.getExplanation() != null) { String res = ""; for (Explanation str : sd.getExplanation().getDetails()) { res += str.toString(); } logger.info(sd.getId() + " " + res); } T o = readDoc(sd.getId(), sd.getVersion(), sd.getSource()); if (o != null) list.add(o); } return list; }
/** not a direct test of NearSpans, but a demonstration of how/when this causes problems */ public void testSpanNearScorerExplain() throws Exception { SpanNearQuery q = makeQuery(); Explanation e = q.weight(searcher).explain(searcher.getIndexReader(), 1); assertTrue( "Scorer explanation value for doc#1 isn't positive: " + e.toString(), 0.0f < e.getValue()); }