특정 라벨 또는 필드 값이 있는 파일 검색

이 페이지에서는 특정 라벨 또는 필드 값이 적용된 파일을 검색하는 방법을 설명합니다.

라벨 필드 유형

Google Drive 라벨 필드는 강력한 유형이 지정되어 있으며 각 유형은 서로 다른 색인 생성 및 검색 의미 체계를 지원합니다. 다음 표에는 사용 가능한 데이터 유형이 나와 있습니다.

유형 라벨 유형 옵션 지원되는 검색 연산자
텍스트 TextOptions is null, is not null, =, contains, starts with
긴 텍스트 LongTextOptions is null, is not null, contains
정수 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'
execution_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

다음 코드 샘플은 하나 이상의 labelId를 사용하여 Drive 파일 리소스에서 특정 라벨 또는 필드 값이 있는 모든 파일을 나열하는 방법을 보여줍니다. files.list 메서드도 사용합니다. 요청 본문은 비어 있어야 합니다.

대답에 labelInfo를 포함하려면 다음 사항도 지정해야 합니다.

  • includeLabels를 ID의 쉼표로 구분된 목록으로 사용합니다.

  • fields 매개변수의 labelInfoincludeLabels 내에서 labelInfo를 반환하도록 지정합니다.

성공하면 응답 본문에 파일 목록이 포함됩니다.

자바

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: 반환할 라벨의 두 번째 labelId입니다.