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 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:copy-of select="@*" />
    <xsl:copy-of select="*" />

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.

Tip: An easy way to add conditional queries to your document is with the Document Condition Query option. This allows you to specify a secondary query as a document condition and does not require you to enter the condition in XSL. To specify a value for the Document Condition Query option, open source associated with your project. Select the Form tab, and click edit beside VSE Source Form. Expand the Advanced section to display the Document Condtion Query option. Enter the query as text as you normally would, and it will be translated into XML before being passed to the search collection. Click OK to save your changes.

To proceed with this tutorial, click Preserving Non-Document Elements.