1

I have been using cts:search with searchable-expression to extract specific elements from XML documents. For example:

Document:

<book>
  <recipe>
    <ingredients>cinnamon, peppermint</ingredients>
    <instruction/>
  </recipe>
  <recipe>
    <ingredients>sugar, peppermint</ingredients>
    <instruction/>
  </recipe>
  <recipe>
    <ingredients>coconut oil</ingredients>
    <instruction/>
  </recipe>
</book>

Query:

cts:search(//recipe, cts:parse("peppermint"))

Results:

<recipe>
  <ingredients>cinnamon, peppermint</ingredients>
  <instruction>
  </instruction>
</recipe>
<recipe>
  <ingredients>sugar, peppermint</ingredients>
  <instruction>
  </instruction>
</recipe>

However, this doesn't work with cts:not-query and no result is returned for the following search:

cts:search(//recipe, cts:parse("-cinnamon"))

Looking into the query plan, my understanding is that the cts:not-query is applied at the fragment (document) level prior to filtering so the above document is eliminated outright. If that is correct, this behavior makes sense.

Nevertheless I am still wondering if this can be accomplished somehow. Thanks!

Fan Li
  • 1,057
  • 7
  • 11

1 Answers1

2

Searchable expressions that don't match root nodes are discouraged.

One problem with more complex searchable expressions is that a small change can make the search require filtering.

That's one reason why the SJS version of search - cts.search() - doesn't support searchable expressions.

The recommended approach is

  • To model the documents with a separate document for each object / row (recipe in this case) -- which eliminates the potential for false positives in unfiltered search that match parts of the criteria in two different objects.
  • To use the cts:query argument for matching and to use XPaths on the returned documents for extracting nodes.
  • To assemble objects into lists as needed based on queries.

Hoping that helps,

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
ehennum
  • 7,295
  • 13
  • 9
  • I guess what's special for cts:not-query is that false-positive becomes false-negative. Hence even filtered search won't work. Do you think if it is the case? – Fan Li Jan 07 '21 at 21:12
  • 1
    @FanLi -- yes, the not-query prevents the document from getting past the index resolution stage, so filtering won't help. – Dave Cassel Jan 08 '21 at 13:53