xquerymarklogiccts-search

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?

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.


Solution

  • XQuery 3 support in MarkLogic might help with this, but otherwise no. You can put the cts:query part in $query like this though:

    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!