switch
Use the switch stage in a Data Pipeline filter to apply different fields stages to events based on conditions. Each event is evaluated against the case conditions in order, and the fields stages of the matching case determine which fields are kept or removed for that event.
Syntax
switch
case <condition1>: (
fields [-] <field1>, <field2>, ...
[| fields [-] <field3>, ...]
)
[case <condition2>: (
fields [-] <field1>, <field2>, ...
) ...]
[default: (
fields [-] <field1>, <field2>, ...
)]Parameters
case
clause
Yes
Defines a condition and the fields stages to apply to events that match it. You can define multiple case clauses.
<condition>
boolean
Yes
A logical expression, using the same syntax as the filter stage. Supports and, or, not, comparison operators, and supported functions.
default
clause
No
Defines the fields stages to apply to events that do not match any case condition. The default clause does not accept a condition.
fields
stage
Yes
One or more fields stages, separated by pipes (|) and enclosed in parentheses. Use - to exclude fields.
Returns
The switch stage returns each event with the fields selected or excluded by the fields stages of the matching case. Events that match no case are processed by the default clause. If there is no default clause, those events are returned unchanged.
Usage notes
The
switchstage is available only in Data Pipeline filters. In a pipeline filter, it can be combined with thefilterandfieldsstages, for examplefilter <condition> | fields <fields> | switch ....caseclauses are evaluated in the order they are defined. Only the firstcasewhose condition evaluates to true is applied to an event.Only the
fieldsstage is allowed inside acaseordefaultclause. Other stages, such asalterorfilter, are not allowed.Each
caseanddefaultclause must contain at least onefieldsstage, and eachfieldsstage must specify at least one field.Wildcards (
*) and theasclause are not allowed in afieldsstage inside theswitchstage.System fields cannot be excluded, for example
_time,_insert_time,_raw_log,_id,_vendor,_product,_tag, and the_collector_* fields. System field names are not case sensitive, so- _TIMEis also rejected. You can still include system fields.Only one
defaultclause is allowed, and it cannot include a condition.The
switch,case, anddefaultkeywords are not case sensitive.The functions that you can use in
caseconditions are the same as those supported in Data Pipeline filters. Array functions are not supported.The
inoperator in acasecondition accepts only a list of values, for example severity in ("HIGH","MEDIUM"). Inner queries, such asin(dataset = ...), are not supported.
Examples
The examples use the following sample events:
HIGH
alice
BLOCK
10.0.0.1
trace-01
{...}
MEDIUM
bob
ALLOW
10.0.0.2
trace-02
{...}
LOW
carol
ALLOW
10.0.0.3
trace-03
{...}
Example 1: Applying different field selections by severity
Goal: Keep only the key fields for high-severity events, remove the debugging field from medium-severity events, and remove the large fields from all other events.
XQL code:
Explanation: The HIGH event keeps only severity, user, action, and src_ip. The MEDIUM event keeps all fields except debug_info. The LOW event matches no case, so the default clause removes debug_info and raw_payload.
Output:
HIGH
alice
BLOCK
10.0.0.1
MEDIUM
bob
ALLOW
10.0.0.2
{...}
LOW
carol
ALLOW
10.0.0.3
Example 2: Using multiple fields stages in a case
Goal: Select a set of fields and then exclude one of them within the same case.
XQL code:
Explanation: For the HIGH and MEDIUM events, the first fields stage keeps severity, user, and src_ip, and the second fields stage removes src_ip. The LOW event is processed by the default clause, which removes raw_payload.
Output:
HIGH
alice
MEDIUM
bob
LOW
carol
ALLOW
10.0.0.3
trace-03
Example 3: Using switch without a default clause
Goal: Change only events that match a condition and leave all other events unchanged.
XQL code:
Explanation: Only the event where action is BLOCK has raw_payload removed. The other events match no case, and because there is no default clause, they are returned unchanged.
Output:
HIGH
alice
BLOCK
10.0.0.1
trace-01
MEDIUM
bob
ALLOW
10.0.0.2
trace-02
{...}
LOW
carol
ALLOW
10.0.0.3
trace-03
{...}
Example 4: Combining switch with filter and fields
Goal: Drop low-severity events, remove a field from all remaining events, and then apply field selection by action.
XQL code:
Explanation: The filter stage drops the LOW event, and the fields stage removes debug_info from the remaining events. The switch stage then keeps only the key fields for the BLOCK event and removes raw_payload from the other event.
Output:
HIGH
alice
BLOCK
10.0.0.1
MEDIUM
bob
ALLOW
10.0.0.2
Example 5: Invalid switch stages
Goal: Identify syntax that the switch stage does not support.
XQL code:
Explanation: Each of these filters is rejected when you save or simulate the Data Pipeline filter.
Related articles
Last updated
Was this helpful?
