計算查詢結果
您可以使用 matters.count
方法,在建立匯出作業前計算 Gmail 或網路論壇查詢的訊息數量。有了這項資訊,您就能調整查詢篩選器,以便傳回更多或更少的結果。
如要使用保管箱資源,帳戶必須具備必要的保管箱權限,並能存取相關資料。如要存取案件,帳戶必須是該案件的建立者、共用對象,或具備「查看所有案件」權限。
以下範例說明如何針對符合下列條件的訊息,計算查詢傳回的結果:
- 由帳戶
email1
和email2
擁有的郵件。 - 排除草稿郵件。
- 傳送至
ceo@solarmora.com
的訊息。
Java
public Long count(Vault client, String matterId) { AccountInfo emailsToSearch = new AccountInfo().setEmails(ImmutableList.of("email1", "email2")); MailOptions mailQueryOptions = new MailOptions().setExcludeDrafts(true); String queryTerms = "to:ceo@solarmora.com"; Query query = new Query() .setCorpus("MAIL") .setDataScope("ALL_DATA") .setSearchMethod("ACCOUNT") .setAccountInfo(emailsToSearch) .setTerms(queryTerms); CountArtifactsRequest request = new CountArtifactsRequest().setQuery(query); Operation operation = client.matters().count(matterId, request).execute(); while(!operation.getDone()) { sleep(2000); operation = service.operations().get(operation.getName()).execute(); } if(operation.getResponse() != null) { return Long.parseLong(operation.getResponse.get("total_count").toString()); } return -1; }
Python
def count(service, matter_id): emails_to_search = ['email1', 'email2'] mail_query_options = {'excludeDrafts': True} query_terms = 'to:ceo@solarmora.com' 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(): time.sleep(2) operation = service.operations().get(name=operation.getName()).execute() if operation.getResponse() is None: return -1 return operation.getResponse()["total_count"]