חיפוש קבצים עם תווית או ערך שדה ספציפיים

במאמר הזה מוסבר איך לחפש קבצים עם תווית ספציפית או ערך שדה ספציפי.

סוגי שדות התווית

שדות התווית ב-Google Drive הם בעלי הקלדה חזקה, וכל סוג תומך בסמנטיקה שונה של יצירת אינדקס וחיפוש. בטבלה הבאה מוצגים סוגי הנתונים הזמינים:

סוג אפשרויות של סוג התווית אופרטורים נתמכים לחיפוש
טקסט TextOptions is null, is not null, =, contains, starts with
מספר שלם 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 כרשימה של מזהים מופרדים בפסיקים.

  • labelInfo בפרמטר fields כדי לציין שרוצים שהערך labelInfo יוחזר בתוך includeLabels.

אם הפעולה בוצעה ללא שגיאות, גוף התגובה יכיל את רשימת הקבצים.

Java

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

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="files(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 fileList;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

מחליפים את מה שכתוב בשדות הבאים:

  • LABEL_1_ID: labelIdהתווית הראשונה מתוך התוויות שיוחזרו.
  • LABEL_2_ID: התווית השנייה labelId להחזרה.