[xquery-talk] search mechanism

Wolfgang Meier wolfgang at exist-db.org
Thu Apr 6 17:03:13 PDT 2006


> This does work but seems rather complicated to me!
> What do you think?
> I would think that it should be possible (and better?) to do this entirely
> in one single XQuery ...?

Guessing from the unusual &= operator, I assume you are using eXist,
which provides a nice "eval" function. To handle dynamic expressions
like these, it is sometimes easier to construct the user query as a
string, then pass it to util:eval to get an intermediate result, which
can be further processed by the main XQuery. For example:

let $query := construct-query-from-params()
let $result := util:eval($query)
for $doc in $result
order by fn:year-from-date($doc//date) descending
return $doc

> Second, I only want to show the first 10 hits to the user and generate links
> ( [11-20] [21-30] etc.) to allow accessing the rest.
> So far I am thinking about storing the start- and stop-position plus all (up
> to 4 queries) that have led to the results with each link.

If this is going to be a web application, one possibility would be to
store the results in the HTTP session. This should not be a problem if
you are just keeping references to nodes stored in the db. Again,
there are functions for this job. For example, my little sandbox app
(http://demo.exist-db.org/sandbox/sandbox.xql - code is in the
distribution) first only returns the number of hits for the query and
stores the result set into the session:

let $results := util:eval($qu)
return (
 request:set-session-attribute("cached", $results),
 <result hits="{count($results)}" />

Later, the items are retrieved asychronously by a background
Javascript and passed to a stylesheet:

let $cached := request:get-session-attribute("cached")
   let $item :=
       <item num="{$num}">
       transform:transform($item, doc($sandbox:XML_HIGHLIGHT_STYLE), ())


More information about the talk mailing list