5

我正在使用 SolrJ 搜索 Solr 索引,并尝试获取 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().get(id) ,但似乎解释映射仅包含具有最后找到的文档值的键 null 。

任何想法如何获得正确的解释?

4

3 回答 3

6

就我而言,Solr 索引本身存在一个错误。下面的代码现在可以工作了。

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

When creating an index and having problems like above make sure that the id field determined in schema.xml (e.g. <uniqueKey>id</uniqueKey>) contains correct data. In my case the id field I used in the code was not the same as Solr thought it was and it contained no data, thus the explainmap had only one field with a key null.

于 2010-09-29T10:08:59.187 回答
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>
于 2010-09-29T09:58:08.357 回答
1

You can also get the explain information as a field in the document by passing in the special [explain] field (with square brackets) in the field list.

于 2014-03-24T14:55:22.353 回答