xquery version "1.0";
declare namespace session="http://exist-db.org/xquery/session";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace util="http://exist-db.org/xquery/util";
declare namespace i18n="http://apache.org/cocoon/i18n/2.1";
import module namespace ristenutil="http://www.risten.no/shared/util" at "../xquery/ristenutil.xqm";
(: Map query field parameter to xpath selection :)
declare function local:query-field($field as xs:string) as xs:string
{
if ($field = "head") then
"common/head"
else if ($field = "def") then
"senses/sense/def"
else
"."
};
(: Create filter expression from query term, field and mode,
and add a classification filter if specified :)
declare function local:filter-expr($term as xs:string,
$field as xs:string,
$mode as xs:string) as xs:string
{
let $class := request:get-parameter("class", "all")
let $c := if ($class = 'all') then ".//@top &= '*'"
else if (contains($class, '-') ) then
concat(".//@botm &= '", substring-after($class, '-'), "'" )
else if ( string-length($class) = 1) then
concat(".//@top &= '", $class, "'" )
else
concat(".//@mid &= '", $class, "'" )
(: Build the term search expression: :)
let $f := local:query-field($field),
$t := concat("'", $term, "'"), (: add single quotes around the search term :)
$t2 := replace($term,"\?",".?"), (: replace ? with .? :)
$t3 := replace($t2,"\*",".*"), (: replace * with .* :)
$t4 := replace($t3,"\(","\\("), (: escape left parenthesis :)
$t5 := replace($t4,"\)","\\)"), (: escape right parenthesis :)
$ttrad := concat("'^", $t5, "'") (: anchor the search string to each end :)
let $termsearch :=
if ($mode = "trad") then
concat("matches(", $f, ", ", $ttrad, ")")
else if ($mode = "near") then
concat("near(", $f, ", ", $t, ")")
else if ($mode = "exact") then
concat($f, " = ", $t)
else
concat($f, " &= ", $t)
return
(: Removed the condition $term = '*', since querying the entire db seems
acceptably fast - orig. condition was: $term = '*' or $term = '' :)
if ($term = '' ) then
$c
else if ( $class = 'all') then
$termsearch
else
concat($termsearch, " and ", $c)
};
(: Map order parameter to xpath for order by clause :)
(: Since the ordering element is not selectable, this function does
not make any sense any more, but I'll keep it for now just in case. :)
declare function local:order-expr($field as xs:string) as xs:string
{
if ($field = "head") then
"common/head"
else
"common/qa, $r/common/head"
};
(: Assemble the query string :)
declare function local:build-query($srchcoll as xs:string,
$srchlang as xs:string+,
$class as xs:string,
$term as xs:string,
$orderby as xs:string)
as xs:string
{
let $field := request:get-parameter("field", "any"),
$mode := request:get-parameter("mode", "all"),
$expr := local:filter-expr($term, $field, $mode),
$collID := tokenize($srchcoll, '/')[last()]
return
if ($srchlang = "all") then
concat("for $r in collection('",
$srchcoll,
"')//entry[",
$expr,
"]/senses/sense order by $r/../../",
local:order-expr($orderby),
" collation '?lang=sme-SE' return ",
"let $doc := util:document-name($r) return ",
"
Your document path: {$srchcoll}
Your $lang string: {$srchlang}
Your $class string: {$class}
Your session attributes: {$session}
Your query filter/old query:
{$queryOld}
Your document path: {$srchcoll}
Your $lang string: {$srchlang}
Your $class string: {$class}
Your session attributes: {$session}
Your query filter/old query:
{$queryOld}
Your complete search query:
{$query}
Your document path: {$srchcoll}
Your $lang string: {$srchlang}
Your $class string: {$class}
Your session attributes: {$session}
Your query filter/old query:
{$queryOld}
Your complete search query:
{$query}
{ round-half-to-even( seconds-from-duration( util:system-time()-$start) , 3)}
) }