简体   繁体   English

MarkLogic:绑定cts:将表达式搜索到变量

[英]MarkLogic: binding cts:search expression to a variable

In MarkLogic, is it possible to bind a cts:search expression to a variable and then use that variable elsewhere in the XQuery ? 在MarkLogic中,是否可以将cts:search表达式绑定到变量,然后在XQuery中的其他地方使用该变量?

I want to do something like this: 我想做这样的事情:

let $query := cts:search(doc(),
                               cts:and-query((
                                  cts:element-attribute-word-query(
                                    xs:QName("para"),
                                    xs:QName("role"),
                                      "intro") ,

                                  cts:element-attribute-word-query(
                                    xs:QName("title"),
                                    xs:QName("role"),
                                      "inline")
                                       ))
                                     )


let $number-of-results := xdmp:estimate($query)

return $number of results

But, I'm not sure how to pass the expression itself, rather than what it returns. 但是,我不确定如何传递表达本身,而不是它返回的内容。

Geert's answer is correct and practical: reuse the cts:query item, not the database access expression. Geert的答案是正确和实用的:重用cts:query项,而不是数据库访问表达式。 Use the cts:query to query the database as needed. 使用cts:query根据需要查询数据库。

But in some cases it can be useful to "pass the expression itself" as in the original question. 但在某些情况下,像原始问题一样“传递表达本身”会很有用。 That might seem difficult because XQuery 1.0 doesn't really allow for metaprogramming. 这似乎很难,因为XQuery 1.0并不真正允许元编程。 Variable bind to sequences of items, which are the result of evaluating expressions. 变量绑定到项目序列,这是评估表达式的结果。 Variables don't bind to unevaluated expressions. 变量不会绑定到未评估的表达式。

MarkLogic offers a way around this, using functions like http://docs.marklogic.com/xdmp:path or http://docs.marklogic.com/xdmp:value or http://docs.marklogic.com/xdmp:eval for generic expression evaluation. MarkLogic提供了一种方法,使用http://docs.marklogic.com/xdmp:pathhttp://docs.marklogic.com/xdmp:valuehttp://docs.marklogic.com/xdmp等函数用于通用表达式评估的eval In most cases it's better to bind the cts:query instead, as in Geert's answer. 在大多数情况下,最好绑定cts:query,就像在Geert的答案中一样。 But if you really need metaprogramming, you can build expressions as strings and evaluated them on demand. 但是如果你真的需要元编程,你可以将表达式构建为字符串并根据需要对它们进行评估。

let $query := 'cts:search(doc(),
                           cts:and-query((
                              cts:element-attribute-word-query(
                                xs:QName("para"),
                                xs:QName("role"),
                                  "intro") ,

                              cts:element-attribute-word-query(
                                xs:QName("title"),
                                xs:QName("role"),
                                  "inline")
                                   ))
                                 )'
return xdmp:value('xdmp:estimate('||$query||')')

XQuery 3 support in MarkLogic might help with this, but otherwise no. MarkLogic中的XQuery 3支持可能对此有所帮助,但是否则没有。 You can put the cts:query part in $query like this though: 您可以将cts:query部分放在$ query中,但是:

let $query := cts:and-query((
                              cts:element-attribute-word-query(
                                xs:QName("para"),
                                xs:QName("role"),
                                  "intro") ,

                              cts:element-attribute-word-query(
                                xs:QName("title"),
                                xs:QName("role"),
                                  "inline")
                                   ))

let $number-of-results := xdmp:estimate(cts:search(doc(), $query))
let $results := cts:search(doc(), $query)

return $number-of-results

HTH! HTH!

In the general case you can do exactly what you want, it's just that xdmp:estimate() is a special case that requires an inline searchable expression and not a variable to a searchable expression. 在一般情况下,您可以完全按照自己的意愿执行操作,只是xdmp:estimate()是一种特殊情况,需要内联可搜索表达式而不是可搜索表达式的变量。 That's why xdmp:estimate(cts:search(doc(), $query)) works. 这就是为什么xdmp:estimate(cts:search(doc(), $query))有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM