MarkLogic 8 - XQuery - cts: search - programmatically modify the database

How can I change the database that the cts:search function works with software?

I am now in the query console.

+5
source share
2 answers

You probably need to use xdmp:eval , which takes a parameter argument, and there you can specify the database:

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

Although at the lowest level, xdmp: eval is really what happens, the cleanest option that is probably the easiest to write inline is xdmp: invoke-function - and it might even be better to use an anonymous function inside it. This combination allows the natural use of existing variables. If you want to go further, then look at xdmp: apply (to add extra flexibility)

In addition, in MarkLogic 8, there is a new type of transaction called update-auto-commit, which also makes it nice and clean to call the inline function, waiting for the results (without appearing) and have it in its own transaction. Used correctly, then the update / insert results are available even in the calling code.

The code example below uses cts: search for another database and naturally uses 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>) 
+2
source

Source: https://habr.com/ru/post/1240455/


All Articles