Search code examples
javasolrlucenesolrj

How to get Lucene explanation for a SolrDocument with Solrj?


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?


Solution

  • 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.