如何使用 Solrj 获取 SolrDocument 的 Lucene 解释?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我而言,Solr 索引本身存在错误。下面的代码现在可以工作了。
创建索引并遇到上述问题时,请确保 schema.xml 中确定的 id 字段(例如
id
)包含正确的数据。就我而言,我在代码中使用的 id 字段与 Solr 认为的不同,并且它不包含任何数据,因此解释映射只有一个键为 null 的字段。In my case there was a bug in the Solr index itself. Code below works now.
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.您是否尝试过从管理控制台调试查询?这显示了完整的输出。
QueryResponse
有几个可能有用的方法getDebugMap()
和getExplainMap()
。我还没有在代码中测试它,但在管理控制台上调试查询时,我得到以下信息;Have you tried debugging a query from the admin console? This shows you the full output.
QueryResponse
has a couple of methodsgetDebugMap()
andgetExplainMap()
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;您还可以通过在字段列表中传入特殊的 [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.