特定のラベルやフィールド値を持つファイルを検索する

このページでは、特定のラベルまたはフィールド値が適用されたファイルを検索する方法について説明します。

ラベル フィールドのタイプ

Google ドライブのラベル フィールドは、各タイプで異なるインデックス作成と検索セマンティクスをサポートしているため、厳密に型指定されています。次の表に、使用可能なデータ型を示します。

種類 ラベルタイプのオプション サポートされている検索演算子
テキスト TextOptions is null, is not null, =, contains, starts with
長いテキスト LongTextOptions is null, is not null, contains
Integer IntegerOptions is null, is not null, =, !=, <, >, <=, >=
日付 DateOptions is null, is not null, =, !=, <, >, <=, >=
選択 SelectionOptions is null, is not null, =, !=
ユーザー UserOptions is null, is not null, =, !=
選択リスト SelectionOptions(max_entries > 1) is null, is not null, in, not in
ユーザーリスト UserOptions(max_entries > 1) is null, is not null, in, not in

1. ラベルやフィールドの有無に基づく検索

特定のラベルが適用されている(または適用されていない)アイテムを検索できます。

  • 'labels/contract' in labels
  • not 'labels/contract' in labels

また、特定のフィールドが設定されている(または設定されていない)アイテムを検索することもできます。

  • labels/contract.comment IS NOT NULL
  • labels/contract.comment IS NULL

2. 単一値のフィールドに基づく検索

想定されるフィールド値に一致する検索クエリを作成できます。次の表に、有効なフィールド クエリを示します。

クエリ対象 クエリ文字列
コメントが「hello」に設定されているアイテム labels/contract.comment = 'hello'
コメントが「hello」で始まるファイル labels/contract.comment STARTS WITH 'hello'
ステータスが実行されるファイル labels/contract.status = 'executed'
ステータスが実行されないファイル labels/contract.status != 'executed'
run_date が特定の日付より前のファイルであるファイル labels/contract.execution_date < '2020-06-22'
value_usd(整数)が特定の値より小さいファイル labels/contract.value_usd < 2000
client_contact が特定のメールアドレスに設定されているファイル labels/contract.client_contact = 'alex@altostrat.com'

3. 複数の値を持つフィールド(ListOptions.max_entries > 1 など)を持つフィールドに基づいて検索する

複数の値に対応するフィールドは、IN 演算子でのみクエリできます。

  • 'EMAIL_ADDRESS' IN labels/project.project_leads
  • NOT 'EMAIL_ADDRESS' IN labels/project.project_leads

次のコードサンプルは、1 つ以上の labelId を使用して、ドライブのファイル リソースから特定のラベルまたはフィールド値を持つすべてのファイルを一覧表示する方法を示しています。また、files.list メソッドも使用します。リクエストの本文は空にする必要があります。

レスポンスに labelInfo を含める場合は、次も指定する必要があります。

  • includeLabels: ID のカンマ区切りリスト。

  • fields パラメータの labelInfo は、includeLabels 内で labelInfo を返すことを示します。

成功した場合、レスポンスの本文にはファイルのリストが含まれます。

Java

List<File> fileList = driveService.files().list().setIncludeLabels("LABEL_1_ID,LABEL_2_ID").setFields("items(labelInfo, id)").setQ("'labels/LABEL_1_ID' in labels and 'labels/LABEL_2_ID' in labels").execute().getItems();

Python

file_list = drive_service.files().list(includeLabels="LABEL_1_ID,LABEL_2_ID", q="'labels/LABEL_1_ID' in labels and 'labels/LABEL_2_ID' in labels", fields="items(labelInfo, id)").execute();

Node.js

/**
* Search for Drive files with specific labels
* @return{obj} file list with labelInfo
**/
async function searchForFileWithLabels() {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});
  const service = google.drive({version: 'v3', auth});
  try {
    const fileList = await service.files.list({
      includeLabels: 'LABEL_1_ID,LABEL_2_ID',
      q: '\'labels/LABEL_1_ID\' in labels and \'labels/LABEL_2_ID\' in labels',
      fields:'files(labelInfo, id)',
    });
    return file;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }

次のように置き換えます。

  • LABEL_1_ID: 返されるラベルの最初の labelId
  • LABEL_2_ID: 返されるラベルの 2 番目の labelId