1
votes

I want to get a list of all collections in a database. I know how to do it using XQuery code:

cts:collections("*")  

But I am having trouble figuring out how to do this using the MarkLogic Java API.

Was reading up on StructuredQueryBuilder, but it seems too complex for what I am trying to achieve.

2
Small note: I recommend dropping that "*" argument. It is probably not doing what you expect. Use cts:collection-match if you want to filter collection names using a pattern.grtjn

2 Answers

1
votes

you can use ServerEvaluationCall to execute your XQuery.

ServerEvaluationCall call = databaseClient.newServerEval().xquery("cts:collections(\"*\")");
for (EvalResult result : call.eval()) {
   String collection = result.getString();
   System.out.println(collection);
}
0
votes

You can use QueryOptionsManager.writeOptions​() to persist query options that define a values lookup for the collection lexicon:

<options xmlns="http://marklogic.com/appservices/search">
  <values name="collections">
    <collection/>
  </values>
</options>

Then use a QueryManager.values() request to get the collections.

For more detail, see:

http://docs.marklogic.com/guide/java/query-options#id_83483

http://docs.marklogic.com/javadoc/client/com/marklogic/client/admin/QueryOptionsManager.html

http://docs.marklogic.com/guide/java/searches#id_83836

http://docs.marklogic.com/javadoc/client/com/marklogic/client/query/QueryManager.html#values-com.marklogic.client.query.ValuesDefinition-T-

As an alternative, for more flexibility and control, you can install a JavaScript or XQuery resource service extension or main module on the server and call the resource service extension or invoke the module.

You can use eval() to iterate quickly while developing the server side code. The only caveat is that eval() is discouraged in production because of the security concerns (especially when the operations require that the user have elevated privileges).

Hoping that's useful,