1

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.

1
  • 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. Commented Aug 15, 2019 at 7:03

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.