Use a subsearch
In this section you will learn how to correlate events by using subsearches.
Think of a subsearch as a search within a search. In a search string, subsearches are contained in square brackets and evaluated first. The result of the subsearch is then used as an argument to the primary, or outer, search.
Let's find the single most frequent shopper on the Buttercup Games online store, and what that shopper has purchased.
The following examples show why a subsearch is useful. Example 1 shows how to find the most frequent shopper without a subsearch. Example 2 shows how to find the most frequent shopper with a subsearch.
Example 1: Search without a subsearch
You want to find the single most frequent shopper on the Buttercup Games online store and what that shopper has purchased. Use the
top command to return the most frequent shopper.
1. To find the shopper who accessed the online shop the most, use this search.
sourcetype=access_* status=200 action=purchase | top limit=1 clientip
limit=1argument specifies to return 1 value. The
clientipargument specifies the field to return.
- This search returns one
clientipvalue, which you will use to identify the VIP shopper.
You now need to run another search to determine how many different products the VIP shopper has purchased.
2. Use the
stats command to count the purchases by this VIP customer.
sourcetype=access_* status=200 action=purchase clientip=126.96.36.199 | stats count, dc(productId), values(productId) by clientip
- This search uses the
count()function to return the total count of the purchases for the shopper. The
dc()function is the distinct_count function. Use this function to count the number of different, or unique, products that the shopper bought. The
valuesargument is used to display the actual product IDs in the results.
The drawback to this approach is that you have to run two searches each time you want to build this table. The top purchaser is not likely to be the same person at any given time range.
Example 2: Search with a subsearch
A subsearch is enclosed in square brackets [ ] and processed first when the search is parsed.
1. Copy and paste the following search into the Search bar and run the search.
sourcetype=access_* status=200 action=purchase [search sourcetype=access_* status=200 action=purchase | top limit=1 clientip | table clientip] | stats count, dc(productId), values(productId) by clientip
- The subsearch is the portion of the search in the square brackets.
search sourcetype=access_* status=200 action=purchase | top limit=1 clientip | table clientip
- This subsearch is almost identical to the search in Example 1 Step 1. The difference is the last piped command,
| table clientip.
- Because the
topcommand returns the count and percent fields, the
tablecommand is used to keep only the
- These results should match the result of the two searches in Example 1, if you run it on the same time range. If you change the time range, you might see different results because the top purchasing customer will be different.
- Note: The performance of this subsearch depends on how many distinct IP addresses match
status=200 action=purchase. If there are thousands of distinct IP addresses, the
topcommand has to keep track of all of those addresses before the top 1 is returned, impacting performance. By default, subsearches return a maximum of 10,000 results and have a maximum runtime of 60 seconds. In large production environments, it is possible that the subsearch in this example will timeout before it completes. The best option is to rewrite the query to limit the number of events that the subsearch must process. Alternatively, you can increase the maximum results and maximum runtime parameters.
You can make the information more understandable by renaming the columns.
You rename columns by using the AS operator on the fields in your search. If the rename that you want to use contains a space, you must enclose the rename in quotation marks.
2. To rename the fields, copy and paste the following search into the Search bar and run the search.
sourcetype=access_* status=200 action=purchase [search sourcetype=access_* status=200 action=purchase | top limit=1 clientip | table clientip] | stats count AS "Total Purchased", dc(productId) AS "Total Products", values(productId) AS "Products ID" by clientip | rename clientip AS "VIP Customer"
3. Experiment with this search. What happens when you run the search over different time periods? What if you wanted to find the top product sold and how many people bought it?
This completes Part 4 of the Search Tutorial.
You have learned how to use fields, the Splunk search language, and subsearches to search your data. Continue to Part 5: Enriching events with lookups.
Use the search language
Enabling field lookups
This documentation applies to the following versions of Splunk® Enterprise: 6.4.0, 6.4.1, 6.4.2, 6.4.3, 6.4.4, 6.4.5, 6.4.6, 6.4.7, 6.4.8, 6.4.9, 6.4.10, 6.4.11