字段(适用于探索)

此页面涉及 探索的一部分 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 可让您指定 Explore 中的哪些字段在 Explore 界面中公开。这些字段可以是探索的基本视图中的字段,也可以是探索的联接中提供的字段。如果您不使用 fields,Looker 默认会公开所有字段。

字段列表可按如下方式使用:[view_name.field_a, view_name.field_b]

您还可以引用一组字段(在视图的 set 参数中定义),例如 [view_name.set_a*]。星号表示您引用的是集合名称,而不是字段名称。

在这两种情况下,请注意,字段或集必须具有完整的范围。换句话说,您必须同时添加视图名称和字段名称。

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 探索中的 customer 视图中显示名为 nameaddressage 的字段:

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

仅在 customer“探索”中显示 customer 视图中 export_fields 集内的字段集:

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} ;;
  }
}

注意事项

您可以使用 fieldsexplore 排除单个商品或一组商品

您可以使用 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]
  }
}

作为 join 的一部分的 fields 参数无法使用 ALL_FIELDS

本页面介绍 explore 的子级参数 fields。以这种方式使用 fields 参数时,您可以访问 ALL_FIELDS* 集,然后排除不需要的字段,如前所述。

还有一个 fields 参数,它是 join 的子参数。如果以这种方式使用,您将无法使用 ALL_FIELDS* 集。

ALL_FIELDS 集受 join 的一部分 fields 参数的限制

本页介绍了嵌套在 explore 参数下的 fields。还有一个类似的 fields 参数,嵌套在 join 下。请务必了解同时将 fields 应用于这两个级别的含义。

首先,应用 join 下的所有 fields 参数。这会创建一组可供探索选择的字段。请参考下面的示例:

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

在此示例中:

  • orders 中的所有字段都将可用,并包含在 ALL_FIELDS* 中。
  • users 中的 name 将可供使用,并包含在 ALL_FIELDS* 中。
  • users 中的其他字段将不会包含在 ALL_FIELDS* 集中,也无法使用。

现在,如果我们向 explore 下添加 fields 参数,则会在该组的基础上添加限制。假设我们执行了以下操作:

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

在此示例中:

  • 来自 ordersprice 将按预期显示,因为它位于我们创建的 ALL_FIELDS* 集中。
  • 来自 usersaddress 不会显示,因为在加入 users 时未包含 address(仅包含 name)。
  • 来自 usersname 也不会显示,因为我们未将其添加到第 fields: [orders.price, users.address] 行的“探索”中。