[英]MarkLogic: convert string to path expression in cts:search
How can I create a function in MarkLogic that takes an XPath as a string and then passes it to the cts:search
function? 如何在MarkLogic中创建一个函数,将XPath作为字符串,然后将其传递给
cts:search
函数?
I want to do something like this: 我想做这样的事情:
xquery version "1.0-ml";
declare namespace local = "http://www.local.com/" ;
declare function local:xpath-search($xpath as xs:string, $collection as xs:string, $limit as xs:string) {
let $valid := cts:valid-index-path($xpath,fn:false())
let $results := cts:search(xdmp:value($xpath), cts:and-query(()) ) [position() < 100]
return
if ($valid = false()) then "xpath is invalid"
else (
if ($results = '') then "no results were found"
else $results
)
} ;
local:xpath-search('//p', '', '')
But, I'm getting the "expression is unsearchable" error. 但是,我得到了“表达是不可搜索的”错误。
Use http://docs.marklogic.com/search:search and supply it with the <searchable-expression>
option. 使用http://docs.marklogic.com/search:search并使用
<searchable-expression>
选项提供它。
The search:search
function already implements what you want. search:search
功能已经实现了您想要的功能。 Under the hood it calls cts:search
and supplies the searchable-expression using xdmp:value
. 在引擎盖下它调用
cts:search
并使用xdmp:value
提供可搜索表达式。 You could do that to, but search:search
has already been written and tested. 你可以这样做,但
search:search
已经编写和测试。
In passing, using path-based searchable expressions with cts:search
is something of a trap for the unwary. 顺便说一句,使用基于路径的可搜索表达式与
cts:search
是一个陷阱,对于粗心大意。 In most cases it is better to use collection()
as the first argument to cts:search
, matching the entire database. 在大多数情况下,最好使用
collection()
作为cts:search
的第一个参数cts:search
,匹配整个数据库。 Then use a cts:query
for the second parameter, to match the documents you are interested in. 然后使用
cts:query
第二个参数,以匹配您感兴趣的文档。
But what about //p
? 但是
//p
怎么样? It's important to understand that MarkLogic indexes fragments , not elements. 重要的是要了解MarkLogic索引片段而不是元素。 By default, fragments are documents.
默认情况下,片段是文档。 You can change that: you can even fragment at the
//p
level. 您可以更改:您甚至可以在
//p
级别进行分段。 But in most cases it's a bad idea. 但在大多数情况下,这是一个坏主意。 You're probably better off using
cts:search
to match documents, and cts:highlight
to find matches in paragraphs. 你可能最好使用
cts:search
匹配文档,然后cts:highlight
以查找段落中的匹配项。 The search:search
function supports that, too. search:search
功能也支持它。
Cascavel: 卡斯卡韦尔:
You could try expressing the entire cts:search() as a string (concatenating the static parts with the path) and invoking it with xdmp:value or xdmp:eval. 您可以尝试将整个cts:search()表示为字符串(将静态部分与路径连接)并使用xdmp:value或xdmp:eval调用它。
Hoping that helps, 希望有所帮助,
Erik Hennum Erik Hennum
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.