[英]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这一定是真的
And EITHER of these must be true:其中一个必须是真的:
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
字段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
例如,有人可能传入10
的authorId
和2017-01-01T00:00:00
的dateTime
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.