I have the impression that the XQuery and the Server-side JavaScript APIs in MarkLogic are largely equivalent. But there seems to be a big difference in cts:search
vs cts.search
. In cts:search
, I am able to specify an element to be searched and returned. For example, I can retrieve all recipes using cinnaomon as ingredient from a recipe book:
cts:search(//recipe, cts:element-word-query(xs:QName('ingredients'), 'cinnamon'))
Whereas cts.search
doesn't accept a path expression and will return the whole recipe book document:
cts.search(cts.elementWordQuery(xs.QName('ingredients'), 'cinnamon'))
The same question has been asked in MarkLogic mailing list but I don't see an answer there: https://developer.marklogic.com/pipermail/general/2015-March/016508.html
Below is a minimal example:
<book>
<recipe>
<ingredients>cinnamon, peppermint</ingredients>
<instruction/>
</recipe>
<recipe>
<ingredients>sugar, peppermint</ingredients>
<instruction/>
</recipe>
<recipe>
<ingredients>coconut oil</ingredients>
<instruction/>
</recipe>
</book>
The xquery would be:
cts:search(//recipe, cts:element-word-query(xs:QName('ingredients'), 'cinnamon'))
and the response:
<recipe>
<ingredients>cinnamon, peppermint</ingredients>
<instruction></instruction>
</recipe>
There are technical reasons why this is so. The cts:search
function in XQuery is actually not a function but a special form that has a function syntax. What that means is that the first argument doesn't actually get evaluated and then passed in to the function (if you think about it, that would be a very inefficient way to proceed!). In Javascript, the cts.search
function is a real function. To avoid the inefficiency, we dropped the first parameter, so you need to pull the part you care about off the result.
If you want to constrain the set of results to those that are within the element recipe
, wrap your query with a cts:element-query(xs:QName("recipe"), $your-query)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With