<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to generate a conditional search based on time? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345645#M102407</link>
    <description>&lt;P&gt;OK, see my other answer where I turn it inside out and use &lt;CODE&gt;map&lt;/CODE&gt; instead of a nested subsearch.&lt;/P&gt;</description>
    <pubDate>Mon, 13 Mar 2017 21:10:26 GMT</pubDate>
    <dc:creator>woodcock</dc:creator>
    <dc:date>2017-03-13T21:10:26Z</dc:date>
    <item>
      <title>How to generate a conditional search based on time?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345638#M102400</link>
      <description>&lt;P&gt;I need to figure out a way to execute one of two different search strings based on the time range in a first search.&lt;/P&gt;

&lt;P&gt;If a sample is more than 2 weeks old, the associated lab data is stored in a summary index. [search1]&lt;BR /&gt;
If a sample is less than 2 weeks old, the lab data will need to be live-calculated via a much more complicated search string (includes subsearch, join, dedup). [search2]&lt;/P&gt;

&lt;P&gt;I tried to use &lt;CODE&gt;multisearch&lt;/CODE&gt;, but since search2 contains non-streaming commands ( &lt;CODE&gt;join&lt;/CODE&gt; &amp;amp; &lt;CODE&gt;dedup&lt;/CODE&gt;), that didn't work.&lt;/P&gt;

&lt;P&gt;Search1 and Search2 both return fields sample_name and sample_value, so not a single value. My understanding is that this prevents me from using eval/if based on _time or a time token from my search.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;base search
| join sample_name
[ run either search1 or search2]
| table sample_name sample_value
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 13:10:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345638#M102400</guid>
      <dc:creator>mstark31</dc:creator>
      <dc:date>2020-09-29T13:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate a conditional search based on time?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345639#M102401</link>
      <description>&lt;P&gt;@mstark31, here is one way to do it... &lt;/P&gt;

&lt;P&gt;1) Whatever is the timerange in the first search, you can define a Time input for the same. I have used &lt;STRONG&gt;tok_time&lt;/STRONG&gt; as time in my example.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  &amp;lt;fieldset submitButton="false"&amp;gt;
    &amp;lt;input type="time" token="tok_time" searchWhenChanged="true"&amp;gt;
      &amp;lt;label&amp;gt;Select Time&amp;lt;/label&amp;gt;
      &amp;lt;default&amp;gt;
        &amp;lt;earliest&amp;gt;-15m&amp;lt;/earliest&amp;gt;
        &amp;lt;latest&amp;gt;now&amp;lt;/latest&amp;gt;
      &amp;lt;/default&amp;gt;
    &amp;lt;/input&amp;gt;
  &amp;lt;/fieldset&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;2) Run a dumy search to compare selected Earliest and Latest Time and set the index/environment name as summary or realtime (ideally to be passed to a macro to set criteria of the base search etc.)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  &amp;lt;!-- Dummy Search to set index name for main query based on time range selected --&amp;gt;
  &amp;lt;search&amp;gt;
    &amp;lt;query&amp;gt;| makeresults
  | eval now=_time&amp;lt;/query&amp;gt;
      &amp;lt;earliest&amp;gt;$tok_time.earliest$&amp;lt;/earliest&amp;gt;
      &amp;lt;latest&amp;gt;$tok_time.latest$&amp;lt;/latest&amp;gt;
    &amp;lt;sampleRatio&amp;gt;1&amp;lt;/sampleRatio&amp;gt;
    &amp;lt;preview&amp;gt;
          &amp;lt;!-- Compare Todays Date with Earliest Time in the Time Range and 
         if greater than 2 week (2*7*24*60*60=1209600 seconds) set Summary 
         else set Realtime index--&amp;gt;
          &amp;lt;condition match="$result.now$-$job.searchEarliestTime$&amp;gt;1209600"&amp;gt;
            &amp;lt;set token="selectedIndex"&amp;gt;summary&amp;lt;/set&amp;gt;
          &amp;lt;/condition&amp;gt;
          &amp;lt;condition&amp;gt;
            &amp;lt;set token="selectedIndex"&amp;gt;realtime&amp;lt;/set&amp;gt;
          &amp;lt;/condition&amp;gt;
    &amp;lt;/preview&amp;gt;
&amp;lt;/search&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Finally use the $selectedIndex$ token to set index to summary. Ideally through a macro which takes string values for environment as a paramter, this we you can set other criteria for base search if you want like summary index will have sourcetype as stash.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;!-- Use $selectedIndex$ token to set index in main search--&amp;gt;
&amp;lt;search&amp;gt;
  &amp;lt;query&amp;gt; base search
  | join sample_name
    [ `setindex($selectedIndex$)`
      |&amp;lt;remaining Search&amp;gt; ]
  | table sample_name sample_value
  &amp;lt;/query&amp;gt;
&amp;lt;/search&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;PS: One more condition you can try out is to see if Earliest and Latest time spans for more than two weeks then go for Summary Index &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; &amp;lt;condition match="$job.searchLatestTime$-$job.searchEarliestTime$&amp;gt;1209600"&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Mar 2017 20:04:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345639#M102401</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-03-10T20:04:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate a conditional search based on time?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345640#M102402</link>
      <description>&lt;P&gt;Here is how I'll try&lt;/P&gt;

&lt;P&gt;Step1) create a saved search for each of search1 and search2, full query that you want to execute on the subsearch. Just a simple saved search. Say the names are search1 and search2 itself.&lt;/P&gt;

&lt;P&gt;Step2) update your query like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;base search
 | join sample_name
 [ | savedsearch [| gentimes start=-1 | addinfo | eval search=if((info_max_time-info_min_time)&amp;gt;14*86400"search1","search2") | table search ] nosubstitution=t]
 | table sample_name sample_value
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The addinfo command adds the fields info_min_time and info_max_time which are the earliest and latest value of the time range that you've selected. If will run subsearch &lt;CODE&gt;| savedsearch search1&lt;/CODE&gt; if the selected time range is more than 2 weeks/14 days or will run &lt;CODE&gt;| savedsearch search2&lt;/CODE&gt; otherwise.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 13:12:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345640#M102402</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2020-09-29T13:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate a conditional search based on time?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345641#M102403</link>
      <description>&lt;P&gt;Like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;base search
| join sample_name
[[|noop|stats count AS search | addinfo | eval search=if(info_max_time&amp;lt;relative_time(now(), "-14d@d"), "Search1 String Here", "Search2 String Here")]]
| table sample_name sample_value
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Mar 2017 21:02:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345641#M102403</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-03-10T21:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate a conditional search based on time?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345642#M102404</link>
      <description>&lt;P&gt;The question says "more than 2 weeks old" is the criteria, so you are probably needing something like &lt;CODE&gt;if(info_max_time &amp;lt;= info_search_time -14*86400,...&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 23:18:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345642#M102404</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-03-10T23:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate a conditional search based on time?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345643#M102405</link>
      <description>&lt;P&gt;Are there any restrictions with using &lt;CODE&gt;noop&lt;/CODE&gt; in a subsearch? &lt;BR /&gt;
I tried this but got the error: &lt;BR /&gt;
&lt;EM&gt;Error in 'SearchParser': Subsearches are only valid as arguments to commands. Error at position '195' of search query 'search index=...{snipped} {errorcontext = [ [|noop }'&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 20:52:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345643#M102405</guid>
      <dc:creator>mstark31</dc:creator>
      <dc:date>2017-03-13T20:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate a conditional search based on time?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345644#M102406</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|noop|stats count AS search | addinfo | eval search=if(info_max_time&amp;lt;relative_time(now(), "-14d@d"), "Search1 String Here", "Search2 String Here") | map search="search  base search
| join sample_name [ $search$ ]
| table sample_name sample_value"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Mar 2017 21:09:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345644#M102406</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-03-13T21:09:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate a conditional search based on time?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345645#M102407</link>
      <description>&lt;P&gt;OK, see my other answer where I turn it inside out and use &lt;CODE&gt;map&lt;/CODE&gt; instead of a nested subsearch.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 21:10:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345645#M102407</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-03-13T21:10:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate a conditional search based on time?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345646#M102408</link>
      <description>&lt;P&gt;Thank you everyone for your answers. &lt;BR /&gt;
I ended up going in a slightly different direction with my solution, but I've learned so many new things based on your collective input.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 13:28:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345646#M102408</guid>
      <dc:creator>mstark31</dc:creator>
      <dc:date>2017-03-17T13:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to generate a conditional search based on time?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345647#M102409</link>
      <description>&lt;P&gt;So up-vote the useful answers and then pick the best one (or yours, this one) and click &lt;CODE&gt;Accept&lt;/CODE&gt; to close the question.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 18:59:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-generate-a-conditional-search-based-on-time/m-p/345647#M102409</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-03-17T18:59:23Z</dc:date>
    </item>
  </channel>
</rss>

