marklogicrelevancects-search

How to preserve relevance order of cts:search results


I have the following codes:

1)

for $song in cts:search(fn:doc(), "night")
return $song/ts:top-song/ts:title/text() 

2)

cts:search(fn:doc(), "night")/ts:top-song/ts:title/text()

cts:search returns documents as per the relevance. Both the codes, return results in a different order. Which would return the result with the correct relevance and why ?


Solution

  • The first.

    In the second, XPath is applied to the entire sequence returned by cts:search, but as per XPath standard the result of that is re-ordered into document-order, which becomes unpredictable when the nodes in the sequence come from different documents.

    The first applies a FLWOR iteration on the sequence, which ensures keeping the order, and the XPath is applied to each item in the sequence separately.

    HTH!