Solr - How to get search result in specific format

放肆的年华 提交于 2019-12-21 20:55:33

问题


While exploring example for indexing wikipedia data in Solr, how can we get the expected result (i.e. same as data imported)?

Is there any process that we can achieve it through configurations not from group query, because I have data which having lots of inner tags.

I explored xslt result transformation, but i am looking for 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 by 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"
        }]
      }
    ]
  }

回答1:


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() &gt; 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


来源:https://stackoverflow.com/questions/18307246/solr-how-to-get-search-result-in-specific-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!