欄位 (適用於探索)

本頁面是指 探索中的 fields 參數。

fields 也可做為聯結的一部分,詳情請參閱 fields (適用於聯結) 參數說明文件頁面。

fields 也可做為集合的一部分,詳情請參閱 set 參數說明文件頁面。

用量


explore: explore_name {
  fields: [
    field-or-set-specification,
    field-or-set-specification,
    ...
  ]
}
階層
fields
預設值
ALL_FIELDS*

接受
方括號,內含以半形逗號分隔的欄位或集合清單

特別規則
  • 所有欄位和集合都必須完整界定範圍 (使用 view_name.field_name 語法)
  • 如要排除欄位和集合,請在前面加上連字號 (-)
  • 設定名稱一律以星號 (*) 結尾
  • 您可以使用內建的集合名稱 ALL_FIELDS*,其中包含探索中的所有欄位
  • 您可以使用內建的集合名稱 view_name*,其中包含參照檢視區塊中的所有欄位。

定義

fields 可讓您指定要在「探索」使用者介面中公開哪些探索欄位。這些欄位可位於「探索」的基本檢視畫面,或「探索」聯結提供的欄位。如果您未使用 fields,Looker 預設會公開所有欄位。

欄位清單可能像這樣使用:[view_name.field_a, view_name.field_b]

您也可以參照一組欄位 (在檢視區塊的 set 參數中定義),例如 [view_name.set_a*]。星號會告知 Looker,您參照的是集合名稱,而非欄位名稱。

請注意,在這兩種情況下,欄位或集合都必須完全限定範圍。換句話說,您必須同時提供檢視區塊名稱和欄位名稱。

Looker 會自動建立名為 ALL_FIELDS* 的集合,其中包含探索基本檢視區塊和聯結中的所有欄位。如果您只想排除少數不想要的欄位,這種做法就非常實用,例如:

explore: view_name {
  fields: [ALL_FIELDS*, -joined_view_name.unwanted_field]
}

Looker 也會自動建立包含特定檢視中所有欄位的集合,可使用 view_name* 格式參照。舉例來說,下列探索只包含 customers 檢視區塊中的欄位:

explore: all_people {
  fields: [customers*]
}

範例

只顯示「customer 探索」中「customer」檢視區塊的「name」欄位:

explore: customer {
  fields: [customer.name]
}

customer Explore 中,只顯示 customer 檢視畫面中名為 nameaddressage 的欄位:

explore: customer {
  fields: [customer.name, customer.address, customer.age]
}

只在「探索」的 export_fields 檢視畫面中顯示 customer 欄位集:customer

explore: customer {
  fields: [customer.export_fields*]
}

從「order」探索中排除「customer」檢視中的「status」欄位:

explore: order {
  fields: [ALL_FIELDS*, -customer.status]
  join: customer {
    sql_on: ${order.customer_id} = ${customer.id} ;;
  }
}

注意事項

您可以使用 fields 搭配 explore 排除個別項目或項目組合

您可以在 explore 參數下使用 fields,以便運用 ALL_FIELDS* 集合,然後排除欄位。例如:

explore: order {
  fields: [
    ALL_FIELDS*,
    -customer.unwanted_field_a,
    -customer.unwanted_field_b
  ]
  join: customer {
    sql_on: ${order.customer_id} = ${customer.id} ;;
  }
}

我們可以使用 view_name.field_name 語法,在「探索」層級的 fields 參數中,參照已彙整檢視區塊 customer 的欄位。

您也可以使用 - 語法排除定義的欄位集:

explore: orders {
  fields: [ALL_FIELDS*, -users.statezip*]
  join: users {
    sql_on: ${orders.user_id} = ${users.id} ;;
    relationship: many_to_one
  }
}

view: users {
  set: statezip {
    fields:[state, zip]
  }
}

屬於 joinfields 參數無法使用 ALL_FIELDS

本頁面是指 explore 的子項 fields 參數。以這種方式使用 fields 參數時,您可以存取 ALL_FIELDS* 集,然後排除不需要的欄位,如先前所示。

此外,還有 fields 參數,這是 join 的子項。以這種方式使用時,您無法使用 ALL_FIELDS* 集。

ALL_FIELDS 集會受到 fields 參數限制,該參數是 join 的一部分

本頁面說明 fields 巢狀結構位於 explore 參數下方時的情況。此外,join 底下也有類似的 fields 參數。請務必瞭解同時在兩個層級套用 fields 的影響。

首先,系統會套用 join 下的所有 fields 參數。這樣一來,探索檢視表就能從中選擇欄位。請參閱以下範例:

explore: orders {
  join: users {
    fields: [name]
    sql_on: ${orders.user_id} = ${users.id} ;;
  }
}

在這種情況下:

  • orders 中的所有欄位都會提供,並納入 ALL_FIELDS*
  • name 將可使用,並納入 ALL_FIELDS*users
  • users 中的其他欄位不會提供或納入 ALL_FIELDS* 集。

現在,如果我們在 explore 下方新增 fields 參數,就會在該組限制條件之上新增限制。假設我們執行下列操作:

explore: orders {
  fields: [orders.price, users.address]
  join: users {
    fields: [name]
    sql_on: ${orders.user_id} = ${users.id} ;;
  }
}

在這種情況下:

  • price 會如預期顯示,因為它位於我們建立的 ALL_FIELDS* 集中。orders
  • usersaddress「不會」顯示,因為加入 users 時未包含該使用者 (只有 name)。
  • name 中的 users 也不會顯示,因為我們未將其新增至 fields: [orders.price, users.address] 列的「探索」。