You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by "sachin.kale" <sa...@live.com> on 2012/07/12 20:23:57 UTC
Solr custom XSLT with highlighting
I have created the following sample xslt which highlights the keyword in the
content field for anybody who wants to use it.
1) In your solrconfig.xml
<requestHandler name="/select" class="solr.SearchHandler">
<lst name="defaults">
...
Add following params to this defaults section
<str name="hl">on</str>
<str name="fl">highlighting,title,url</str>
<str name="wt">xslt</str>
<str name="tr">custom-solr.xsl</str>
<str name="hl.fl">title,content</str>
2) Create the following "custom-solr.xsl" under /solr/example/solr/conf/xslt
-------------------custom-solr.xsl (start)--[solr
3.0.6]--------------------------
<?xml version='1.0' encoding='UTF-8'?>
<xsl:transform version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent ="yes"/>
<xsl:output method="html" indent="yes" version="4.0"/>
<xsl:variable name="title" select="concat('Total
(',response/result/@numFound,' documents) found')"/>
<xsl:variable name="keyword" select="response/lst/lst/str[@name='q']"/>
<xsl:template match='/'>
<html>
<head>
<title><xsl:value-of select="$title"/></title>
<xsl:call-template name="css"/>
</head>
<body>
<xsl:value-of select="$title"/> matching {<xsl:value-of select="$keyword"/>}
<xsl:apply-templates select="response"/>
</body>
</html>
</xsl:template>
<xsl:template match="response">
<xsl:for-each select="result/doc">
<xsl:variable name="cTitle" select="arr[@name='title']/str"/>
<xsl:variable name="cUrl" select="str[@name='url']"/>
<xsl:variable name="hContent"
select="/response/lst[@name='highlighting']/lst[@name=$cUrl]/arr/str"/>
<xsl:attribute name="href"><xsl:value-of select="$cUrl"/></xsl:attribute>
<xsl:call-template name="unescapeEm">
<xsl:with-param name="val" select="$cTitle"/>
</xsl:call-template>
<div class="note">
<label>...</label>
<xsl:call-template name="unescapeEm">
<xsl:with-param name="val" select="$hContent"/>
</xsl:call-template>
</div>
</xsl:for-each>
</xsl:template>
<xsl:template name="unescapeEm">
<xsl:param name="val" select="''"/>
<xsl:variable name="preEm" select="substring-before($val, '<')"/>
<xsl:choose>
<xsl:when test="$preEm or starts-with($val, '<')">
<xsl:variable name="insideEm"
select="substring-before($val,'</')"/>
<xsl:value-of select="$preEm"/><strong><xsl:value-of
select="substring($insideEm, string-length($preEm)+5)"/></strong>
<xsl:variable name="leftover" select="substring($val,
string-length($insideEm) + 6)"/>
<xsl:if test="$leftover">
<xsl:call-template name="unescapeEm">
<xsl:with-param name="val" select="$leftover"/>
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$val"/>
</xsl:otherwise>
</xsl:choose>
<label>...</label>
</xsl:template>
<xsl:template match="*"/>
<xsl:template name="css">
</xsl:template>
</xsl:transform>
-------------------custom-solr.xsl (end)---------------------------------
--
View this message in context: http://lucene.472066.n3.nabble.com/Solr-custom-XSLT-with-highlighting-tp3994704.html
Sent from the Solr - User mailing list archive at Nabble.com.