json - Solr - How to get search result in specific format -
while exploring example indexing wikipedia data in solr, how can expected result (i.e. same data imported)?
is there process can achieve through configurations not group query, because have data having lots of inner tags.
i explored xslt result transformation, looking json response.
imported doc:
<page> <title>accessiblecomputing</title> <ns>0</ns> <id>10</id> <redirect title="computer accessibility" /> <revision> <id>381202555</id> <parentid>381200179</parentid> <timestamp>2010-08-26t22:38:36z</timestamp> <contributor> <username>olenglish</username> <id>7181920</id> </contributor> </revision> </page>
solrconfig.xml:
<dataconfig> <datasource type="filedatasource" encoding="utf-8" /> <document> <entity name="page" processor="xpathentityprocessor" stream="true" foreach="/mediawiki/page/" url="data/enwiki-20130102-pages-articles.xml" transformer="regextransformer,dateformattransformer" > <field column="id" xpath="/mediawiki/page/id" /> <field column="title" xpath="/mediawiki/page/title" /> <field column="revision" xpath="/mediawiki/page/revision/id" /> <field column="user" xpath="/mediawiki/page/revision/contributor/username" /> <field column="userid" xpath="/mediawiki/page/revision/contributor/id" /> <field column="text" xpath="/mediawiki/page/revision/text" /> <field column="timestamp" xpath="/mediawiki/page/revision/timestamp" datetimeformat="yyyy-mm-dd't'hh:mm:ss'z'" /> <field column="$skipdoc" regex="^#redirect .*" replacewith="true" sourcecolname="text"/> </entity> </document> </dataconfig>
response solr query:
"response": { "numfound": 1, "start": 0, "docs": [ { "id": "10", "timestamp": "2010-08-26t17:08:36z", "revision": 381202555, "titletext": "accessiblecomputing", "userid": 7181920, "user": "olenglish" } ] }
expected response:
"response": { "numfound": 1, "start": 0, "docs": [ { "id": "10", "timestamp": "2010-08-26t17:08:36z", "revision": 381202555, "titletext": "accessiblecomputing", "contributor": [{ "userid": 7181920, "user": "olenglish" }] } ] }
if don't idea of using xsltresponsewriter (which can int outputting results in json well), can create own searchcomponent
, modify output. when use custom searchcomponent
can apply different responsewriters output (xml, json, csv, xslt, etc.).
you can learn how create custom searchcomponent
in this article, example.
to use xsltresponsewriter
, add code solrconfig.xml
:
<queryresponsewriter name="xslt" class="org.apache.solr.response.xsltresponsewriter"/>
add json.xsl
file conf/xslt
folder, has transformation rules xml output (when use wt=xml
in query), 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 can response using url like:
http://localhost:8983/solr/select/?q=id:10&wt=xslt&tr=json.xsl