How to get total number of documents in Marklogic optic api search?
Note: the relationship between documents and results depends on how you created your TDE template. Some templates may actually create many rows from a single document. I am going to assume that your title related to documents should read "result" instead of documents since you reference "results" in the actual question.
For what you are trying to do, I would use op.groupBy()
Free standing example:
const op = require('/MarkLogic/optic');
op.fromLiterals([
{row:1, val:2},
{row:1, val:4},
{row:2, val:3},
{row:2, val:5},
{row:2, val:7}
])
.groupBy([],op.count('count', 'row')).result()
Result: {"count":5}
The reason for the empty array in the groupBy()
statement is to say group by everything (by not passing a column to group by)
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();