Search code examples
restmarklogic

Marklogic REST API search for latest document version


We need to restrict a MarkLogic search to the latest version of managed documents, using Marklogic's REST api. We're using MarkLogic 6.

Using straight xquery, you can use dls:documents-query() as an additional-query option (see Is there any way to restrict marklogic search on specific version of the document).

But the REST api requires XML, not arbitrary xquery. You can turn ordinary cts queries into XML easily enough (execute <some-element>{cts:word-query("hello world")}</some-element> in QConsole).

If I try that with dls:documents-query() I get this:

<cts:properties-query xmlns:cts="http://marklogic.com/cts">
    <cts:registered-query>
        <cts:id>17524193535823153377</cts:id>
    </cts:registered-query>
</cts:properties-query>

Apart from being less than totally transparent... how safe is that number? We'll need to put it in our query options, so it's not something we can regenerate every time we need it. I've looked on two different installations here and the the number's the same, but is it guaranteed to be the same, and will it ever change? On, for example, a MarkLogic upgrade?

Also, assuming the number is safe, will the registered-query always be there? The documentation says that registered queries may be cleared by the system at various times, but it's talking about user-defined registered queries, and I'm not sure how much of that applies to internal queries.

Is this even the right approach? If we can't do this we can always set up collections and restrict the search that way, but we'd rather use dls:documents-query if possible.


Solution

  • The call to dls:documents-query() makes sure the query is actually registered (on the fly if necessary), but that won't work from REST api. You could extend the REST api with a custom extension as suggested by Mike, but you could also use the following:

    cts:properties-query(
      cts:and-not-query(
        cts:element-value-query(
          xs:QName("dls:latest"),
          "true",
          (),
          0
        ),
        cts:element-query(
          xs:QName("dls:version-id"),
          cts:and-query(())
        )
      )
    )
    

    That is the query that is registered by dls:documents-query(). Might not be future proof though, so check at each upgrade. You can find the definition of the function in /Modules/MarkLogic/dls.xqy

    HTH!