Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

marklogic - How to combine cts:search searchable-expression and cts:not-query?

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...