A relatively unknown, but powerful way to integrate with Solr is via its support for
XSLT (eXtensible Stylesheet Language Transformations). XSLT is a specification for transforming XML documents into other XML formats, which includes HTML. There are various implementations of this specification and Java includes one. Solr provides a query response writer that executes a provided XSLT stylesheet to transform Solr's XML search results into some other format. Solr comes with a couple of examples in ./conf/xslt/
. Here is an example of transforming search results into an RSS feed:
http://localhost:8983/solr/mbartists/select/?q=marley&wt=xslt&tr=example_rss.xsl
The wt
parameter triggers the use of XSLT, and the tr
parameter supplies the name of the stylesheet to be used.
There are some caveats to keep in mind for XSLT support. Internally, XSLT files are compiled before they are used, and while Solr will cache the last compiled XSLT for a period of time, configured in the queryResponseWriter
via the xsltCacheLifetimeSeconds
parameter, it only caches a single one. So, if you use more than one XSLT stylesheet, then you are likely to find degraded performance. Additionally, because Solr has to have the entire XML document in memory first to render the XSLT stylesheet, you may run into memory issues if you are returning large numbers of results.
Need a debugger for Solr queries?
Want to understand how Solr determined the score for the documents you returned? You can use example.xsl
to quickly transform your results to HTML and expose the query debugging information in an easy-to-read format. Just make sure you specify the score field to be returned so that you get the toggle for the scoring info: http://localhost:8983/solr/mbartists/select/?q=smashing&wt=xslt&tr=example.xsl&fl=*,score&debugQuery=true
.