Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path expression in MarkLogic cts.search

Tags:

marklogic

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>
like image 400
Fan Li Avatar asked Dec 18 '22 18:12

Fan Li


1 Answers

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)

like image 150
mholstege Avatar answered Feb 08 '23 08:02

mholstege