You need a serialized version of a cts:query
. This is required when using <additional-query>, as well as for the parse function of custom constraints.
Applies to MarkLogic versions 7+
<root>{cts:collection-query("published")}</root>/node()
When configuring search options for the REST API or the Search API, it’s often useful to have some search criteria added to every search. For instance, you might want to add a collection query to ensure that only published documents are returned. This can be done using the <addition-query/>
option of the search configuration. Writing the search criteria is pretty straightforward:
cts:collection-query("published")
However, the APIs don’t expect to find XQuery code in the XML configuration and they won’t interpret it. They require that the query be serialized XML. That means that we need to produce something that looks like this:
<cts:collection-query xmlns:cts="https://marklogic.com/cts"> <cts:uri>published</cts:uri> </cts:collection-query>
It’s not obvious how to get from the first form to the latter, but this recipe shows how. In Query Console, wrap the query code in an element with {}
braces, then add a /node()
after the element. The output is the serialized XML that you can then copy to your search options.
Notice that this recipe does not give you dynamic search options; rather, it provides a method to generate the static content that you need. To generate a dynamically-constructed additional-query
, you would need to build the options at run time.
By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.