Splunk Search

How to search for a value in multiple fields

robinettdonWY
Path Finder

I have 2 sources in separate indexes; the first contains a field "appId"; to get the human readable (appDisplayName) I need to search the 2nd source. Normally I'd do this with a subsearch:

index=index2 sourcetype=st2 
    [search index=index1 sourcetype=st1 | stats values(appId) as appId |format]
|stats values(appDisplayName) as appDisplayName by appId

The problem I'm running into is the ID for the app in the first source is always called "appId" but depending on the type of app (which i don't know from the first source); appId will either correspond to 1 of 2 fields in the second source; it will either be "appId" or "resourceId". I need to find "appId" from the 1st source in either "appId" or "resourceId" in the second source and the corresponding human readable will either be "appDisplayName" or "resourceDisplayName" in the second source.

Any ideas on how to approach this?

Thanks!

Labels (2)
0 Karma
1 Solution

DalJeanis
Legend

Don't use a subsearch where the stats can handle connecting the two. This is called the "Splunk soup" method.

(index=index2 sourcetype=st2) OR 
(index=index1 sourcetype=st1)
 | fields appId, resourceId appDisplayName resourceDisplayName
 | rename COMMENT as "above selects only the record types and fields you need" 

 | rename COMMENT as "create synthetic fields as per diogofm answer" 
 | eval appId = coalesce(appId, resourceId)
 | eval appDisplayName = colaesce(appDisplayName, resourceDisplayName)

 | rename COMMENT as "stats together for final result" 
 | stats values(appDisplayName) as appDisplayName by appId

View solution in original post

0 Karma

DalJeanis
Legend

Don't use a subsearch where the stats can handle connecting the two. This is called the "Splunk soup" method.

(index=index2 sourcetype=st2) OR 
(index=index1 sourcetype=st1)
 | fields appId, resourceId appDisplayName resourceDisplayName
 | rename COMMENT as "above selects only the record types and fields you need" 

 | rename COMMENT as "create synthetic fields as per diogofm answer" 
 | eval appId = coalesce(appId, resourceId)
 | eval appDisplayName = colaesce(appDisplayName, resourceDisplayName)

 | rename COMMENT as "stats together for final result" 
 | stats values(appDisplayName) as appDisplayName by appId
0 Karma

diogofgm
SplunkTrust
SplunkTrust

You can always eval the multiple fields into one using coalesce.

|eval appId = coalesce(appId, resourceId)
|eval appDisplayName = colaesce(appDisplayName, resourceDisplayName)

A nice blog post about coalesce:
https://www.splunk.com/en_us/blog/tips-and-tricks/search-command-coalesce.html

------------
Hope I was able to help you. If so, some karma would be appreciated.
0 Karma

robinettdonWY
Path Finder

Thanks! I wish I could accept both answers.

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...