[XQZone General] returning snippets centered on matches...
Shannon Scott Shiflett
sss4r at virginia.edu
Tue Oct 4 14:16:06 PDT 2005
Travis, good question, good timing. I wish I could help, but writing
code using cts:highlight() to return "highlighted hits in context" is
something I'll be tasked to do in the very near future, so I'm eager
to read the responses to this thread....
On Oct 4, 2005, at 5:06 PM, Travis Raybold wrote:
> im searching a data node, and want to return just a subset of the
> text, centered on the matching string. i've modified some code
> kelly sent me, and it works fairly well, but a search that used to
> take a fraction of a second now takes 5 seconds. has anyone coded a
> function to return snippets instead of the whole text that are
> reasonably efficient? i could code this in php, but it seems a
> natural place to use marklogic, if i can just figure out how to
> make it efficient.
>
> thanks in advance for any help, the sample code follows
>
> --travis
>
> ----------------------------------------------------------------------
> ------------------------------
>
> let $string := "totalitarian"
>
> let $query := cts:element-word-query(xs:QName("data"),$string)
>
> return
>
> for $class in (cts:search(//class,$query))
>
> return
>
> <result>
>
> {
>
> let $scope := 5
>
>
> let $hit := cts:highlight($class,$query,concat
> ("<TRAVISRAYBOLDCONSTANT_START>",
> $cts:text,"<TRAVISRAYBOLDCONSTANT_END>"))
>
>
> let $hit-text := cts:highlight($class,$query,<span
> style="color:red;font-weight:bold">{$cts:text}</span>)
>
>
> let $before := tokenize(substring-before
> ($hit,"<TRAVISRAYBOLDCONSTANT_END>")," ")
>
> let $after := tokenize(substring-after
> ($hit,"<TRAVISRAYBOLDCONSTANT_START>")," ")
>
>
> let $before-count := count($before)
>
> let $before-snippet := $before[($before-count - $scope) to $before-
> count]
>
> let $after-snippet := $after[1 to $scope]
>
>
> let $snippet := <snippet>. . . . {string-join(($before-snippet,$hit-
> text//this-hit/text(),$after-snippet)," ")} . . . . </snippet>
>
>
> let $hilight-snippet :=
>
> cts:highlight($snippet,$string,<span style="color:red;font-
> weight:bold">{$cts:text}</span>)
>
>
> return $hilight-snippet
>
> }
>
> </result>
>
>
> _______________________________________________
> General mailing list
> General at xqzone.marklogic.com
> http://xqzone.com/mailman/listinfo/general
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4196 bytes
Desc: not available
Url : http://xqzone.marklogic.com/pipermail/general/attachments/20051004/f48b176b/smime.bin
More information about the General
mailing list