You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2010/05/29 01:26:04 UTC

svn commit: r949318 - in /lucene/dev/trunk/solr: CHANGES.txt src/common/org/apache/solr/common/params/CommonParams.java src/java/org/apache/solr/util/SolrPluginUtils.java

Author: hossman
Date: Fri May 28 23:26:04 2010
New Revision: 949318

URL: http://svn.apache.org/viewvc?rev=949318&view=rev
Log:
SOLR-1915: DebugComponent now uses a NamedList to model Explanation objects in it's responses instead of Explanation.toString

Modified:
    lucene/dev/trunk/solr/CHANGES.txt
    lucene/dev/trunk/solr/src/common/org/apache/solr/common/params/CommonParams.java
    lucene/dev/trunk/solr/src/java/org/apache/solr/util/SolrPluginUtils.java

Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=949318&r1=949317&r2=949318&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Fri May 28 23:26:04 2010
@@ -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
 ----------------------
 

Modified: lucene/dev/trunk/solr/src/common/org/apache/solr/common/params/CommonParams.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/common/org/apache/solr/common/params/CommonParams.java?rev=949318&r1=949317&r2=949318&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/common/org/apache/solr/common/params/CommonParams.java (original)
+++ lucene/dev/trunk/solr/src/common/org/apache/solr/common/params/CommonParams.java Fri May 28 23:26:04 2010
@@ -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";
   

Modified: lucene/dev/trunk/solr/src/java/org/apache/solr/util/SolrPluginUtils.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/src/java/org/apache/solr/util/SolrPluginUtils.java?rev=949318&r1=949317&r2=949318&view=diff
==============================================================================
--- lucene/dev/trunk/solr/src/java/org/apache/solr/util/SolrPluginUtils.java (original)
+++ lucene/dev/trunk/solr/src/java/org/apache/solr/util/SolrPluginUtils.java Fri May 28 23:26:04 2010
@@ -29,6 +29,7 @@ import org.apache.solr.common.SolrExcept
 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,72 +390,131 @@ 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 list of Explanations for each item in a list of docs.
+   * 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 getExplainList(Query query, DocList docs,
-                                         SolrIndexSearcher searcher,
-                                         IndexSchema schema)
-    throws IOException {
-        
-    NamedList explainList = new SimpleOrderedMap();
+  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();
 
-      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());
+      explainList.add(strid, searcher.explain(query, id) );
     }
     return explainList;
   }
 
+
+  /**
+   * Generates an list 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
+   * @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<String> outList = new SimpleOrderedMap<String>();
+    NamedList<Explanation> explainList = 
+      getExplanations(query,docs,searcher,schema);
+
+    for (Map.Entry<String,Explanation> entry : explainList) {
+      outList.add(entry.getKey(), "\n"+entry.getValue().toString());
+    }
+    return outList;
+  }
+
   /**
    * Executes a basic query in lucene syntax
    */
@@ -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.