Problem

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.

Solution

Applies to MarkLogic versions 7+

<root>{cts:collection-query("published")}</root>/node()

Discussion

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.

Learn More

Search Datasheet

Learn how MarkLogic uses an “Ask Anything” Universal Index to index data immediately when it loads so you can immediately begin asking questions of your data.

Query Options Reference

This appendix is a reference guide to the query options used for search and lexicon analysis by the XQuery Search API and the MarkLogic Client APIs (REST, Java, Node.js).

Creating a Custom Constraint

Learn how to specify your function implementations in the options XML to create a custom constraint that’s not pre-defined in the Search API.

This website uses cookies.

By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.