Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count documents in a database directory

Tags:

marklogic

Say I have 50 records in directory /example in a MarkLogic database. Is there any way to find size of directory? In this case it should give me 50. I need it in search API.

Thanks in advance

like image 457
Y.Prithvi Avatar asked Dec 15 '22 17:12

Y.Prithvi


2 Answers

xdmp:estimate(cts:search(fn:doc(), cts:directory-query("/example/", "infinity")))

Use xdmp:estimate over fn:count to get a fast response.

like image 128
Dave Cassel Avatar answered Feb 08 '23 09:02

Dave Cassel


with the java api you could do a search for a given directory and then get the total nr of hits.

    DatabaseClient client = DatabaseClientFactory.newClient(host,port, "admin", "****", Authentication.DIGEST);
    QueryManager queryMgr = client.newQueryManager();
    StructuredQueryBuilder qb = new StructuredQueryBuilder();
    StructuredQueryDefinition querydef = 
            qb.directory(true, "/content/enhanced-rosetta/");
    SearchHandle resultsHandle = queryMgr.search(querydef,new SearchHandle());
    MatchDocumentSummary[] results = resultsHandle.getMatchResults();
    System.out.println("Total count=" + resultsHandle.getTotalResults());
    client.release();
like image 31
prker Avatar answered Feb 08 '23 10:02

prker