Ambuj Jauhari
Ambuj Jauhari

Reputation: 1289

How to fetch all documents that match a URI pattern in Marklogic

I am starting to learn the java api for marklogic, What is want to know is how can i get all the documents that conform to a particular URI pattern.

For e.g. i want to get all the documents from the ML db where URI pattern is "/Downloads/Current/com.crc.eng.dollar/*"

Upvotes: 4

Views: 1103

Answers (2)

ehennum
ehennum

Reputation: 7335

You can query for documents within a directory. Something along the following lines should work:

DatabaseClient         client    = DatabaseClientFactory.newClient(...);
GenericDocumentManager docMgr    = client.newDocumentManager()
QueryManager           queryMgr  = client.newQueryManager();
StructuredQueryBuilder queryBldr = new StructuredQueryBuilder();

for (int pageNo=1; pageNo < YOUR_MAXIMUM_BEFORE_STOPPING; pageNo++) {
    SearchHandle resultsHandle = queryMgr.search(
        queryBldr.directory(true, "/Downloads/Current/com.crc.eng.dollar/"),
        new SearchHandle(),
        pageNo
        );

    MatchDocumentSummary[] docSummaries = resultsHandle.getMatchResults();
    for (MatchDocumentSummary docSummary: docSummaries) {
        InputStreamHandle docHandle = docMgr.read(
            docSummary.getUri(), new InputStreamHandle()
            );
        // ... do something with the document ...
    }

    if (docSummaries.length < queryMgr.getPageLength()) {
        break;
    }
}

To make the query more efficient, persist query options with a snippet transform set to empty and identify the query options when creating the query builder.

If all of the documents are JSON or XML, you can use a more specific document manager.

By the way, in MarkLogic 8, the query will be able to return a page of documents directly.

For more information:

http://docs.marklogic.com/javadoc/client/index.html http://docs.marklogic.com/guide/java

Upvotes: 3

John Smith
John Smith

Reputation: 1579

I'm using the XQuery API instead of the Java one, but maybe you can try using cts:uri-match through XCC (see: http://docs.marklogic.com/cts:uri-match)

Upvotes: 0

Related Questions