Count query results

You can use the matters.count method to count the messages from a Gmail or Groups query before you create an export. With this information, you can refine your query filters to return more or less results.

To work with Vault resources, the account must have the required Vault privileges and access to the matter. To access a matter, the account must have created the matter, have the matter shared with them, or have the View All Matters privilege.

The following example shows how to count the results returned by a query for messages that meet the following criteria:

  • messages owned by accounts email1 and email2.
  • excludes draft messages.
  • messages sent to


public Long count(Vault client, String matterId) {
  AccountInfo emailsToSearch = new AccountInfo().setEmails(ImmutableList.of("email1", "email2"));
  MailOptions mailQueryOptions = new MailOptions().setExcludeDrafts(true);
  String queryTerms = "";
  Query query =
    new Query()
  CountArtifactsRequest request = new CountArtifactsRequest().setQuery(query);
  Operation operation = client.matters().count(matterId, request).execute();

  while(!operation.getDone()) {
    operation = service.operations().get(operation.getName()).execute();
  if(operation.getResponse() != null) {
    return Long.parseLong(operation.getResponse.get("total_count").toString());
  return -1;


def count(service, matter_id):
  emails_to_search = ['email1', 'email2']
  mail_query_options = {'excludeDrafts': True}
  query_terms = ''
  mail_query = {
    'corpus': 'MAIL',
    'dataScope': 'ALL_DATA',
    'searchMethod': 'ACCOUNT',
    'accountInfo': {
        'emails': emails_to_search
    'terms': query_terms,
    'mailOptions': mail_query_options,
  request = {
    'query': mail_query
  operation = service.matters().count(matterId=matter_id, body=request).execute()

  while not operation.getDone():
    operation = service.operations().get(name=operation.getName()).execute()

  if operation.getResponse() is None:
    return -1

  return operation.getResponse()["total_count"]