Splunk Search

How to write a Subsearch to provide search criteria for outer search?

dmerrick
Observer

Hello, I am trying to do what i believe would be a correlated subquery. I need to search a file for a value, then re-search that same file for everything related to that value.

In a log file of all items and the messages produced as they are processed, I need to search for specific failure messages, grab the item that failed and re-search the file for all messages related to that item. 

What I currently have:
source="logs" host="test"

    [

       search source="logs" host="test" ("failed to subtract" OR "failed to add")

       |  rex "^[(?<item>[\w.-]+)\].+"   

       |  dedup item

       |  fields + item

   ] 

|  rex "^[(?<item>[\w.-]+)\]\s(?<message>.+)"

|  table _time, item, message

 

The inner [search] gives results on its own, but when placed as a subsearch, the whole provides no results.

Any help would be appreciated!

Labels (1)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

source="logs" host="test"
| rex "(?<failure>failed to subtract|failed to add)"
| rex "^\[(?<item>[\w.-]+)\].+"   
| eventstats values(failure) as failure by item
| where isnotnull(failure)
| rex "^\[(?<item>[\w.-]+)\]\s(?<message>.+)"
| table _time, item, message
0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @dmerrick,

only one question:

you have the same search condition both in main and subsearch with the only difference of the strings condition in the subsearch, in other words: subsearch is a subset of the main search, 

so why don't you use only the subsearch as main?

source="logs" host="test" ("failed to subtract" OR "failed to add")
|  rex "^[(?<item>[\w.-]+)\].+"   
|  rex "^[(?<item>[\w.-]+)\]\s(?<message>.+)"
|  table _time, item, message

Then use always the index condition so you'll have faster searches.

Ciao.

Giuseppe

0 Karma
Get Updates on the Splunk Community!

Index This | Why did the turkey cross the road?

November 2025 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  &#x1f680; Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Feel the Splunk Love: Real Stories from Real Customers

Hello Splunk Community,    What’s the best part of hearing how our customers use Splunk? Easy: the positive ...