I want to get all documents where one path range index value is equal to another value in the same document (Example: /metadata/book-title = /metadata/chapter-title). Is there a way?
I want to get all documents where one path range index value is equal to another value in the same document (Example: /metadata/book-title = /metadata/chapter-title). Is there a way?
The obvious thought is to feed a cts:values result on one field into a so-called shotgun-OR query on the other field, but that won't make sure that both fields are equal within the document. You'd have to manually filter the false positives with XPath afterwards, which is not very optimal.
A better approach is to use Optic API. Something like this gives you an idea of how that would work:
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()
You can replace the element-references with other references, like path-references, if that matches your indexes.
HTH!
Yes - you pass the result of cts:values([reference to your path-range-index]) to a cts:range-query of the other.
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
)
Then use $match-query in your larger query.
Please note that this is a bare-bones example. In reality, you will likely tune both the full query and the cts:values() with query options and possibly a query scoping the results of cts:values().
Also note that there is a also a cts:path-range-query() rather than range-query which may be more convenient. However, I try to keep my code generic (not caring where the data in the index came from because that implementation detail could change over time).