Can i sort results by Metadata?

8 views
Skip to first unread message

Snorri Páll

unread,
Dec 1, 2008, 5:29:57 PM12/1/08
to Google Search Appliance/Google Mini - Google Search Appliance/Google Mini
Hi i was wondering if i could use <xsl:sort> to apply a different sort
method to results?

I need results ordered by a certain <MT> attribute.. where N="nafn"
but sort by V.

<MT N="nafn" V="John Johnson"/>

<R N="1">
<U>http://www.hi.is/is/simaskra/4389</U>
<UE>http://www.hi.is/is/simaskra/4389</UE>
<UD>http://www.hi.is/is/simaskra/4389</UD>
<T>Starfsmaður | Háskóli Íslands</T>
<RK>0</RK>
<FS NAME="date" VALUE="2008-11-06"/>
<MT N="display-type" V="starfsmadur"/>
<MT N="nafn" V="John Johnson"/>
<MT N="starfsheiti" V="Title"/>
<MT N="simanumer" V="0"/>
<MT N="farsimi" V="0"/>
<MT N="netfang" V="ma...@domain.com"/>
<MT N="stafsetning" V="Tæknigarður"/>
<MT N="mynd" V="http://www.khi.is/files/staffmyndir/
engin.jpg"/>

<S>lorem ipsum </S>
<LANG>is</LANG>
<HAS>
<L/>
<C SZ="11k" CID="mtpniBdOWosJ" ENC="UTF-8"/>
</HAS>
</R>

Joe D'Andrea

unread,
Dec 1, 2008, 7:00:22 PM12/1/08
to Google-Search-...@googlegroups.com
On Mon, Dec 1, 2008 at 5:29 PM, Snorri Páll <sno...@snorripall.com> wrote:
>
> Hi i was wondering if i could use <xsl:sort> to apply a different sort
> method to results?
>
> I need results ordered by a certain <MT> attribute.. where N="nafn"
> but sort by V.

You sure can ... but with caveats. Look in the XSLT for:

<!-- for real results -->
<xsl:apply-templates select="RES/R">
<xsl:with-param name="query" select="$query"/>
</xsl:apply-templates>

Let's say you wanted to sort numerically in ascending order. This
would do the trick:

<!-- for real results -->
<xsl:apply-templates select="RES/R[MT/@N='nafn']">
<xsl:sort select="MT/@V" data-type="number" order="ascending" />
<xsl:with-param name="query" select="$query"/>
</xsl:apply-templates>

The square brackets act as a filter for metadata with a name of "nafn"
while the xsl:sort element lets us zone in on the value.

Now for the caveats:

First, be wary of R/@L (level). You may want to disable directory
crowding[1] in this case anyway (via filter=p).

Second, using my particular example, if any results do NOT have
metadata with a name of "nafn" they will go missing ... and knock your
total out of whack (e.g., "Results 1 - 10").

While you could mess around with the top_sep_bar template, it would be
a lot cleaner to always have a consistent 10 (or 20, 30, etc.) results
per page.

To work around this, I was about to suggest letting the GSA filter by
metadata[2] for you (via requiredfields), but now I'm not sure if that
takes wildcards.

Hmm. Only one way to find out! "Left as a well-meaning exercise for
the reader." ;)

--
Joe D'Andrea
Liquid Joe LLC
+1 (908) 781-0323
www.liquidjoe.biz

[1] http://snurl.com/70d8u [code_google_com]
[2] http://snurl.com/4u015 [code_google_com]

Snorri Páll

unread,
Dec 2, 2008, 12:33:09 PM12/2/08
to Google Search Appliance/Google Mini - Google Search Appliance/Google Mini
Hey thanx for the reply. I ended up with this which works but i'm
having problems with the language sort, doesnt seem to sort correctly
even if i specify a lang parameter for the sort, ever heard of that?

<xsl:apply-templates select="RES/R">
<xsl:with-param name="query" select="$query"/>
<xsl:sort select="MT[@N='nafn']/@V" lang="is"
order="ascending"/>
</xsl:apply-templates>

Snorri

On Dec 2, 12:00 am, "Joe D'Andrea" <jdand...@gmail.com> wrote:

Joe D'Andrea

unread,
Dec 2, 2008, 2:16:12 PM12/2/08
to Google-Search-...@googlegroups.com
On Tue, Dec 2, 2008 at 12:33 PM, Snorri Páll <sno...@snorripall.com> wrote:

> ... i'm having problems with the language sort, doesnt seem to sort
> correctly even if i specify a lang parameter for the sort ...


>
> <xsl:apply-templates select="RES/R">
> <xsl:with-param name="query" select="$query"/>
> <xsl:sort select="MT[@N='nafn']/@V" lang="is"
> order="ascending"/>
> </xsl:apply-templates>

The GSA uses libxslt ... which I suspect doesn't support the lang
attribute, largely because support for it appears to have been added
to libxslt only this past June. :(

"Tue Jun 3 18:26:26 CEST: patch ... adding xsl:sort lang support using
the locale support from the C library."[1]

(Again, this is for libxslt in general.)

Meanwhile, you should be OK putting xsl:sort after xsl:with-param.
("When used within xsl:for-each, xsl:for-each-group, or
xsl:perform-sort, xsl:sort elements must occur before any other
children."[2])

Final reality check, "the effective value of the attribute must be a
value that would be valid for the xml:lang attribute."[3] In your case
it's Icelandic (is) which is indeed the ISO code.

That reminds me ... I really have to visit Reykjavik again!

--
Joe D'Andrea
Liquid Joe LLC
+1 (908) 781-0323
www.liquidjoe.biz

[1] http://www.xmlsoft.org/XSLT/ChangeLog.html
[2] http://www.w3.org/TR/xslt20/#sorting
[3] http://www.w3.org/TR/xml/#sec-lang-tag

Reply all
Reply to author
Forward
0 new messages