If you don't like the idea of using XsltResponseWriter (which can help int outputting the results in JSON as well), you can create your own SearchComponent
, which will modify the output. When you use a custom SearchComponent
you can apply different ResponseWriters to the output (xml, json, csv, xslt, etc.).
You can learn how to create a custom SearchComponent
in this article, for example.
To use XsltResponseWriter
, add this code to solrconfig.xml
:
<queryResponseWriter name="xslt" class="org.apache.solr.response.XSLTResponseWriter"/>
Add a json.xsl
file to conf/xslt
folder, which has transformation rules for your XML output (when you use wt=xml
in your query), something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="text" indent="no" media-type="application/json"/>
<xsl:template match="result">
<xsl:text>{"response":{"docs":[</xsl:text>
<xsl:apply-templates select="doc"/>
<xsl:text>]}}</xsl:text>
</xsl:template>
<xsl:template match="doc">
<xsl:if test="position() > 1">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:text>{"contributor": [{"userId": </xsl:text><xsl:value-of select="userId"/><xsl:text>, "user": "</xsl:text><xsl:value-of select="user"/><xsl:text>"}]}</xsl:text>
</xsl:template>
</xsl:stylesheet>
Then you can get this response using a url like:
http://localhost:8983/solr/select/?q=id:10&wt=xslt&tr=json.xsl