使用 cts 查询检索与给定文档 uri 关联的集合- Marklogic

Using cts query to retrieve collections associated with the given document uri- Marklogic

我需要在 Marklogic 中检索给定文档所属的集合。 我知道 xdmp 命令可以做到这一点。但是我需要在cts查询中使用它来检索数据,然后从中过滤记录。

xdmp:document-get-collections("uri of document") can't be run inside cts-query to give appropriate data.

知道如何使用 cts 查询来完成吗?

谢谢

我想到了几个选项:

选项一:使用cts:values()

cts:values(cts:collection-reference())

如果您查看文档,您会发现您还可以通过将查询作为参数之一传递来将其限制为某些片段。

**更新:[11-10-2017] 附加到此的评论要求将 cts:values() 的结果限制为单个文档的示例(出于实际目的,我会说 fragment == document)

cts:values 的文档对此进行了解释。它是第 4 个参数 - 限制结果的查询。了解此模式,因为它是 MarkLogic 的许多功能的一部分。它是你的朋友。我将用于此问题陈述的查询是 cts:document-query();

一个例子:

cts:values(
  cts:collection-reference(), 
  (), 
  (), 
  cts:document-query('/path/to/my/document')
)

完整示例:

cts:search(
    collection(),
    cts:collection-query(
        cts:values(
          cts:collection-reference(), 
          (), 
          (), 
          cts:document-query('/path/to/my/document')
       )
    )
)[1 to 10]

选项二:使用cts:collection-match()
需要更多地控制 returning 文档中的一些集合,然后使用 cts:colection-match()。与第一个选项一样,您可以将结果限制为仅某些片段。但是,它的好处是可以选择模式。


注意:
它们都是 return 一个序列 - 非常适合输入查询的其他部分。然而,在幕后,我相信它们的工作方式不同。第二个选项是 运行 对词典。唯一集合名称列表越大,模式匹配越复杂,解析时间就越长。我在项目中使用集合匹配。但是,当我可以通过将结果限制为较少数量的文档来限制可能的选择时,我通常会使用它。

您在寻找 cts:collection-query() 吗?

您不能一步完成。您必须先 运行 编码才能检索与文档关联的集合。您可以为此使用 xdmp:document-get-collections 之类的东西。然后,您必须将其输入到动态构建的 cts 查询中:

let $doc-collections := xdmp:document-get-collections($doc-uri)
return
  cts:search(collection(), cts:collection-query($doc-collections))[1 to 10]

HTH!

将两个 XML 文件插入同一个 collection:

xquery version "1.0-ml";
xdmp:document-insert("/a.xml", <root><sub1><a>aaa</a></sub1></root>,
map:map() => map:with("collections", ("coll1")));

xdmp:document-insert("/b.xml", <root><sub2><a>aaa</a></sub2></root>,
map:map() => map:with("collections", ("coll1")));

搜索 collection:

xquery version "1.0-ml";
let $myColl:= xdmp:document-get-collections("/a.xml")
return 
    cts:search(/root,
    cts:and-query((cts:collection-query($myColl),cts:element-query(xs:QName("a"),"aaa")
)))