2

I want to run below query using the MarkLogic Java API

cts:search(fn:doc(), cts:and-query((cts:collection-query("/abc/xyz"), 
cts:collection-query("/abc/xyz/pqr"))))

2 Answers 2

6

Use a StructuredQueryBuilder, the and() and collection() methods to construct the equivalent structured query to search for documents that are in both the collections.

// create the client -- this will change slightly in Java Client API 4.x
DatabaseClient client = 
    DatabaseClientFactory.newClient(host, port, user, password, authType);
// create a manager for searching
QueryManager queryMgr = client.newQueryManager();

// create a query builder
StructuredQueryBuilder qb = new StructuredQueryBuilder();
// build a search definition
StructuredQueryDefinition query = 
    qb.and(
      qb.collection("/abc/xyz"), 
      qb.collection("/abc/xyz/pqr"));

// run the search
queryMgr.search(query, resultsHandle);
Sign up to request clarification or add additional context in comments.

Comments

2

You need to use a structured query, rather than a cts:query. They're very similar in expressiveness. The Java Client API includes a builder class for structured query, com.marklogic.client.query.StructuredQueryBuilder.

For details, see the following:

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.