<?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: Subsearch for negation in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290631#M87827</link>
    <description>&lt;P&gt;Thanks. But i want to use subsearch only.  Any suggestion?&lt;/P&gt;</description>
    <pubDate>Fri, 12 May 2017 15:15:18 GMT</pubDate>
    <dc:creator>satishsdange</dc:creator>
    <dc:date>2017-05-12T15:15:18Z</dc:date>
    <item>
      <title>Subsearch for negation</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290629#M87825</link>
      <description>&lt;P&gt;I am trying to write a subsearch which will negate few days/hours from results considering event count. But below search is not giving me desired result. Could someone please point me the right search?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal sourcetype="splunkd" NOT [search index=_internal sourcetype="splunkd" | timechart count | search count &amp;gt; 10] | timechart count
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 May 2017 14:52:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290629#M87825</guid>
      <dc:creator>satishsdange</dc:creator>
      <dc:date>2017-05-12T14:52:47Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch for negation</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290630#M87826</link>
      <description>&lt;P&gt;Why not just run this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal sourcetype="splunkd" | timechart count | WHERE count &amp;lt;= 10
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 May 2017 15:10:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290630#M87826</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-05-12T15:10:10Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch for negation</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290631#M87827</link>
      <description>&lt;P&gt;Thanks. But i want to use subsearch only.  Any suggestion?&lt;/P&gt;</description>
      <pubDate>Fri, 12 May 2017 15:15:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290631#M87827</guid>
      <dc:creator>satishsdange</dc:creator>
      <dc:date>2017-05-12T15:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch for negation</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290632#M87828</link>
      <description>&lt;P&gt;Since the timechart will use some default span based on time range (as you've not specified anywhere), using subsearch will be a complex solution. If your requirement is met using just a single-simple search, any specific reason you want to go with subsearch (you'll be running the same search twice)?&lt;/P&gt;</description>
      <pubDate>Fri, 12 May 2017 16:14:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290632#M87828</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-05-12T16:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch for negation</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290633#M87829</link>
      <description>&lt;P&gt;Doing it the way somesoni suggested is much better - why do you insist on using subsearch if I may ask?&lt;/P&gt;</description>
      <pubDate>Fri, 12 May 2017 17:49:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290633#M87829</guid>
      <dc:creator>ckunath</dc:creator>
      <dc:date>2017-05-12T17:49:26Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch for negation</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290634#M87830</link>
      <description>&lt;P&gt;I have no idea why you are trying to do what you are trying to do, but it really won't work that way.&lt;/P&gt;

&lt;P&gt;First, your subsearch is scanning the &lt;CODE&gt;_internal&lt;/CODE&gt; index for that &lt;CODE&gt;sourcetype&lt;/CODE&gt;, then creating a table that contains only &lt;CODE&gt;_time&lt;/CODE&gt; and &lt;CODE&gt;count&lt;/CODE&gt;.  The field &lt;CODE&gt;_time&lt;/CODE&gt; that is returned by &lt;CODE&gt;timechart&lt;/CODE&gt; will have been binned into buckets of whatever the default duration is for &lt;CODE&gt;timechart&lt;/CODE&gt;, based on the time period that you are running the search over, so very few transactions would possibly match the exact &lt;CODE&gt;_time&lt;/CODE&gt; values returned. The field &lt;CODE&gt;count&lt;/CODE&gt; does not even exist on the &lt;CODE&gt;_internal&lt;/CODE&gt; index, so nothing &lt;STRONG&gt;ever&lt;/STRONG&gt; will be returned.  &lt;/P&gt;

&lt;P&gt;Then even if that worked, you are then asking splunk to go back and add it all up again... getting no more data than the first time, and no different answer.&lt;/P&gt;

&lt;P&gt;Now, understand, anything CAN be done.  We could code a way to take the results of a subsearch and collect the data off the &lt;CODE&gt;_internal&lt;/CODE&gt; index (preferably using &lt;CODE&gt;tstats&lt;/CODE&gt; rather than a full scan) and use that to populate the search with time inclusion criteria.  For some reason, &lt;CODE&gt;tstats&lt;/CODE&gt; refuses to work correctly with complex &lt;CODE&gt;_time&lt;/CODE&gt; tests, so you'd feed it back to your original search like this...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal sourcetype="splunkd" 
    [
    | tstats count as spancount where index=_internal AND sourcetype="splunkd" BY _time span=5m 
    | eval spanstart=_time, spanend=_time+300
    | eval highlow=if(spancount&amp;gt;10000,"high","low")
    | streamstats current=f last(highlow) as prevhighlow
    | eval spanchange=if(highlow=coalesce(prevhighlow,"none"),0,1)
    | streamstats sum(spanchange) as spannumber
    | stats sum(spancount) as spancount count as periods min(spanstart) as spanstart, max(spanend) as spanend, min(highlow) as highlow by spannumber
    | where highlow="low"
    | table spanstart spanend
    | format
    | rex mode=sed field=search "s/spanend=\"/_time&amp;lt;/g"
    | rex mode=sed field=search "s/spanstart=\"/_time&amp;gt;=/g"
    | rex mode=sed field=search "s/\"//g"
    | table search
    ]
| bin _time span=5m 
| stats count by _time
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;All that mumbo-jumbo between &lt;CODE&gt;| table spanstart spanend&lt;/CODE&gt; and  &lt;CODE&gt;| table search&lt;/CODE&gt; is turning our paired _time values into a search format that reads like...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;( ( _time&amp;lt;1494608100 AND _time&amp;gt;=1494607800 ) OR ( _time&amp;lt;1494609000 AND _time&amp;gt;=1494608400 )  ... )
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Note that in my &lt;CODE&gt;highlow&lt;/CODE&gt; test I I used 10000 rather than 10, due to the traffic on my index. &lt;/P&gt;

&lt;P&gt;...and after all that work, it gets the exact same results as somesoni2's&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=_internal sourcetype="splunkd" | timechart span=5m count | WHERE count &amp;lt;= 10000
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 May 2017 17:49:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-for-negation/m-p/290634#M87830</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-05-12T17:49:55Z</dc:date>
    </item>
  </channel>
</rss>

