将多个 MarkLogic 查询与 cts:search 组合

Combining multiple MarkLogic queries with cts:search

我正在尝试查询 MarkLogic 中的文档数据库以查找适合多个参数的文档:

这一定是真的

  1. 作者列表必须包含提供的作者 ID

并且以下任何一个必须为真:

  1. extra-documents 的列表中的任何一个 date-added 字段必须比提供的日期
  2. 更新
  3. 文档本身必须有一个 newer-document-date 字段,该字段比提供的日期
  4. 更新

例如,某人可能会传入 10authorId2017-01-01T00:00:00

dateTime

以下示例解释了为什么它们 should/shouldn 没有被退回(删除了无关的 XML)

This document should be returned because it has an extra-document with a date-added that is more recent than the provided date, and a matching author.
<metadata>
<extra-documents>
   <extra-document>
      <date-added>2018-09-11T00:00:00</date-added>
   </extra-document>
</extra-documents>
<book-authors>
   <book-author>10</book-author>
   <book-author>20</book-author>
</book-family-authors>
</metadata>

This document should be returned because it has a newer-document-date greater than the provided date
<metadata>
<extra-documents>
   <extra-document>
      <date-added>2000-09-11T00:00:00</date-added>
   </extra-document>
</extra-documents>
<book-authors>
   <book-author>10</book-author>
   <book-author>20</book-author>
</book-family-authors>
<newer-document-date>2019-02-03T00:00:00</newer-document-date>
</metadata>


This document should NOT be returned because it is missing the author, even though it fills the date requirement on both the extra-document and the newer-document-date
<metadata>
<extra-documents>
   <extra-document>
      <date-added>2020-09-05T00:00:00</date-added>
   </extra-document>
</extra-documents>
<book-authors>
   <book-author>20</book-author>
</book-family-authors>
<newer-document-date>2019-02-03T00:00:00</newer-document-date>
</metadata>

我是 cts:search 的新手,我很难弄清楚如何构建这样一个可以搜索特定节点的复杂查询。我想到的最好的是:

cts:search(/*, 
        cts:and-query (( 
            cts:search(/*:metadata/*:book-authors/*:book-author, $author-id),
            cts:element-query(
                    fn:QName("http://example.com/collection/book-metadata", "newer-document-date"), 
                    cts:true-query()
            ), <-- this element-query was an attempt to make sure the field exists on the book, as some books don't have this field
            cts:search(/*:metadata,
                cts:element-range-query(fn:QName("http://example.com/collection/book-metadata", "newer-document-date"), "<", $date-from)
            ))
        )
    )

但是,这似乎无法正常工作,并且尝试添加第三个要求非常困难,以至于我现在只是想立即实现前两个要求。对此的任何帮助表示赞赏。我不确定 cts:search 是否是最好的方法,或者我是否正确使用 /*:metadata... 之类的东西来搜索特定字段

包装两个查询,查找 date-added 元素或 newer-document-date 的值小于 cts:or-query(), and apply that inside of a cts:and-query() 内部的 $date-from 以及cts:element-value-query() 对应 $author-id 值:

declare namespace meta = "http://example.com/collection/book-metadata";
cts:search(doc(),
  cts:and-query((
    cts:element-value-query(xs:QName("meta:book-author"), $author-id),
    cts:or-query((
      cts:element-query(xs:QName("meta:date-added"), cts:true-query()),
      cts:element-range-query(xs:QName("meta:newer-document-date"), "<", "$date-from")
    ))
  ))
)