有没有一种方法可以搜索一个路径范围索引等于另一个的文档?

Is there a way to search for documents where one path range index is equal to another?

我想获取同一文档中一个路径范围索引值等于另一个值的所有文档 (示例:/metadata/book-title = /metadata/chapter-title)。有办法吗?

是 - 你传递了另一个 cts:values([reference to your path-range-index]) to a cts:range-query 的结果。

let $title-list := cts:values(cts:path-reference("/metadata/book-title"))
let $match-query := cts:range-query(
  cts:path-reference("/metadata/chapter-title"),
  "=",
  $title-list
)

然后在更大的查询中使用 $match-query。

请注意,这是一个简单的例子。实际上,您可能会同时调整完整查询和使用查询选项的 cts:values() 以及可能限定 cts:values() 结果范围的查询。

另请注意,还有一个 cts:path-range-query() 而不是 range-query 可能更方便。但是,我尝试使我的代码保持通用(不关心索引中的数据来自何处,因为该实现细节可能会随着时间而改变)。

显而易见的想法是将一个字段的 cts:values 结果提供给另一个字段的所谓的 shotgun-OR 查询,但这不能确保文档中的两个字段相等.之后您必须使用 XPath 手动过滤误报,这不是最佳选择。

更好的方法是使用 Optic API。像这样的东西让你知道它是如何工作的:

import module namespace op="http://marklogic.com/optic"
     at "/MarkLogic/optic.xqy";

op:from-lexicons(
  map:new((
    map:entry("bookTitle", cts:element-reference(xs:QName("book-title"))),
    map:entry("chapterTitle", cts:element-reference(xs:QName("chapter-title"))),
    map:entry("uri", cts:uri-reference())
  )),
  "books"
)
 => op:where(
      op:eq(
        op:view-col("books", "bookTitle"),
        op:view-col("books", "chapterTitle")
      )
    )
 => op:result() 

如果与您的索引匹配,您可以将元素引用替换为其他引用,例如路径引用。

HTH!