<?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 trigger second search based on first search where condition in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/556832#M158153</link>
    <description>&lt;P&gt;niketnilay is no longer with us so he will be unable to answer, you may wish to start a new thread as this thread is quite old...&lt;/P&gt;</description>
    <pubDate>Wed, 23 Jun 2021 09:02:44 GMT</pubDate>
    <dc:creator>gjanders</dc:creator>
    <dc:date>2021-06-23T09:02:44Z</dc:date>
    <item>
      <title>How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341908#M101304</link>
      <description>&lt;P&gt;I have a dbquery alert which will trigger when first query has more than 250 records then second search will trigger using |map command. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| dbxquery connection=conn query="select * from db" 
|where records&amp;gt;=250
|map maxsearches=1 search="dbxquery select query"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So, I am looking for an equivalent query that works for regular log events. I tried subsearch but it didn't worked well.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=* source=*  sourcetype=json 
| where count &amp;lt; 100 
| trigger second query when above condition agree 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;All I am trying is output the second query output results based on first query search condition.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 20:30:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341908#M101304</guid>
      <dc:creator>ibob0304</dc:creator>
      <dc:date>2018-01-31T20:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341909#M101305</link>
      <description>&lt;P&gt;In your example the timechart command only returns &lt;CODE&gt;count&lt;/CODE&gt; and &lt;CODE&gt;_time&lt;/CODE&gt; fields.  Is this all you want to use to perform your secondary search?&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;map&lt;/CODE&gt; is probably not the best solution for your needs.  Can you elaborate a bit regarding what your data looks like, and what type of data would indicate a need to find additional data?&lt;/P&gt;</description>
      <pubDate>Sun, 04 Feb 2018 02:20:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341909#M101305</guid>
      <dc:creator>micahkemp</dc:creator>
      <dc:date>2018-02-04T02:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341910#M101306</link>
      <description>&lt;P&gt;Sorry, changed the query. All I want is to show second query results based on first trigger condition &lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 03:04:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341910#M101306</guid>
      <dc:creator>ibob0304</dc:creator>
      <dc:date>2018-02-19T03:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341911#M101307</link>
      <description>&lt;P&gt;@ibob0304, Following is a run anywhere dashboard where count from query 1 is used to set the second query to be run. Please try out and confirm. I have added a text box to adjust the count in query 1 for testing purpose.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;form&amp;gt;
  &amp;lt;label&amp;gt;Run second search based on number of results&amp;lt;/label&amp;gt;
  &amp;lt;!-- First Search which returns count for where condition match--&amp;gt;
  &amp;lt;search&amp;gt;
    &amp;lt;query&amp;gt;| makeresults
    | eval resultCount="$tokCount$"
    | fields - _time
    &amp;lt;/query&amp;gt;
    &amp;lt;done&amp;gt;
      &amp;lt;!-- Second query is set only first query returns count greater than 100--&amp;gt;
      &amp;lt;condition match="$result.resultCount$&amp;gt;100"&amp;gt;
        &amp;lt;set token="tokSubQuery"&amp;gt;index=_internal sourcetype=splunkd log_level!=INFO earliest="$tokTime.earliest$" latest="$tokTime.latest$"| stats count by component | sort - count | head 5&amp;lt;/set&amp;gt;
      &amp;lt;/condition&amp;gt;
      &amp;lt;!-- If the count is less than 100 token is unset to stop search and hide the same --&amp;gt;
      &amp;lt;condition&amp;gt;
        &amp;lt;unset token="tokSubQuery"&amp;gt;&amp;lt;/unset&amp;gt;
      &amp;lt;/condition&amp;gt;
    &amp;lt;/done&amp;gt;
  &amp;lt;/search&amp;gt;
  &amp;lt;fieldset submitButton="false"&amp;gt;
    &amp;lt;input type="time" token="tokTime" searchWhenChanged="true"&amp;gt;
      &amp;lt;label&amp;gt;&amp;lt;/label&amp;gt;
      &amp;lt;default&amp;gt;
        &amp;lt;earliest&amp;gt;-24h@h&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;!-- Text box input is for testing purpose to generate count &amp;gt; 100 and &amp;lt; 100 --&amp;gt;
    &amp;lt;input type="text" token="tokCount" searchWhenChanged="true"&amp;gt;
      &amp;lt;label&amp;gt;Enter Limit Count for test&amp;lt;/label&amp;gt;
      &amp;lt;default&amp;gt;200&amp;lt;/default&amp;gt;
    &amp;lt;/input&amp;gt;
  &amp;lt;/fieldset&amp;gt;
  &amp;lt;row depends="$tokSubQuery$"&amp;gt;
    &amp;lt;panel&amp;gt;
      &amp;lt;table&amp;gt;
        &amp;lt;search&amp;gt;
          &amp;lt;query&amp;gt;$tokSubQuery$&amp;lt;/query&amp;gt;
        &amp;lt;/search&amp;gt;
      &amp;lt;/table&amp;gt;
    &amp;lt;/panel&amp;gt;
  &amp;lt;/row&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Feb 2018 04:31:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341911#M101307</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-02-19T04:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341912#M101308</link>
      <description>&lt;P&gt;Awesome! great example explained in a great way.&lt;/P&gt;

&lt;P&gt;Just a question, does the &lt;CODE&gt;&amp;lt;done&amp;gt;&lt;/CODE&gt;  tag follows a &lt;CODE&gt;&amp;lt;query&amp;gt;&lt;/CODE&gt;  tag? and the "result" in "$result.resultCount$" refers to the result of the recent query?&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 23:35:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341912#M101308</guid>
      <dc:creator>nabeel652</dc:creator>
      <dc:date>2018-02-20T23:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341913#M101309</link>
      <description>&lt;P&gt;@nabeel652, &lt;CODE&gt;&amp;lt;done&amp;gt;&lt;/CODE&gt; and &lt;CODE&gt;&amp;lt;progress&amp;gt;&lt;/CODE&gt; are two of &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/Viz/EventHandlerReference#Search_event_handlers"&gt;Search Event Handlers&lt;/A&gt; that allows access to default tokens &lt;CODE&gt;$result.&amp;lt;fieldName&amp;gt;$&lt;/CODE&gt; (only single value or first row value). Refer to the documentation for details. &lt;/P&gt;

&lt;P&gt;The &lt;CODE&gt;&amp;lt;done&amp;gt;&lt;/CODE&gt; and &lt;CODE&gt;&amp;lt;query&amp;gt;&lt;/CODE&gt; tags should be inside &lt;CODE&gt;&amp;lt;search&amp;gt;&lt;/CODE&gt;. But I dont think sequence is of importance. The &lt;CODE&gt;&amp;lt;done&amp;gt;&lt;/CODE&gt; search event handler accesses fields from the query in the same search.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2018 06:30:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341913#M101309</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-02-21T06:30:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341914#M101310</link>
      <description>&lt;P&gt;Thanks @niketnilay for the explanation. &lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 01:05:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341914#M101310</guid>
      <dc:creator>nabeel652</dc:creator>
      <dc:date>2018-02-22T01:05:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341915#M101311</link>
      <description>&lt;P&gt;Thanks Niket, I upvoted the answer. But, do you know if this can be done using ad- hoc search instead of dashboard ? &lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 01:25:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341915#M101311</guid>
      <dc:creator>ibob0304</dc:creator>
      <dc:date>2018-02-22T01:25:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341916#M101312</link>
      <description>&lt;P&gt;@nabeel652, I see you deleted your prior comment, which is fine. But, I would like to let you know that if there is no response to the answer then the person who posted might waited for long time looking for an answer and gave up after some days. And I am looking for an adhoc search. If you know then let me know the answer and I will appreciate it or else dont use words like shame, and this is not stackoverflow&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 01:42:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341916#M101312</guid>
      <dc:creator>ibob0304</dc:creator>
      <dc:date>2018-02-22T01:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341917#M101313</link>
      <description>&lt;P&gt;There was no point discussing this when I deleted my comments. Period&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 01:50:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341917#M101313</guid>
      <dc:creator>nabeel652</dc:creator>
      <dc:date>2018-02-22T01:50:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341918#M101314</link>
      <description>&lt;P&gt;As long as you are using count, index, source and sourcetype in the root search ONLY; you can take advantage of tstats instead of stats/timechart etc.&lt;/P&gt;

&lt;P&gt;Like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | tstats count where index=* AND sourcetype=json | search count &amp;gt; 100 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Or you could even use the metadata or eventstats commands.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 13:22:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341918#M101314</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2018-02-22T13:22:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341919#M101315</link>
      <description>&lt;P&gt;@ibob0304, you can use the map command to set the token in the main search only if &lt;CODE&gt;count is &amp;gt;250&lt;/CODE&gt;. This way the second search will fail if the token is not set. Following is a run any where example.&lt;/P&gt;

&lt;P&gt;PS: Instead of    &lt;CODE&gt;| makeresults     | eval testCount=300&lt;/CODE&gt; you will have your first query that returns count. I have used this for testing so that you can change 300 to 50 to see the search fail.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval testCount=300
| eval tokenForSecondSearch=case(testCount&amp;gt;=250,"true")
| map search="search index=_internal sourcetype=splunkd log_level!=INFO| stats count by component| sort - count| head 10| eval tokenForSecondSearch=\"$tokenForSecondSearch$\"| fields - tokenForSecondSearch"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Refer to &lt;CODE&gt;map&lt;/CODE&gt; command documentation: &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map"&gt;http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 17:35:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341919#M101315</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-02-22T17:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341920#M101316</link>
      <description>&lt;P&gt;Thanks Niket&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 19:34:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341920#M101316</guid>
      <dc:creator>ibob0304</dc:creator>
      <dc:date>2018-02-22T19:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341921#M101317</link>
      <description>&lt;P&gt;@ibob0304 anytime &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I am glad you found it working &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 16:58:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/341921#M101317</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2018-02-23T16:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/556616#M158084</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/201110"&gt;@niketn&lt;/a&gt;&amp;nbsp;, Good Afternoon! sorry for asking you , but I have an issue in my dashboard in triggering a second search if the count of result of my first search&amp;nbsp; will be&amp;nbsp; != 0 .&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was passing by and I saw this post where you explain how to do that, but in fact,&amp;nbsp;&amp;nbsp;in the solution... apparently once we set the token, the SECOND SEARCH &lt;STRONG&gt;will not run a second time if the FIRST SEARCH runs again&lt;/STRONG&gt;.&amp;nbsp; Do you know How Can I get this around?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jun 2021 21:46:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/556616#M158084</guid>
      <dc:creator>Luciana</dc:creator>
      <dc:date>2021-06-23T21:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to trigger second search based on first search where condition</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/556832#M158153</link>
      <description>&lt;P&gt;niketnilay is no longer with us so he will be unable to answer, you may wish to start a new thread as this thread is quite old...&lt;/P&gt;</description>
      <pubDate>Wed, 23 Jun 2021 09:02:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-trigger-second-search-based-on-first-search-where/m-p/556832#M158153</guid>
      <dc:creator>gjanders</dc:creator>
      <dc:date>2021-06-23T09:02:44Z</dc:date>
    </item>
  </channel>
</rss>

