2010-09-29 18 views
5

我正在用SolrJ搜索Solr索引,並試圖獲取Lucene的解釋以便將其記錄下來以供進一步使用。如何使用Solrj獲取SolrDocument的Lucene解釋?

的代碼是這樣的:

SolrServer server = new CommonsHttpSolrServer("solr_url"); 
    SolrQuery solrquery = new SolrQuery(); 
    solrquery.set("fl", "score, id"); // id is a String field 
    solrquery.set("rows", "1000"); 
    solrquery.set("debugQuery", "on"); 
    solrquery.setQuery("query words here"); 

    try { 
     QueryResponse response = server.query(solrquery); 
     SolrDocumentList docs = response.getResults(); 
     Iterator<SolrDocument> dociterator = docs.iterator(); 

     while (dociterator.hasNext()) 
     { 
      SolrDocument doc = dociterator.next(); 
      String id = (String) doc.getFirstValue(idfield); 
      Float relevance = (Float) doc.getFirstValue("score"); 
      String explanation = ???; 
     } 
    } catch (SolrServerException e) { 
     e.printStackTrace(); 
    } 

我想通response.getEplainMap()將包含一個地圖就像response.getEplainMap()的價值得到(ID),但它似乎explainmap。僅包含具有最後找到的文檔的值的關鍵null。

任何想法如何得到正確的解釋?

回答

6

在我的情況下,Solr索引本身存在一個錯誤。下面的代碼現在工作。

Map<String, String> explainmap = response.getExplainMap(); 
String explanation = explainmap.get(id); 

當創建一個索引,並且具有類似上述的問題,確保(例如<uniqueKey>id</uniqueKey>)在schema.xml中確定的id字段包含正確的數據。在我的情況下,我在代碼中使用的id字段與Solr認爲的不同,它不包含任何數據,因此explainmap只有一個字段,其中有一個鍵爲null。

1

您是否嘗試過從管理控制檯調試查詢?這會顯示完整的輸出。

QueryResponse有幾個方法getDebugMap()getExplainMap()可能證明是有用的。我沒有在代碼中進行測試,但在調試查詢時在管理控制檯上進行了測試,得到以下結果;

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <lst name="responseHeader"> 
    <int name="status">0</int> 
    <int name="QTime">0</int> 
    <lst name="params"> 
     <str name="q">stuff</str> 
     <str name="start">0</str> 
     <str name="indent">on</str> 
     <str name="explainOther"/> 
     <str name="wt">standard</str> 
     <str name="hl.fl"/> 
     <str name="fq"/> 
     <str name="version">2.2</str> 
     <str name="qt">standard</str> 
     <str name="debugQuery">on</str> 
     <str name="fl">*,score</str> 
     <str name="rows">1</str> 
    </lst> 
    </lst> 
    <result name="response" numFound="79" start="0" maxScore="4.050907"> 
    <doc> 
     <float name="score">4.050907</float> 
     ..other bits of data 
    </doc> 
    </result> 
    <lst name="debug"> 
    <str name="rawquerystring">stuff</str> 
    <str name="querystring">stuff</str> 
    <str name="parsedquery">MYSEARCHFIELD:stuff</str> 
    <str name="parsedquery_toString">MYSEARCHFIELD:stuff</str> 
    <lst name="explain"> 
     <str name="6095">  <--- 6095 is the ID of the document 
     4.050907 = (MATCH) fieldWeight(MYSEARCHFIELD:stuff in 1292), product of: 
     1.4142135 = tf(termFreq(MYSEARCHFIELD:stuff)=2) 
     9.166156 = idf(docFreq=79, maxDocs=281583) 
     0.3125 = fieldNorm(field=MYSEARCHFIELD, doc=1292) 
     </str> 
    </lst> 

    ..timing stuff here 

    </lst> 
</response> 
+0

正如我自己的答案中提到的ID是錯的(閱讀:不存在)。但是,如果在我自己找出問題之前,我已經閱讀了您的答案,我會從調試查詢控制檯中看到它,因爲我相信解釋輸出在您的文章中沒有名稱/ id屬性。所以通過張貼您解決我的問題的方式:) – Timo 2010-09-29 10:17:15

1

您還可以通過在字段列表中傳入特殊的[explain]字段(帶方括號)來獲取文檔中字段的解釋信息。