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

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

סוגים של שדות תווית

השדות של התוויות ב-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'
קבצים שבהם {9/}_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("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 השני של תווית שצריך להחזיר.