Manage saved queries

The saved query API lets you programmatically manage saved queries in Vault. You can run a saved query to retrieve the same results or additional, newer results, depending on the criteria of the query. For example, if your query includes an end date, you'll retrieve the same results you did when you first ran the query. If the query doesn't include an end date, you'll get your original results plus any new data that meets the search criteria.

You can use this API in scripts that:

  • create a saved query in a matter
  • delete saved query from a matter
  • retrieve a saved query from a matter
  • retrieve all saved queries from a matter

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.

Create a saved search query

The following examples show how to create saved queries for mail messages and for files in Drive:

Mail

Java

public SavedQuery createMailAllDataAccountSavedQuery(String matterId) throws Exception {
    AccountInfo emailsToSearch =
        new AccountInfo().setEmails((ImmutableList.of("email1", "email2")));
    MailOptions mailOptions = new MailOptions().setExcludeDrafts(true);
    String queryTerms = "to:ceo@solarmora.com";
    Query mailQuery =
        new Query()
            .setCorpus("MAIL")
            .setDataScope("ALL_DATA")
            .setSearchMethod("ACCOUNT")
            .setAccountInfo(emailsToSearch)
            .setTerms(queryTerms)
            .setMailOptions(mailOptions);
    SavedQuery savedQuery =
        new SavedQuery()
            .setDisplayName("NEW SAVED QUERY NAME")
            .setQuery(mailQuery);
    return client.matters().savedQueries().create(matterId, savedQuery).execute();
} 

Python

def create_mail_all_data_account_saved_query(self, 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,
    }
    saved_query = {
        'displayName': 'NEW SAVED QUERY NAME',
        'query': mail_query,
    }
    return self.service.matters().savedQueries().create(
        matterId=matter_id, body=saved_query).execute()

Drive

Java

public SavedQuery createDriveAllDataOUSavedQuery(String matterId) throws Exception {
    OrgUnitInfo ouToSearch = new OrgUnitInfo().setOrgUnitId("ou id retrieved from admin sdk");
    DriveOptions driveQueryOptions = new DriveOptions().setIncludeTeamDrives(true);
    Query driveQuery =
        new Query()
            .setCorpus("DRIVE")
            .setDataScope("ALL_DATA")
            .setSearchMethod("ORG_UNIT")
            .setOrgUnitInfo(ouToSearch)
            .setDriveOptions(driveQueryOptions);
    SavedQuery savedQuery =
        new SavedQuery()
            .setDisplayName("NEW SAVED QUERY NAME")
            .setQuery(driveQuery);
    return client.matters().savedQueries().create(matterId, savedQuery).execute();
  }
} 

Python

def create_drive_all_data_ou_saved_query(self, matter_id):
    ou_to_search = 'ou id retrieved from admin sdk'
    drive_query_options = {'includeTeamDrives': True}
    drive_query = {
        'corpus': 'DRIVE',
        'dataScope': 'ALL_DATA',
        'searchMethod': 'ORG_UNIT',
        'orgUnitInfo': {
            'org_unit_id': ou_to_search,
        },
        'driveOptions': drive_query_options
    }
    saved_query = {
        'displayName': 'NEW SAVED QUERY NAME',
        'query': drive_query,
    }
    return self.service.matters().savedQueries().create(
        matterId=matter_id, body=saved_query).execute()

Delete a saved search query

The following example shows how to delete a saved query.

Java

public Empty deleteSavedQuery(String matterId, String savedQueryId) throws Exception {
    return client.matters().savedQueries().delete(matterId, savedQueryId).execute();
} 

Python

def delete_saved_query(self, matter_id, saved_query_id):
    empty_response = self.service.matters().savedQueries().delete(
        matterId=matter_id, savedQueryId=saved_query_id).execute()
    return empty_response

Get a saved search query

The following example shows how to retrieve a saved query by its ID.

Java

public SavedQuery getSavedQuery(String matterId, String savedQueryId) throws Exception {
    return client.matters().savedQueries().get(matterId, savedQueryId).execute();
} 

Python

def get_saved_query(self, matter_id, saved_query_id):
    saved_query = self.service.matters().savedQueries().get(
        matterId=matter_id, savedQueryId=saved_query_id).execute()
    return saved_query

List saved queries in a matter

The following example shows how to list a matter's saved queries.

Java

public void listSavedQueries(String matterId) throws Exception {
    ListSavedQueriesResponse firstPageResponse =
        client.matters().savedQueries().list(matterId).setPageSize(10).execute();
    String nextPageToken = firstPageResponse.getNextPageToken();
    if (nextPageToken != null) {
      client
          .matters()
          .savedQueries()
          .list(matterId)
          .setPageSize(10)
          .setPageToken(nextPageToken)
          .execute();
    }
} 

Python

def list_saved_queries(self, matter_id):
    first_page_response = self.service.matters().savedQueries().list(
        matterId=matter_id, pageSize=10).execute()
    if 'nextPageToken' in first_page_response:
      self.service.matters().savedQueries().list(
          pageSize=10,
          pageToken=first_page_response['nextPageToken']).execute()