1

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

这必须是真的

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

其中任何一个都必须是真的:

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

例如,有人可能会传入一个authorIdof10和一个dateTimeof2017-01-01T00:00:00

下面是解释为什么应该/不应该返回它们的示例(删除了无关的 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...是否正确使用诸如搜索特定字段之类的东西

4

1 回答 1

2

包装两个查询以查找date-added元素的存在或newer-document-date值小于$date-froma 内部的值cts:or-query(),并将 a 内部的值cts:and-query()与值一起应用: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")
    ))
  ))
)
于 2021-07-30T00:53:36.413 回答