marklogicmarklogic-8marklogic-9marklogic-optic-api

How to get total number of documents in Marklogic optic api search?


how to count or estimate numbers of results when using marklogic optic api.

var results = op.fromView('x', 'y')
.where() <- I need to count all results from view after where.
.limit(200)
.result()
.toArray();

I would be nice to achieve something like this in response:

var count = cts.estimate(query); 
var items = fn.subsequence(search, 1, 20).toObject(); 
{ items: items; count: count } 

Solution

  • I'd like to build on David Ennis' answer, as you're looking to get the total and the results. These are two separate things you'll need to do, but it looks like this:

    let myView = 
      op.fromView('x', 'y')
        .where(conditions);
    
    let total = fn.head(myView.groupBy(null, op.count('total')).result()).total;
    
    let results = 
      myView
        .limit(200)
        .result()
        .toArray();