本页中提及的
fields
参数是探索的一部分。
fields
也可用作联接的一部分,如fields
(针对联接)参数文档页面所述。
fields
也可用作集的一部分(如set
参数文档页面中所述)。
用量
字段:[
field-or-set-specification,
field-or-set-specification,
...
]
}
层次结构
fields |
默认值
ALL_FIELDS* 接受
方括号内包含以英文逗号分隔的字段或集合列表特殊规则
|
定义
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
探索”中,仅显示 customer
视图中名为 name
、address
和 age
的字段:
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} ;;
}
}
注意事项
您可以将 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]
}
}
属于 join
的 fields
参数无法使用 ALL_FIELDS
集
本页面引用的 fields
参数是 explore
的子级。按此方式使用时,您可以访问 ALL_FIELDS*
集,然后排除不需要的字段(如上所示)。
还有一个 fields
参数,它是 join
的子元素。以这种方式使用时,您无法使用 ALL_FIELDS*
集。
ALL_FIELDS
集受 join
中的 fields
参数的限制
本页介绍了当嵌套在 explore
参数下时 fields
的情况。此外,还有一个嵌套在 join
下的类似 fields
参数。请务必了解同时对这两个级别应用 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} ;;
}
}
在此示例中:
- 来自
orders
的price
将按预期显示,因为它位于我们创建的ALL_FIELDS*
集中。 - 不会显示来自
users
的address
,因为users
加入时并未包含(只有name
)。 - 来自
users
的name
也不会显示,因为我们未将其添加到“fields: [orders.price, users.address]
”行中的“探索”。