我在 Solr 中有一个产品数据。 Solr 已经通过查询将它们提供为 XML。
但是,我需要将数据转换为不同的 XML 格式(只是 xml 节点的名称不同),以便将它们作为提要提供给其他应用程序。
知道吗,我怎样才能从 Solr 快速做到这一点?
最佳答案
我终于设法使用现有的响应编写器之一来完成此操作。这不需要编写新的响应编写器。这是我的做法。
我使用 XSLTResponseWriter 生成自定义格式 xml。您可以在这里找到更多详细信息:http://wiki.apache.org/solr/XsltResponseWriter
您可以在此处找到有关如何使用响应编写器的更多信息:https://wiki.apache.org/solr/QueryResponseWriter
好的,现在在使用它之前,需要对其进行配置。
第 1 步:在 solrconfig.xml 中定义 XSLT 的 QueryResponseWriter
结束查询组件后,将以下代码添加到 solrconfig.xml 中。
<!--
Changes to XSLT transforms are taken into account
every xsltCacheLifetimeSeconds at most.
-->
<queryResponseWriter name="xslt" class="org.apache.solr.response.XSLTResponseWriter">
<int name="xsltCacheLifetimeSeconds">5</int>
</queryResponseWriter>
您可以在 http://wiki.apache.org/solr/XsltResponseWriter 找到其文档
第 2 步:使用正确的 xslt 格式或自定义您自己的格式
您可以使用默认 Solr 下载中提供的现有 xslt 格式,也可以使用它来按照您希望的工作方式进行修改。已经提供了 5 种示例格式。假设您使用 example.xsl 生成一个包含所有字段的 html 文档,那么您需要像这样查询它。
我将其定制为使用 custom.xsl 文件格式来实现我自己的格式。我稍后再讲。
第 3 步:使用 XSLT 查询您的 Solr
http://localhost:8983/solr/mysolrcore/select?q=*:*&wt=xslt&tr=default.xsl&&rows=10
这将查询 solr 并以 default.xsl 中定义的格式显示数据。请注意 wt 和 tr 参数。您可以在结果行中传递所需的记录数。
使用 XSLT 自定义 XML 格式
以下是我如何使用 xslt 格式化自定义 xml 格式。希望这对某人有帮助。
我使用 example_rss.xsl 作为基础,并对其进行了如下修改。
<?xml version='1.0' encoding='UTF-8'?>
<!--
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
-->
<!--
Sample transform of Solr query results to custom XML format
-->
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output
method="xml"
encoding="utf-8"
media-type="application/xml"
/>
<xsl:template match='/'>
<xml version="1.0">
<xsl:apply-templates select="response/result/doc"/>
</xml>
</xsl:template>
<!-- search results xslt -->
<xsl:template match="doc">
<xsl:variable name="id" select="str[@name='Id']"/>
<xsl:variable name="timestamp" select="date[@name='timestamp']"/>
<item>
<id><xsl:value-of select="int[@name='Id']"/></id>
<title><xsl:value-of select="str[@name='Name']"/></title>
<link>
http://localhost:8983/solr/mysolrcore/<xsl:value-of select="string[@name='url']"/>p-<xsl:value-of select="int[@name='Id']"/>
</link>
<image>
<xsl:value-of select="str[@name='ImageURL']"/>
</image>
<category><xsl:value-of select="arr[@name='Category']"/></category>
<availability><xsl:value-of select="bool[@name='StockAvailability']"/></availability>
<description>
<xsl:value-of select="str[@name='ShortDescription']"/>
</description>
</item>
</xsl:template>
</xsl:stylesheet>
这会生成有效的 xml 文档,而无需编写您自己的自定义响应编写器。
关于solr - 来自 Solr 的自定义格式 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22861364/