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>