Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 461
Timo Albert Avatar asked Sep 29 '10 08:09

Timo Albert


1 Answers

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.

like image 110
Timo Albert Avatar answered Oct 16 '22 11:10

Timo Albert