简体   繁体   中英

MarkLogic 8 - XQuery - cts:search - Change database programmatically

How can I change the database that a cts:search function runs against programmatically?

Right now I'm in query console.

You will probably need to use xdmp:eval , which accepts an options argument, and in there you can specify the database:

xdmp:eval("cts:search(...)",  (),
   <options xmlns="xdmp:eval">
     <database>{xdmp:database("otherdb")}</database>
   </options>)

Although on the lowest level, xdmp:eval is really what happens, the most clean option which is probably easiest to write inline is xdmp:invoke-function - and even better may be to also use an anonymous function within it. This combination allows for natural use of existing variables. If you want to go further, then also look at xdmp:apply(to add more flexability)

Furthermore, in MarkLogic 8, there is a new transaction-type called update-auto-commit which also makes it nice and clean to invoke a function inline, while waiting for the results (no spawn) and have it in its own transaction. Used properly, then the results of an update/insert are even available in the calling code.

The code sample below applies cts:search against another database and naturally uses the variables in the main code:

xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";

let $query := cts:word-query("foo")
let $start := 1
let $end := 3
let $database-name := "your-other-database-name-here"

 return
    xdmp:invoke-function(
      function() { 
          cts:search(doc(), $query)[$start to $end]        
      },
      <options xmlns="xdmp:eval">
        <database>{xdmp:database($database-name)}</database>
      </options>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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