简体   繁体   English

将多个 MarkLogic 查询与 cts:search 结合使用

[英]Combining multiple MarkLogic queries with cts:search

I am trying to query a database of documents in MarkLogic to find any that fit several parameters:我正在尝试在 MarkLogic 中查询文档数据库以找到适合多个参数的任何文档:

This MUST be true这一定是真的

  1. A list of authors must contain an author ID provided作者列表必须包含提供的作者 ID

And EITHER of these must be true:其中一个必须是真的:

  1. A list of extra-documents must have a date-added field on any of them that is more recent than the provided date extra-documents列表必须在其中任何一个比提供的日期更新extra-documents date-added字段
  2. The document itself must have a newer-document-date field that is more recent than the provided date文档本身必须具有比提供的日期更近的newer-document-date字段

For example, someone may pass in an authorId of 10 , and a dateTime of 2017-01-01T00:00:00例如,有人可能传入10authorId2017-01-01T00:00:00dateTime

Below are samples explaining why they should/shouldn't be returned (removed extraneous XML)下面是解释为什么应该/不应该返回它们的示例(删除了无关的 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>

I am new to using cts:search , and am having trouble figuring out how to build a complex query like this that can search specific nodes.cts:search开始使用cts:search ,并且无法弄清楚如何构建这样一个可以搜索特定节点的复杂查询。 The best I have come up with is this:我想出的最好的是:

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)
            ))
        )
    )

However, this doesn't seem to work correctly, and trying to add the 3rd requirement has been extremely difficult, to the point that I am simply trying to get these first 2 implemented right now.但是,这似乎无法正常工作,并且尝试添加第三个要求非常困难,以至于我现在只是想立即实施前两个要求。 Any help on this is appreciated.对此的任何帮助表示赞赏。 I'm unsure if cts:search is the best approach for this, or if I am using things like /*:metadata... correctly for searching specific fields我不确定cts:search是否是最好的方法,或者我是否正确使用/*:metadata...类的东西来搜索特定字段

Wrap the two queries looking for either the existence of the date-added element or the newer-document-date with a value less than the $date-from inside of a cts:or-query() , and apply that inside of a cts:and-query() along with the cts:element-value-query() for the $author-id values:包装两个查询以查找date-added元素或newer-document-date ,其值小于cts:or-query()$date-from ,并将其应用到cts:and-query()以及$author-id值的cts:element-value-query()

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")
    ))
  ))
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM