index="tutorialdata" sourcetype="access*" status=200 action=purchase [search index="tutorialdata" sourcetype="access*" status=200 action=purchase | top limit=1 clientIP | table clientIP] | stats count by productId
A subsearch in Splunk is a search within a search. Subsearches allow you to run a secondary search and use the results of that search as input for the main (outer) search. Subsearches are enclosed in square brackets ([...]), and their results are passed to the outer search for further processing.
Here’s a breakdown of how subsearches work, some common use cases, and examples:
subsearch_maxout.Subsearches are enclosed within square brackets [...], and they are placed within the main search.
<main_search> [<subsearch>]This is one of the most common subsearch use cases. A subsearch is used to generate a set of field-value pairs that are passed to the main search as filters.
index=web sourcetype=access_logs [search index=security sourcetype=firewall_logs | fields ip | dedup ip]search index=security sourcetype=firewall_logs | fields ip | dedup ip) retrieves unique ip addresses from firewall logs. The results are passed to the main search, which then filters access logs (index=web sourcetype=access_logs) to show only events from those specific IPs.returnThe return command inside a subsearch allows you to format the results in a specific way (as a list of field-value pairs). This is useful when you need to pass specific fields to the outer search.
index=web sourcetype=access_logs [search index=security sourcetype=user_logs "login_failed" | fields user | dedup user | return 10 user]user_logs (index=security sourcetype=user_logs "login_failed"). The return 10 user command formats the output as user="user1" OR user="user2" OR .... This result is passed to the outer search, which filters access_logs to show only the logs for those specific users.A subsearch can be used to match dynamic values in the outer search.
index=web sourcetype=access_logs [search index=web sourcetype=error_logs error_type=high | stats count by host | where count > 100 | fields host]high error events (index=web sourcetype=error_logs error_type=high). The main search (index=web sourcetype=access_logs) then retrieves logs from those hosts.formatThe format command is used within a subsearch to format the results as a list of OR’ed conditions. This is useful when you want to pass a list of field-value pairs to the outer search.
index=web sourcetype=access_logs [search index=web sourcetype=auth_logs "login_failed" | stats count by ip | where count > 10 | fields ip | format]login_failed). The format command transforms the list of IPs into a condition like ip="192.168.0.1" OR ip="192.168.0.2" OR ..., and the main search filters access logs for those IPs.Subsearches can be used for exclusion as well. You can use a subsearch to exclude specific results from the outer search.
index=web sourcetype=access_logs NOT [search index=security sourcetype=suspicious_ips | fields ip]suspicious_ips logs, and the main search excludes events from the access_logs that contain those IPs.append)You can use subsearches to append or join data from two different searches, allowing you to merge different datasets.
index=web sourcetype=access_logs | append [search index=web sourcetype=user_logs | fields user, action]user_logs search to the access_logs search. The fields user, action ensures that only the relevant fields from the subsearch are appended.| Command | Description |
|---|---|
return |
Outputs results in a list of field=value pairs, or as field="value1" OR field="value2". |
format |
Formats the results of the subsearch as a set of OR conditions. |
fields |
Specifies which fields to include in the subsearch results. |
append |
Appends the subsearch results to the outer search results (used for joining data). |
NOT |
Excludes the results of a subsearch from the outer search. |
subsearch_maxout in the limits.conf file if necessary, though increasing this can impact performance.[search <subsearch>] runs a secondary search and uses its results to filter or manipulate the outer search.NOT [<subsearch>]).append or similar commands to join results from different searches.Subsearches are a powerful tool in Splunk, enabling complex searches by passing results between searches. However, care should be taken to manage performance, especially with large datasets.