Conditional Boosting
The Free XSL parsers in the previous sections boosted all documents that were returned in your search results. This may be appropriate if you are creating a source that itself only generates boost items. (In that case, you might want to set the document element's boost-and-list attribute to boost-only so that these results are only in the boost, not in your standard results.)
More commonly, you will want to conditionalize your parser so that only items that match a specific conditional expression are boosted. This conditional expression is usually based on an analysis of the results that you are returning, such as a timestamp, specific strings that occur in the snippet, or specific values in specific fields that are returned as part of your search results. Another common usage is positional filtering boosting the first three results of a query.
For the purposes of this tutorial, we will modify the XSL parser to only boost items within the search results where the snippet field contains the string Tommy in a case-insensitive fashion. ("Tommy Flat") is a recurring character in some the books that make up the binning-collection search collection.
If it is not already open, return to the binning-collection source screen by clicking the Sources entry in the left-hand Watson™ Explorer Engine administration tool navigation bar to display the most recently opened source, which should be the binning-collection source. Select the Parser tab, and click Edit to edit your existing Free XSL parser. Modify the parser so that it looks like the following:
<xsl:template match="/"> <boost name="df" /> <xsl:apply-templates select="//document" /> </xsl:template> <xsl:template match="document"> <document url="{@url}"> <xsl:if test="viv:test(content[@name='snippet'], 'tommy', 'case-insensitive-regex')"> <xsl:attribute name="boost-name">df</xsl:attribute> <xsl:attribute name="boost-display">boost-and-list</xsl:attribute> </xsl:if> <xsl:copy-of select="@*" /> <xsl:copy-of select="*" /> </document> </xsl:template>
This parser is largely the same as the parser introduced in Creating a Primary Parser and that we refined in Preserving Attributes, except that it always creates a <document> node but only sets its boost-name and boost-display attributes if a specific condition is met.
The condition used to identify whether a document node should be boosted is defined in an xsl:if clause that uses the viv:test function to compare the contents of the snippet field (the extracted summary of document content) with the string halley using a case-insensitive regular expression. All sub-elements and attributes of the original <document> node are still copied to the output document, but the output document itself is only added to the <boost> node if the snippet field matches halley. Because the boost-display field is set to boost-and-list, any results that match tommy will be appear in your general search results as well as being boosted.
Click OK to save your changes, re-open the project that you created in the first step of the Tutorial: Spotlight tutorial, and try a search for the word car.
This output shows that three of the snippets for query results that match the word car contain some case-insensitive version of the string tommy.
To proceed with this tutorial, click Preserving Non-Document Elements.