Splunk Search

How to find top 20 results and then do a subsequent search?

jackreeves
Explorer

I need to find out the Top 20 sites within my sourcetype and then from there be able to do further analysis on other fields such as Product.
Are there a non-destructive stats command I can use for this?

i.e.

sourcetype=site_data | stats count by "Site Name" | head 20  

and then a subsequent search to find out of those twenty sites what is the top product logged?

Thanks,
Jack

0 Karma
1 Solution

spayneort
Contributor

You could use a subsearch:

sourcetype=site_data [|search sourcetype=site_data | top 20 "Site Name" | fields "Site Name"] <put the rest of your search here>

https://docs.splunk.com/Documentation/Splunk/7.3.0/Search/Usesubsearchtocorrelateevents

View solution in original post

woodcock
Esteemed Legend

Like this:

index=YouShouldAlwaysSpecifyAnIndex sourcetype=site_data
[search index=YouShouldAlwaysSpecifyAnIndex sourcetype=site_data
| top limit=20 "Site Name" | table "Site Name" | format]
| top limit=1 Product BY "Site Name"
0 Karma

woodcock
Esteemed Legend

P.S. field names with spaces are E*V*I*L!

0 Karma

spayneort
Contributor

You could use a subsearch:

sourcetype=site_data [|search sourcetype=site_data | top 20 "Site Name" | fields "Site Name"] <put the rest of your search here>

https://docs.splunk.com/Documentation/Splunk/7.3.0/Search/Usesubsearchtocorrelateevents

danielansell
Path Finder

To add on to this answer, the subsearch provided by spayneort effectively returns the top 20 "Site Name" values as 20 "OR" seperated field=value pairs.

To further understand it, Splunk performs the subsearch first then essentially modifies your search to be:
sourcetype=site_data "Site Name"=https://url1 OR "Site Name"=https://url2 OR "Site Name"=http://url2 OR ....

0 Karma

jackreeves
Explorer

Thanks guys this has worked as expected! Knew there must be a simple solution.

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Take Action Automatically on Splunk Alerts with Red Hat Ansible Automation Platform

 Are you ready to revolutionize your IT operations? As digital transformation accelerates, the demand for ...

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...

Beyond Detection: How Splunk and Cisco Integrated Security Platforms Transform ...

Financial services organizations face an impossible equation: maintain 99.9% uptime for mission-critical ...