1

How to use variable in cts:search as first param.

let $doc-type := 'Employee'
let $query := some cts query
return
    cts:search($doc-type,$query)
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
anuj_gupta
  • 131
  • 4
  • Is your intent for `$doc-type` to be the name of a collection or a path expression (in this example for `/Employee` or `//Employee`)? – Mads Hansen Apr 10 '23 at 11:40

1 Answers1

2

The first parameter is an expression, typically an XPath but can also be a reference to a collection or fn:doc().

If your intent was to search for documents in the Employee collection, then you could use this:

let $doc-type := 'Employee'
let $query := cts:true-query()
return
    cts:search(collection($doc-type), $query)

If your intent was to provide a dynamic XPath expression from a string to apply to that first param as a path expression, that is not as easy.

You may need to generate a stringified search expression with that path expression and then use xdmp:eval():

let $doc-type := '/Employee'
let $query := cts:true-query()
return
  xdmp:eval("cts:search("||$doc-type||", "||$query||")") 

Using xdmp:eval() is generally not advised, due to security and performance concerns.

If you were looking for documents that have Employee as the root element of the document (/Employee), then you could use one of the new (in MarkLogic 11) cts document query functions cts:document-root-query() and apply it to the query instead of as a path expression:

let $doc-type := 'Employee'
let $query := cts:true-query()
return
    cts:search(doc(), cts:and-query(( cts:document-root-query(xs:QName($doc-type)), $query)) )
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147