Hi Mindie, As David said, the function you are looking for is cts:highlight. It looks like you are trying to create a search snippet, sort of like on a google search page. There are lots of ways to do this, but generally, it involves running each search result through cts:highlight to find the matches, and then transforming the highlighted results how you want it. When you transform the document, you need to look in front of and behind each match and then grab the n-words before and after the match. An example of this is in the Shakespeare demo application in the search-lib.xqy file. Look for the truncateText function:
http://xqzone.marklogic.com/svn/bill/trunk/search-lib.xqy lib-search also has a similar function (truncate-text): http://xqzone.marklogic.com/svn/lib-search/trunk/release/lib-search.xqy Here is a simpler program (against the Shakespeare database) that shows the design pattern of highlighting and transforming (but not snippetting): xquery version "0.9-ml" define function 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 truncate($z) } let $query := "to be or not to be" for $x in cts:search(doc(), $query) return 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> :) Hope that helps, -Danny -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of David Sewell Sent: Wednesday, August 20, 2008 7:48 PM To: General Mark Logic Developer Discussion Subject: Re: [MarkLogic Dev General] limiting words in search results cts:highlight() will do the highlighting around your search phrase, but in order to create a partial context around the first match in the paragraph it's necessary to write a fairly complex ad hoc function, probably relying on fn:tokenize() to establish the context and then fn:string-join() to recombine the selected context into something that can be passed to cts:highlight(). We wrote some code to do just that for a publication, and it works well, but it's a bit messy and could no doubt be optimized and made more general-purpose. I'd hesitate to post it to the whole list in its current state--maybe someone out there has an elegant general-purpose function to do this task--but I can share it offline if you like. David S. On Wed, 20 Aug 2008, Mindie Sorenson wrote: > > I’m trying to limit the number of words that are displayed for each > search result. For example, I need to have the results formatted like this: > > > > ... we have the opportunity and also the responsibility to help > restore faith in the family. ... > > > > I have been able to get the first paragraph from each document that > contains the search phrase (responsibility), but I haven’t been able > to successfully limit the words to 7 words before the search phrase > and 7 after the phrase. I have looked through the cts:search > functions, but I haven’t been able to find quite what I’m looking for. Is > there a function that will help me do this? > > > > Thanks > > Mindie > > ______________________________________________________________________ > __________________________ > NOTICE: This email message is for the sole use of the intended > recipient(s) and may contain confidential and privileged information. > Any unauthorized review, use, disclosure or distribution is > prohibited. If you are not the intended recipient, please contact the sender > by reply email and destroy all copies of the original message. > > -- David Sewell, Editorial and Technical Manager ROTUNDA, The University of Virginia Press PO Box 801079, Charlottesville, VA 22904-4318 USA Courier: 310 Old Ivy Way, Suite 302, Charlottesville VA 22903 Email: [EMAIL PROTECTED] Tel: +1 434 924 9973 Web: http://rotunda.upress.virginia.edu/ _______________________________________________ General mailing list [email protected] http://xqzone.com/mailman/listinfo/general
