Splunk Search

How to run the main search only if the subsearch returns true?

prakashbhanu407
New Member

I have to run the Main search only on the last working day of the month, and I got to a search that should work, but the main search is not taking the boolean response of subsearch as a condition to process the rest of the search.

Here is my search:

index=MyIndex queryType="ts" filename=Pharmacy*.JSON [| gentimes start=-1 | eval lastDOM=relative_time(now(),"+1mon@mon-1d") | eval lastWDOM=case(strftime(lastDOM,"%a")="Sun", relative_time(lastDOM,"-2d@d"),strftime(lastDOM,"%a")="Sat", relative_time(lastDOM,"-1d@d"),1=1,lastDOM) | eval query=if(relative_time(now(),"@d")=lastWDOM,1=1,1=2) | table query] | table _time,accountName,filename,filesize 
0 Karma

somesoni2
Revered Legend

Try like this

index=MyIndex queryType="ts" filename=Pharmacy*.JSON [| gentimes start=-1 | eval lastDOM=relative_time(now(),"+1mon@mon-1d") | eval lastWDOM=case(strftime(lastDOM,"%a")="Sun", relative_time(lastDOM,"-2d@d"),strftime(lastDOM,"%a")="Sat", relative_time(lastDOM,"-1d@d"),1=1,lastDOM) | eval index=if(relative_time(now(),"@d")=lastWDOM,"MyIndex","YouWillNotFindThisIndex") | table index] | table _time,accountName,filename,filesize

Basically, if the subsearch condition is false, it'll return a value for field (metadata field used for better performance) index which may never exist so main search will not run. It returns the same index name as main search in case the condition is true.

javiergn
Super Champion

You can use map to simulate a conditional execution:

| gentimes start=-1
| eval lastDOM=relative_time(now(),"+1mon@mon-1d") 
| eval lastWDOM=case(strftime(lastDOM,"%a")="Sun", relative_time(lastDOM,"-2d@d"), strftime(lastDOM,"%a")="Sat", relative_time(lastDOM,"-1d@d"),1=1,lastDOM) 
| eval n_searches=if(relative_time(now(),"@d")=lastWDOM, 1, 0)
| where n_searches > 0
| map maxsearches=1 search="
    | search index=MyIndex queryType=\"ts\" filename=Pharmacy*.JSON 
    | table _time,accountName,filename,filesize
"

ibob0304
Communicator

I couldn't thank much on using map, it solved my long standing issue.. thanks alot..

0 Karma

woodcock
Esteemed Legend

You cannot assign boolean values to a field. Try this:

index=MyIndex queryType="ts" filename=Pharmacy*.JSON earliest=[| gentimes start=-1 | addinfo | eval lastDOM=relative_time(now(),"+1mon@mon-1d") | eval lastWDOM=case(strftime(lastDOM,"%a")="Sun", relative_time(lastDOM,"-2d@d"),strftime(lastDOM,"%a")="Sat", relative_time(lastDOM,"-1d@d"),true(),lastDOM) | eval earliest=if(relative_time(now(),"@d")=lastWDOM,"Do Not Run",info_min_time) | return $earliest] | table _time accountName filename filesize

This will cause the search to crash if it does not meet your criteria.

0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...