SOLR-1915: DebugComponent now uses a NamedList to model Explanation objects in it's responses instead of Explanation.toString

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@949318 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2010-05-28 23:26:04 +00:00
parent dc26c26956
commit 9276737924
3 changed files with 110 additions and 32 deletions

View File

@ -66,6 +66,14 @@ Upgrading from Solr 1.4
(since it has never worked properly). Solr will now warn you if
you attempt to set this configuration option at all. (see SOLR-1846)
* The format of score explanations in the debug section has been
changed to represent the structure of the Explanation object in the
approprate format based on the ResponseWriter. In the unlikely
event that client code was previously depending on the previous
"white space indented plaintext string" format, a
"debug.explain.stringFormat=true" param can be added to the request
to force the old behavior -- but support for this will be removed in
the next release.
Detailed Change List
----------------------
@ -388,6 +396,9 @@ Other Changes
option. It has never worked very well, and in recent versions of
Solr hasn't worked at all. (hossman)
* SOLR-1915: DebugComponent now uses a NamedList to model Explanation
objects in it's responses instead of Explanation.toString (hossman)
Build
----------------------

View File

@ -61,6 +61,13 @@ public interface CommonParams {
/** whether to include debug data */
public static final String DEBUG_QUERY = "debugQuery";
/**
* whether score explanations should be in legacy plain text format
* @deprecated The plain text version will be removed in a future version
*/
@Deprecated
public static final String EXPLAIN_AS_STRING = "debug.explain.stringFormat";
/** another query to explain against */
public static final String EXPLAIN_OTHER = "explainOther";

View File

@ -29,6 +29,7 @@ import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.AppendedSolrParams;
import org.apache.solr.common.params.DefaultSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.common.util.StrUtils;
@ -109,7 +110,7 @@ public class SolrPluginUtils {
* @deprecated Use org.apache.solr.common.params.CommonParams.FL.
*/
@Deprecated
public static String FL = org.apache.solr.common.params.CommonParams.FL;
public static String FL = CommonParams.FL;
/**
* SolrIndexSearch.numDocs(Query,Query) freaks out if the filtering
@ -187,7 +188,7 @@ public class SolrPluginUtils {
public static int setReturnFields(SolrQueryRequest req,
SolrQueryResponse res) {
return setReturnFields(req.getParams().get(org.apache.solr.common.params.CommonParams.FL), res);
return setReturnFields(req.getParams().get(CommonParams.FL), res);
}
/**
@ -309,10 +310,10 @@ public class SolrPluginUtils {
String userQuery,
Query query,
DocList results,
CommonParams params)
org.apache.solr.util.CommonParams params)
throws IOException {
String debug = getParam(req, org.apache.solr.common.params.CommonParams.DEBUG_QUERY, params.debugQuery);
String debug = getParam(req, CommonParams.DEBUG_QUERY, params.debugQuery);
NamedList dbg = null;
if (debug!=null) {
@ -365,7 +366,7 @@ public class SolrPluginUtils {
* own toString method (in case it has internal state Solr
* doesn't know about)
* </li>
* <li>expain - the list of score explanations for each document in
* <li>explain - the list of score explanations for each document in
* results against query.
* </li>
* <li>otherQuery - the query string specified in 'explainOther' query param.
@ -389,40 +390,104 @@ public class SolrPluginUtils {
DocList results)
throws IOException {
String debug = req.getParams().get(org.apache.solr.common.params.CommonParams.DEBUG_QUERY);
String debug = req.getParams().get(CommonParams.DEBUG_QUERY);
NamedList dbg = null;
if (debug!=null) {
dbg = new SimpleOrderedMap();
SolrIndexSearcher searcher = req.getSearcher();
IndexSchema schema = req.getSchema();
boolean legacyExplainStyle
= req.getParams().getBool(CommonParams.EXPLAIN_AS_STRING,false);
/* userQuery may have been pre-processes .. expose that */
dbg.add("rawquerystring", req.getParams().get(org.apache.solr.common.params.CommonParams.Q));
dbg.add("rawquerystring", req.getParams().get(CommonParams.Q));
dbg.add("querystring", userQuery);
/* QueryParsing.toString isn't perfect, use it to see converted
* values, use regular toString to see any attributes of the
* underlying Query it may have missed.
*/
dbg.add("parsedquery",QueryParsing.toString(query, req.getSchema()));
dbg.add("parsedquery",QueryParsing.toString(query, schema));
dbg.add("parsedquery_toString", query.toString());
dbg.add("explain", getExplainList
(query, results, req.getSearcher(), req.getSchema()));
String otherQueryS = req.getParams().get(org.apache.solr.common.params.CommonParams.EXPLAIN_OTHER);
dbg.add("explain", legacyExplainStyle ?
getExplainList(query, results, searcher, schema) :
explanationsToNamedLists(getExplanations(query, results, searcher, schema)));
String otherQueryS = req.getParams().get(CommonParams.EXPLAIN_OTHER);
if (otherQueryS != null && otherQueryS.length() > 0) {
DocList otherResults = doSimpleQuery
(otherQueryS,req.getSearcher(), req.getSchema(),0,10);
dbg.add("otherQuery",otherQueryS);
dbg.add("explainOther", getExplainList
(query, otherResults,
req.getSearcher(),
req.getSchema()));
dbg.add("explainOther", legacyExplainStyle ?
getExplainList(query, otherResults, searcher, schema) :
explanationsToNamedLists(getExplanations(query, otherResults, searcher, schema)));
}
}
return dbg;
}
public static NamedList<Object> explanationToNamedList(Explanation e) {
NamedList<Object> out = new SimpleOrderedMap<Object>();
out.add("match", e.isMatch());
out.add("value", e.getValue());
out.add("description", e.getDescription());
Explanation[] details = e.getDetails();
// short circut out
if (null == details || 0 == details.length) return out;
List<NamedList<Object>> kids
= new ArrayList<NamedList<Object>>(details.length);
for (Explanation d : details) {
kids.add(explanationToNamedList(d));
}
out.add("details", kids);
return out;
}
public static NamedList<NamedList<Object>> explanationsToNamedLists
(NamedList<Explanation> explanations) {
NamedList<NamedList<Object>> out
= new SimpleOrderedMap<NamedList<Object>>();
for (Map.Entry<String,Explanation> entry : explanations) {
out.add(entry.getKey(), explanationToNamedList(entry.getValue()));
}
return out;
}
/**
* Generates an NamedList of Explanations for each item in a list of docs.
*
* @param query The Query you want explanations in the context of
* @param docs The Documents you want explained relative that query
*/
public static NamedList<Explanation> getExplanations
(Query query,
DocList docs,
SolrIndexSearcher searcher,
IndexSchema schema) throws IOException {
NamedList<Explanation> explainList = new SimpleOrderedMap<Explanation>();
DocIterator iterator = docs.iterator();
for (int i=0; i<docs.size(); i++) {
int id = iterator.nextDoc();
Document doc = searcher.doc(id);
String strid = schema.printableUniqueKey(doc);
explainList.add(strid, searcher.explain(query, id) );
}
return explainList;
}
/**
@ -430,29 +495,24 @@ public class SolrPluginUtils {
*
* @param query The Query you want explanations in the context of
* @param docs The Documents you want explained relative that query
* @deprecated this returns the explanations as Strings, instead it
* is recommeded to use getExplanations and call toString()
* yourself, or use explanationsToNamedLists
*/
@Deprecated
public static NamedList getExplainList(Query query, DocList docs,
SolrIndexSearcher searcher,
IndexSchema schema)
throws IOException {
NamedList explainList = new SimpleOrderedMap();
DocIterator iterator = docs.iterator();
for (int i=0; i<docs.size(); i++) {
int id = iterator.nextDoc();
NamedList<String> outList = new SimpleOrderedMap<String>();
NamedList<Explanation> explainList =
getExplanations(query,docs,searcher,schema);
Explanation explain = searcher.explain(query, id);
Document doc = searcher.doc(id);
String strid = schema.printableUniqueKey(doc);
// String docname = "";
// if (strid != null) docname="id="+strid+",";
// docname = docname + "internal_docid="+id;
explainList.add(strid, "\n" +explain.toString());
for (Map.Entry<String,Explanation> entry : explainList) {
outList.add(entry.getKey(), "\n"+entry.getValue().toString());
}
return explainList;
return outList;
}
/**
@ -820,7 +880,7 @@ public class SolrPluginUtils {
*/
public static Sort getSort(SolrQueryRequest req) {
String sort = req.getParams().get(org.apache.solr.common.params.CommonParams.SORT);
String sort = req.getParams().get(CommonParams.SORT);
if (null == sort || sort.equals("")) {
return null;
}
@ -850,7 +910,7 @@ public class SolrPluginUtils {
* @return null if no filter queries
*/
public static List<Query> parseFilterQueries(SolrQueryRequest req) throws ParseException {
return parseQueryStrings(req, req.getParams().getParams(org.apache.solr.common.params.CommonParams.FQ));
return parseQueryStrings(req, req.getParams().getParams(CommonParams.FQ));
}
/** Turns an array of query strings into a List of Query objects.