Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 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 Reply

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,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...