You can try doing a search on collection(), then do a transformation on the
results highlighting what you want. Here is a code sample from the
cts:highlight chapter in the Developer's Guide (see page 129 in
http://developer.marklogic.com/pubs/4.0/books/dev_guide.pdf) that does
something like that (using the Shakespeare Plays XML content):
xquery version "1.0-ml";
declare function local:truncate($x as item()) as item()*
{
typeswitch ($x)
case element(HIGHLIGHTME) return $x/node()
case element(TITLE) return if ($x/../../PLAY) then $x else ()
default return for $z in $x/node() return local:truncate($z)
};
let $query := "to be or not to be"
for $x in cts:search(doc(), $query)
return
local:truncate(cts:highlight($x, $query,
<HIGHLIGHTME>{$cts:node/parent::element()}</HIGHLIGHTME>))
(:
returns:
<TITLE>The Tragedy of Hamlet, Prince of Denmark</TITLE>
<LINE>To be, or not to be: that is the question:</LINE>
:)
-Danny
From: [email protected]
[mailto:[email protected]] On Behalf Of Goh, Swee-Doon
(GPMS)
Sent: Wednesday, April 15, 2009 9:53 AM
To: [email protected]
Subject: [MarkLogic Dev General] Passing search term in XQuery
Hello,
I'm working on a JSP app to do search function for term in the entire
collection. I'm using the JSP Taglib.
I can search for term in specific element, however I want to search for all
elements and return just that element with the term highlighted. The
cts:search(collection()//(p|h1|h2), $query in the snippet below is not ideal
where all element name have to be specified. I tried using
cts:search(collection()//* but it return data including the parent elements.
Is there a recommended way to doing full-text search? I'm open to suggestion.
<% String term = request.getParameter("term"); %>
<h2>Result:</h2>
<xq:execute>
<xq:query>
declare namespace ns1="http://example/ns1";
declare namespace ns2="http://example/ns2";
declare variable $search_term external;
let $query := cts:or-query(tokenize($search_term, ' '))
for $p in
cts:search(collection()//(p|h1|h2|b|i|ns1:para|ns2:paragraph), $query)[1 to 30]
return
<div>
{ cts:highlight($p, $query, <font
color="yellow"><b>{$cts:text}</b></font>) }
<span> (found in: <b>{fn:base-uri($p)}</b></span>
</div>
</xq:query>
<xq:variable localname="search_term" type="xs:string"
value="${param.term}"/>
</xq:execute>
</div>
Thank you!
Swee-Doon Goh
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general