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

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

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

שדות התוויות ב-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, =, !=
רשימת בחירה SelectOptions (עם 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. חיפוש לפי שדות עם ערך יחיד

אתם יכולים לכתוב שאילתות חיפוש שיתאימו לערכי השדות הצפויים. בטבלה הבאה מוצגות שאילתות השדות החוקיים:

על מה ברצונך להריץ שאילתה מחרוזת שאילתה
פריטים שבהם התגובה מוגדרת כ'שלום' labels/contract.comment = 'hello'
קבצים שהתגובה מתחילה בהם ב-'hello' labels/contract.comment STARTS WITH 'hello'
קבצים שהסטטוס שלהם הוא ביצוע labels/contract.status = 'executed'
קבצים שהסטטוס שלהם הוא לא בוצעה labels/contract.status != 'executed'
קבצים שבהם Campaign_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. הוא גם משתמש ב-method 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 השני של תווית שיש להחזיר.