While working with a view of a collection of documents returned from Solr using Solrj earlier today, I was attempting to write out the number of documents found in the search. In pure Java code you’d just request this by just calling .getNumFound() on the SolrDocumentList containing your documents, which whould also mean that they should be available through EL in JSTL by calling ${solrDocumentList.numFound} (which in turn calls getNumFound() in the SolrDocumentList object). The code in question was as simple as:
Which resulted in this error message, which kind of came as a surprise:
java.lang.NumberFormatException: For input string: "numFound" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.parseInt(Integer.java:497)
After digging around a bit and reading the error message yet again, it suddenly hit me: $solrDocumentList was being interpreted and casted to a List, and as such, EL expected an index into the List instead of my call to a function. I’ve not been working with JSTL for too long, so I thought a bit about how to solve this. One solution would be to do the calls in the Action and then just map them to separate variables in the template, but this wasn’t really as pretty as it could be. Instead I wrote a simple wrapper around the SolrDocumentList, which is not a list in itself, but exposes all the elements through it’s getDocumentList-method. That way we can access it in the template by calling ${solrDocumentList.documentList…}.
I’ve included the simple, simple wrapper here. It should be expanded with access to Facet fields etc, but this should be a simple indicator of my suggested solution.
public class SolrSearchResult
{
SolrDocumentList resultDocuments = null;
public SolrSearchResult(SolrDocumentList results)
{
this.resultDocuments = results;
}
public long getNumFound()
{
return this.resultDocuments.getNumFound();
}
public long getStart()
{
return this.resultDocuments.getStart();
}
public float getMaxScore()
{
return this.resultDocuments.getMaxScore();
}
public SolrDocumentList getDocumentList()
{
return this.resultDocuments;
}
public void setDocumentList(SolrDocumentList results)
{
this.resultDocuments = results;
}
}
Any comments and updates are of course as always welcome.
I know this is an OLD post, but I thought this would be a better solution than having to create a wrapper class:
numFound :
(: yay for escaping… trying again:
<c:set var=”numFound”><jsp:getProperty name=”solrDocumentList” property=”numFound”/></c:set>
numFound: <c:out value=”${numFound}”/>