Solr Search Result Attribute-to-Tag using XsltResponseWriter

Solr Search Result Attribute-to-Tag using XsltResponseWriterTypically everyone is happy with Solr search result XML structure which is default response writer called XMLResponseWriter.  However sometimes to meet certain requirements we may choose to customize the Solr Query Response Writer.  Solr provides a support for multiple query response writer, this way we can make custom writer based on requirement.

Out-of-the-Box response writer are –

Example directory in the Solr distribution has few examples for how to make use of XSLT capability in Solr (like RSS, ATOM, Luke etc).  Now I’m going to show you how to make use of Solr XLST Response Writer in a generic way i.e. transforming Solr search result XML attributes into XML tag in the Search result response.


Things to Know

If you’re familiar with following pointers; you will find below configuration as a piece of cake. Don’t worry even slightest knowledge of Solr will do,  just follow the given steps in How to do section.

  • Solr directory structure
  • Editing Solr configuration XML  solrconfig.xml
  • Solr multi-core configuration & structure (this is applicable only; if you have mapped solr configuration files from external directory)


How to do: Step by Step approach

I will be taking you to the configuration, step-by-step in a simple way and easy to understand. I’m proceeding further with assumption of Solr instance already in place at your end.  If you have queries please leave a comment.

Step 1

Download XSLT(solr_result_attrib_to_tag_v0.1.xsl) from GitHub repository for transforming Solr search result attribute-to-tag using XsltResponseWriter.

Step 2

Go to directory called conf <solr.home>/<solr-core-name-if-any>/conf/.  Here you will find all the Solr configuration like schema.xml, solrconfig.xml, stopwords.txt, synonyms.txt, etc.

  • Under conf, create a directory called xslt (note: if directory present already; ignore this step)
  • Place downloaded XSLT file solr_result_attrib_to_tag_v0.1.xsl under directory xslt

Step 3

Open up solrconfig.xml in your favorite text editor.  Look for tag called <queryResponseWriter ... and check whether XSLTResponseWriter is already configured; if configured ignore this step else configure one.

Save your solrconfig.xml file, for performance consideration have a look here.

Step 4

Solr provides option to have more than one QueryResposeWriter, however only one can be default and others could be invoked specifying query param called wt=<query-response-writer-name> and if you have more than one XSLT file configured in Solr of-course you have to tell Solr which one to use for result transformation.

The value of wt is name you defined in solrconfig.xml for query response writer.


Completion

That’s it, we have completed XSLT response writer configuration with Solr Search Result Attribute-to-Tag using XsltResponseWriter using myjeeva.com XSLT file solr_result_attrib_to_tag_v0.1.xsl

Let’s do some test on new XSLT configuration, go to your Solr Admin http://<host-name>:<port-no-if-any>/solr/<solr-core-name-if-any>/admin/ and perform some search, you will get a search result.

Typical XML structure of Solr search result on the browser, similar to sample of Typical XML structure.

Now place your cursor on browser address bar at end & add following query params &wt=xslt&tr=solr_result_attrib_to_tag_v0.1.xsl hit the enter key or go button.

Newly transformed Solr search result XML will be on your browser and click here to download sample New XML Tag structure


Performance Consideration

For better performance in production environment use much higher value on xsltCacheLifetimeSeconds.  Solr does cache of last used XSLT transform, and reuses that.  XSLT gets recompiled every xsltCacheLifetimeSeconds, even if it has not changed.  This simplistic caching mechanism is good enough in many cases.