Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • firepol 125 posts 173 karma points
    Oct 21, 2011 @ 18:24
    firepol
    0

    how does the score work?

    Hi Doug,

    I'm trying to customize the score and I'd like to display for example "5 stars" if the score is very high, 4 stars a big less etc.

    So I guess I'll modify the XSLT accordingly...

    I assume the maximum score is "100"? Thanks for letting me know ;)

    cheers

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Oct 21, 2011 @ 21:20
    Douglas Robar
    1

    The score is not limited or normalized to a 100 point scale, though you could certainly modify xsltsearch.xslt to save the maximum score and divide all scores by that maximum and then multiply by 100 to get a normalized 100 point scale. You could then easily show a number of "stars".

     

    If you're interested in the nitty gritty...the math goes something like this to calculate the 'weight' of a page. To make it clearer, let's assume the following two pages and this XSLTsearch configuration:

     

    XSLTsearch macro parameters include:
    searchFields=pageName,@nodeName,metaKeywords,bodyText

     

    3 Pages in the site:
    @nodeName = Rock
    pageName = Rock 'n' Roll
    bodyText = All rock, all the time.

    @nodeName = Jazz
    pageName = (not set)
    bodyText = All that jazz.

    @nodeName = Classical
    pageName = Classical
    bodyText = Mostly piano and violin and stuff.

     

    So let's imagine we searched for 'all'.

    The letters 'a-l-l' are found in the bodyText of the page called 'Rock', so that page will get returned. It's total score is determined by how many times the search term is found in the various searchFields. 

    The order of the searchField properties determine the weight for each match. The last item in the list of searchFields (the 'bodyText' property) will have a multiplier of 1. The next to last property (metaKeywords) a multiplier of 2. The nodeName attribute (note the @ sign indicating it is an attribute in the xml) would be multiplied by 4, and the pageName property multiplied by 8. The idea here is that when a search term is found in an earlier property or attribute it is much more significant than if found later in the list of searchFields. If the term you are looking for is in the pageName, for instance, that is very significant, much more significant than if it happens to show up even a few times in the bodyText. The order of the searchFields is therefore very important to scoring results in a default XSLTsearch implementation. 

    So, searching for 'all' gives a score of 2 for the the Rock page. That is, the search term is found twice in bodyText, and bodyText has a weight multiplier of 1 for each match. Similarly, the Jazz page gets a score of 1. Therefore, Rock is returned first and Jazz after that.

     

    Let's imagine we searched for the letter 'a':

    Rock page has a score of: 2 matches in bodyText * 1 = 2
    Jazz page has a score of: 1 match in nodeName * 4, plus 3 matches in bodyText * 1 = 7
    Classical page has a score of: 2 matches in pageName * 8, plus 2 matches in nodeName * 4, plus 3 matches in bodyText * 1 = 27

    The results would show Classical, then Jazz, then Rock.

    cheers,
    doug. 

  • firepol 125 posts 173 karma points
    Oct 22, 2011 @ 18:14
    firepol
    0

    Hi Doug, thanks for your explanation, mate!

    And yeah I'll check how to implement the stars ;) maybe I'll share the results.

    Cheers and thanks again for the support

    Paolo

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Oct 22, 2011 @ 19:33
    Douglas Robar
    0

    Yes, please do share how you do it. Personally I think I'd do the math to normalize to a percentage as descirbed briefly above. Then, apply a class to each search result output such as class="star5" or class="star3" and then in your custom css you'd display the proper image of stars.

    cheers,
    doug. 

  • firepol 125 posts 173 karma points
    Oct 25, 2011 @ 14:02
    firepol
    0

    Hei Doug, before I get some headache (xsl/xpath is not my strenght) maybe you can help me... I think to make it easy I just need to select the maximum score, from the pageScoreList (I'll save it in a parameter/variable). Once I have that value I'll do the normalize math. Thanks for letting me know ;)

    cheers

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Oct 25, 2011 @ 19:57
    Douglas Robar
    0

    This was an interesting idea so I figured it out. Here's how you do it, along with some explanation of what's being done and why.

    1. Create a variable for the maximum number of stars you want to display. This makes it easy to modify later if you decide you want more or fewer options.

    2. Create a variable that contains the maximum score from all results.

    You'll add these two variables between the <div id="xsltsearch_results"> and <xsl:for-each select="$matchedNodes"> lines, which are at about line 310 depending on your version of XSLTsearch. The result in context should look like this:

    <!-- display search results to the screen -->
    <div id="xsltsearch_results">
        <xsl:if test="count($matchedNodes) = 0">
            <xsl:text disable-output-escaping="yes"> </xsl:text>
        </xsl:if>
    
     <!-- two new variables for star ratings --> <xsl:variable name="maxStars" select="5" /> <xsl:variable name="maxScore"> <xsl:for-each select="$matchedNodes"> <!-- sort on the page score of the node, within ALL the nodes to return (the sorting must happen before choosing the nodes for this page) --> <xsl:sort select="substring-before(substring-after($pageScoreList, concat(';',generate-id(.),'=')),';')" data-type="number" order="descending"/> <xsl:if test="position() = 1"> <xsl:value-of select="substring-before(substring-after($pageScoreList, concat(';',generate-id(.),'=')),';')" /> </xsl:if> </xsl:for-each> </xsl:variable> 
        <xsl:for-each select="$matchedNodes">
            <!-- sort on the page score of the node, within ALL the nodes to return (the sorting must happen before choosing the nodes for this page) -->
            <xsl:sort select="substring-before(substring-after($pageScoreList, concat(';',generate-id(.),'=')),';')" data-type="number" order="descending"/>
    
            <!-- only the nodes for this page -->
            <xsl:if test="position() &gt;= $startMatch and position() &lt;= $endMatch">
    
    You'll notice that I've used a for-each loop in the maxScore variable that I totally ripped off from the lines below. Don't be confused, you need one for-each in the maxScore variable and then another (the existing one) right after it to actually display the results in the correct order.
    3. we need to do some math to normalize the results. While we're at it, we'll make a span with a class that you'll be able to style with CSS to display your stars. We'll add a new star rating span below the pageScore/relvance rating (around line 350 or so). This is the last thing before we close the </p> tag.
        <!-- show the pageScore/relevance rating -->
        <xsl:if test="$showScores != '0'">
            <span class="xsltsearch_score">
                <xsl:text> (</xsl:text>
                <xsl:value-of select="$dictionaryScore-Score"/>
                <xsl:text>: </xsl:text>
                <xsl:value-of select="substring-before(substring-after($pageScoreList, concat(';',generate-id(.),'=')),';')"/>
                <xsl:text>)</xsl:text>
            </span>
        </xsl:if>
    
        <!-- show star rating --> <span> <xsl:attribute name="class"> <xsl:text>xsltsearch_stars</xsl:text> <xsl:variable name="currentScore" select="substring-before(substring-after($pageScoreList, concat(';',generate-id(.),'=')),';')"/> <xsl:value-of select="floor($currentScore div $maxScore * $maxStars)" /> </xsl:attribute> </span>
    </p>
    
    At this point you'll get the following output for your first result (the one with the maximum number of stars):
    <span class="xsltsearch_stars5" />
    That'll be easy to style with an image of stars and some CSS to display the right number of them.
    So there you go! A fun exercise. 

    cheers,
    doug. 

  • firepol 125 posts 173 karma points
    Oct 26, 2011 @ 10:45
    firepol
    0

    That's awesome. I just did exactly as you said. It works great. Maybe you can include an option "showStars" for the new release ;)

    Thanks again, good job!

Please Sign in or register to post replies

Write your reply to:

Draft