如何使用 Solrj 获取 SolrDocument 的 Lucene 解释?

发布于 2024-09-25 12:31:13 字数 1067 浏览 5 评论 0原文

我正在使用 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)这样的值的映射,但似乎explainmap只包含key null和最后一个值找到文档。

有什么想法如何获得正确的解释吗?

I'm searching an Solr index with SolrJ and trying to get the Lucene explanation for logging it for further use.

The code goes like this:

    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();
    }

I figured that response.getEplainMap() would contain a map with the value like response.getEplainMap().get(id) , but it seems that the explainmap contains only the key null with the value of the last found document.

Any ideas how to get the correct explanation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时浪漫 2024-10-02 12:31:13

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

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

创建索引并遇到上述问题时,请确保 schema.xml 中确定的 id 字段(例如 id)包含正确的数据。就我而言,我在代码中使用的 id 字段与 Solr 认为的不同,并且它不包含任何数据,因此解释映射只有一个键为 null 的字段。

In my case there was a bug in the Solr index itself. Code below works now.

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.

少跟Wǒ拽 2024-10-02 12:31:13

您是否尝试过从管理控制台调试查询?这显示了完整的输出。

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>

Have you tried debugging a query from the admin console? This shows you the full output.

QueryResponse has a couple of methods getDebugMap() and getExplainMap() that might prove useful. I haven't tested it in code but on the admin console when I debug a query I get the following;

<?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>
要走就滚别墨迹 2024-10-02 12:31:13

您还可以通过在字段列表中传入特殊的 [explain] 字段(带方括号)来获取文档中的解释信息作为字段。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文