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

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

라벨 필드 유형

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'
댓글이 '안녕하세요'로 시작하는 파일 labels/contract.comment STARTS WITH 'hello'
상태가 실행되는 파일 labels/contract.status = 'executed'
상태가 실행되지 않는 파일 labels/contract.status != 'executed'
실행_날짜가 특정 날짜 이전인 파일 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를 원한다는 것을 나타냅니다.

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

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