[MarkLogic Dev General] Nested cts:search() functions over JSON documents

Geert Josten Geert.Josten at marklogic.com
Thu Mar 12 22:47:12 PDT 2015


Hi Nick,

I think you are looking cts:json-property-scope-query:

xquery version "1.0-ml";

let $json-doc := xdmp:unquote(
'{
"foo": {
"bar": "UP HERE"
},
"baz": {
"foo": {
"bar": "DOWN HERE"
}
}
}')

return xdmp:document-insert("/doc.json", $json-doc)
;
"element search",
cts:search(doc(), cts:element-query(xs:QName("foo"), cts:element-query(xs:QName("bar"), "here"))),
"json search",
cts:search(doc(), cts:json-property-scope-query("foo", cts:json-property-scope-query("bar", "here")))

Cheers,
Geert

From: Nick Ardlie <nick.ardlie at gmail.com<mailto:nick.ardlie at gmail.com>>
Reply-To: MarkLogic Developer Discussion <general at developer.marklogic.com<mailto:general at developer.marklogic.com>>
Date: Friday, March 13, 2015 at 6:34 AM
To: "general at developer.marklogic.com<mailto:general at developer.marklogic.com>" <general at developer.marklogic.com<mailto:general at developer.marklogic.com>>
Subject: [MarkLogic Dev General] Nested cts:search() functions over JSON documents

Is it possible to define nested (i.e. path constrained) queries in cts:search() over JSON document types in MarkLogic 8?

With XML document types I can nest cts:element-query() functions to achieve this.
But I don't seem to be able to use the cts:element-query() function for JSON.
Unless I'm missing something? It requires an xs:QName() parameter and a local-name only version of a QName doesn't seem to work for JSON documents.

To describe with an example, say I have the following documents (one XML, the other JSON):

let $xml-doc :=
<doc>
<foo>
<bar>UP HERE</bar>
</foo>
<baz>
<foo>
<bar>DOWN HERE</bar>
</foo>
</baz>
</doc>

let $json-doc := xdmp:unquote(
'{
"foo": {
"bar": "UP HERE"
},
"baz": {
"foo": {
"bar": "DOWN HERE"
}
}
}')

let $insert-result := (xdmp:document-insert("ID-1", $json-doc), xdmp:document-insert("ID-2", $xml-doc))


With the XML document I can search for foo/bar elements that contain the word "here" like this:
return cts:search(doc(), cts:element-query(xs:QName("foo"), cts:element-query(xs:QName("bar"), "here")))

After the above insert this will match the XML document but not the JSON document.
I'd like to do the equivalent with the JSON document but the similar cts functions for JSON don't appear to support cts:query nesting (e.g. cts:json-property-value-query()).
Is there a way to do this with JSON documents?
I know I can constrain the first expression argument to cts:search with an XPath.
But I'm interested to know if I can achieve this entirely within cts:query constructs/functions.

Regards,
Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://developer.marklogic.com/pipermail/general/attachments/20150313/cb4dbfac/attachment-0001.html 


More information about the General mailing list