用法
explore: explore_name {
conditionally_filter: {
filters: [field_name: "filter expression", field_name: "filter expression", ...]
unless: [field_name, field_name, ...]
}
}
|
层次结构
conditionally_filter |
默认值
无
接受
一个或多个过滤条件规范(包含字段名称和 Looker 过滤条件表达式),以及 unless 部分中的一个或多个字段名称的列表
|
定义
通过 conditionally_filter 参数,您可以定义一组默认过滤条件,只要用户应用您所定义的第二个列表中的至少一个过滤条件,就可以覆盖这些默认过滤条件。
此参数通常用于防止用户意外创建大型查询,避免给数据库带来过高运行成本。例如,您可以强制用户将其查询范围限制为上周,除非用户明确要求更大的日期范围。
在 conditionally_filter 中应用的过滤条件会在用户运行查询后显示。虽然用户可以更改您设置的默认 value,但除非他们应用了 unless 子参数中指定的至少一个过滤条件,否则无法完全移除该过滤条件。
您使用的字段名称可以是 dimension 或 measure 的名称。
如需引用联接视图(而非此探索)中的维度或度量,请使用 view_name.field_name。
示例
请参考以下示例:
explore: order {
conditionally_filter: {
filters: [id: "123", customer.id: "678,789"]
unless: [date]
}
join: customer {
sql_on: ${order.customer_id} = ${customer.id} ;;
}
}
在本例中,id 过滤条件是指“探索”中名为 order 的 id 字段。customer.id 过滤条件是指名为 customer 的视图中的 id 字段。除非用户在“探索”界面中设置订单日期,否则系统会同时应用这两个过滤条件。此示例还演示了您可以要求提供多个过滤条件。
您指定的默认值可以接受这些类型的表达式。
您还可以强制用户使用订单 ID 过滤条件(默认值为“123”,用户可以更改),除非他们应用订单日期过滤条件:
explore: order {
conditionally_filter: {
filters: [id: "123"]
unless: [date]
}
}
或者,强制用户使用订单 ID 过滤条件(默认值为“123”或“234”,用户可以更改),除非他们应用订单日期或订单时间过滤条件:
explore: order {
conditionally_filter: {
filters: [id: "123,234"]
unless: [date, time]
}
}
或者,强制用户使用订单 ID 过滤条件(默认值为“123”)和客户城市过滤条件(默认值为“芝加哥”),除非他们应用订单日期或客户日期过滤条件:
explore: order {
conditionally_filter: {
filters: [id: "123", customer.city: "Chicago"]
unless: [date, customer.date]
}
join: customer {
sql_on: ${order.customer_id} = ${customer.id} ;;
}
}
常见挑战
使用 conditionally_filter 时,用户无法移除所有过滤条件
使用 conditionally_filter 时,无法在不使用任何过滤条件的情况下运行查询。用户必须使用您指定的条件过滤器,或使用 unless 列表中的自有过滤器。
如果某个组中包含维度 conditionally_filter 和维度 type: time,则该组的其他维度会放在 unless 子参数中
如果您在 conditionally_filter 中指定的 field 是属于维度组的时间维度,那么即使您未添加 unless 子形参,Looker 也会将该组的所有其他维度视为受相应条件过滤器的 unless 子形参约束。
以下两个 LookML 代码块的解读方式完全相同。在此示例中,conditionally_filter 应用于属于 event 维度组的时间维度 event_date。未指定任何 unless 条件,但 Looker 会将 event 群组中的其他维度视为已使用 unless 子形参指定。
LookML 代码块 1:
explore: logs {
# Make sure there is always a filter on event_date, event_week, event_month or event_year
# Default to the last complete day of data
conditionally_filter: {
filters: [logs.event_date: "1 days ago for 1 day"]
}
view: logs {
# Combine the partition date filters and the time filters into a single field group.
dimension_group: event {
type: time
timeframes: [date,week,month,year]
sql: _PARTITIONTIME ;;
}
}
LookML 代码块 2:
explore: logs {
# Make sure there is always a filter on event_date, event_week, event_month or event_year
# Default to the last complete day of data
conditionally_filter: {
filters: [logs.event_date: "1 days ago for 1 day"]
unless: [event_week, event_month, event_year]
}
view: logs {
# Combine the partition date filters and the time filters into a single field group.
dimension_group: event {
type: time
timeframes: [date,week,month,year]
sql: _PARTITIONTIME ;;
}
}
Looker 会以相同的方式解读这两个 LookML 块,即使只有第二个 LookML 块明确将 unless 子参数应用于 event 组的其他维度。
注意事项
有一种方法可将 conditionally_filter 应用于部分用户
如需为部分用户应用条件过滤,但不为其他用户应用,您可以使用模型权限。您需要创建两个模型:一个使用 conditionally_filter,另一个不使用。然后,您可以根据具体用户授予对相应模型的访问权限。
如果您想使用不含 unless 的 conditionally_filter,只需改用 always_filter 即可
如需强制用户使用一组特定的过滤条件(无论如何),但允许他们更改默认值,请改用 always_filter。
如果您需要完全无法更改的过滤条件,请考虑使用 sql_always_where
如果您希望探索具有对所有人相同的过滤条件,并且不允许用户更改过滤条件值,请使用 sql_always_where。
如果您需要无法更改的特定于用户的过滤条件,请考虑使用 access_filter
如果您希望探索具有特定于每个用户的过滤条件,但这些过滤条件无法移除或更改,请使用 access_filter。