Typically 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 –
- XML Response Writer
- XSLT Response Writer
- JSON Response Writer
- PHP Response Writer
- PHP Serialized Response Writer
- Velocity Response Writer
- Ruby Response Writer
- Python Response Writer
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.
<queryResponseWriter name="xslt"> <int name="xsltCacheLifetimeSeconds">5</int> </queryResponseWriter>
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.
For e.g.: &wt=xslt&tr=solr_result_attrib_to_tag_v0.1.xsl
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
<!-- desc: Transforming Solr Search Result XML type structure to tag structure (attribute to tag) using XSLT filename: solr_result_attrib_to_tag_v0.1.xsl author: Jeevanandam M. ([email protected]) date: April 12, 2012 1:02:22 AM GMT+05:30 version 0.1 --> <SolrResponse> <Header> <status>0</status> <QTime>0</QTime> <numFound>17</numFound> <Params> <indent>on</indent> <start>0</start> <tr>solr_result_attrib_to_tag_v0.1.xsl</tr> <q>*:*</q> <wt>xslt</wt> <version>2.2</version> <rows>10</rows> </Params> </Header> <Documents> <Document> <id>GB18030TEST</id> <name>Test with some GB18030 encoded characters</name> <inStock>true</inStock> <price>0.0</price> <features> <string>No accents here</string> <string>这是一个功能</string> <string>This is a feature (translated)</string> <string>这份文件是很有光泽</string> <string>This document is very shiny (translated)</string> </features> </Document> <Document> <id>SP2514N</id> <manu>Samsung Electronics Co. Ltd.</manu> <name>Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133</name> <store>35.0752,-97.032</store> <inStock>true</inStock> <popularity>6</popularity> <manufacturedate_dt>2006-02-13T15:26:37Z</manufacturedate_dt> <price>92.0</price> <cat> <string>electronics</string> <string>hard drive</string> </cat> <features> <string>7200RPM, 8MB cache, IDE Ultra ATA-133</string> <string>NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor</string> </features> </Document> <Document> <id>6H500F0</id> <manu>Maxtor Corp.</manu> <name>Maxtor DiamondMax 11 - hard drive - 500 GB - SATA-300</name> <store>45.17614,-93.87341</store> <inStock>true</inStock> <popularity>6</popularity> <manufacturedate_dt>2006-02-13T15:26:37Z</manufacturedate_dt> <price>350.0</price> <cat> <string>electronics</string> <string>hard drive</string> </cat> <features> <string>SATA 3.0Gb/s, NCQ</string> <string>8.5ms seek</string> <string>16MB cache</string> </features> </Document> <Document> <id>F8V7067-APL-KIT</id> <manu>Belkin</manu> <name>Belkin Mobile Power Cord for iPod w/ Dock</name> <store>45.17614,-93.87341</store> <inStock>false</inStock> <popularity>1</popularity> <manufacturedate_dt>2005-08-01T16:30:25Z</manufacturedate_dt> <price>19.95</price> <weight>4.0</weight> <cat> <string>electronics</string> <string>connector</string> </cat> <features> <string>car power adapter, white</string> </features> </Document> <Document> <id>IW-02</id> <manu>Belkin</manu> <name>iPod & iPod Mini USB 2.0 Cable</name> <store>37.7752,-122.4232</store> <inStock>false</inStock> <popularity>1</popularity> <manufacturedate_dt>2006-02-14T23:55:59Z</manufacturedate_dt> <price>11.5</price> <weight>2.0</weight> <cat> <string>electronics</string> <string>connector</string> </cat> <features> <string>car power adapter for iPod, white</string> </features> </Document> <Document> <id>MA147LL/A</id> <includes>earbud headphones, USB cable</includes> <manu>Apple Computer Inc.</manu> <name>Apple 60 GB iPod with Video Playback Black</name> <store>37.7752,-100.0232</store> <inStock>true</inStock> <popularity>10</popularity> <manufacturedate_dt>2005-10-12T08:00:00Z</manufacturedate_dt> <price>399.0</price> <weight>5.5</weight> <cat> <string>electronics</string> <string>music</string> </cat> <features> <string>iTunes, Podcasts, Audiobooks</string> <string>Stores up to 15,000 songs, 25,000 photos, or 150 hours of video</string> <string>2.5-inch, 320x240 color TFT LCD display with LED backlight</string> <string>Up to 20 hours of battery life</string> <string>Plays AAC, MP3, WAV, AIFF, Audible, Apple Lossless, H.264 video</string> <string>Notes, Calendar, Phone book, Hold button, Date display, Photo wallet, Built-in games, JPEG photo playback, Upgradeable firmware, USB 2.0 compatibility, Playback speed control, Rechargeable capability, Battery level indication</string> </features> </Document> <Document> <id>TWINX2048-3200PRO</id> <manu>Corsair Microsystems Inc.</manu> <name>CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail</name> <payloads>electronics|6.0 memory|3.0</payloads> <store>37.7752,-122.4232</store> <inStock>true</inStock> <popularity>5</popularity> <manufacturedate_dt>2006-02-13T15:26:37Z</manufacturedate_dt> <price>185.0</price> <cat> <string>electronics</string> <string>memory</string> </cat> <features> <string>CAS latency 2, 2-3-3-6 timing, 2.75v, unbuffered, heat-spreader</string> </features> </Document> </Documents> </SolrResponse>
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.