Stay organized with collections
Save and categorize content based on your preferences.
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:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["Count query results\n-------------------\n\nYou 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.\n\nTo work with Vault resources, the account must have the [required Vault\nprivileges](https://support.google.com/vault/answer/2799699) and access to the\nmatter. To access a matter, the account must have created the matter, have the\nmatter shared with them, or have the **View All Matters** privilege.\n\nThe following example shows how to count the results returned by a query for messages that meet the following criteria:\n\n- messages owned by accounts `email1` and `email2`.\n- excludes draft messages.\n- messages sent to `ceo@solarmora.com`.\n\n### Java\n\n```java\npublic Long count(Vault client, String matterId) {\n AccountInfo emailsToSearch = new AccountInfo().setEmails(ImmutableList.of(\"email1\", \"email2\"));\n MailOptions mailQueryOptions = new MailOptions().setExcludeDrafts(true);\n String queryTerms = \"to:ceo@solarmora.com\";\n Query query =\n new Query()\n .setCorpus(\"MAIL\")\n .setDataScope(\"ALL_DATA\")\n .setSearchMethod(\"ACCOUNT\")\n .setAccountInfo(emailsToSearch)\n .setTerms(queryTerms);\n CountArtifactsRequest request = new CountArtifactsRequest().setQuery(query);\n Operation operation = client.matters().count(matterId, request).execute();\n\n while(!operation.getDone()) {\n sleep(2000);\n operation = service.operations().get(operation.getName()).execute();\n }\n if(operation.getResponse() != null) {\n return Long.parseLong(operation.getResponse.get(\"total_count\").toString());\n }\n return -1;\n}\n \n```\n\n### Python\n\n```python\ndef count(service, matter_id):\n emails_to_search = ['email1', 'email2']\n mail_query_options = {'excludeDrafts': True}\n query_terms = 'to:ceo@solarmora.com'\n mail_query = {\n 'corpus': 'MAIL',\n 'dataScope': 'ALL_DATA',\n 'searchMethod': 'ACCOUNT',\n 'accountInfo': {\n 'emails': emails_to_search\n },\n 'terms': query_terms,\n 'mailOptions': mail_query_options,\n }\n request = {\n 'query': mail_query\n }\n operation = service.matters().count(matterId=matter_id, body=request).execute()\n\n while not operation.getDone():\n time.sleep(2)\n operation = service.operations().get(name=operation.getName()).execute()\n\n if operation.getResponse() is None:\n return -1\n\n return operation.getResponse()[\"total_count\"]\n \n```"]]