<?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: Convert string to command, for dynamic union search in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Convert-string-to-command-for-dynamic-union-search/m-p/482279#M135102</link>
    <description>&lt;P&gt;While this does function, it causes the search to be subject to the subsearch limit, e.g.&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;info : [map]: [map]: Search Processor: Subsearch produced 76673 results, truncating to maxout 10000.&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;This doesn't occur when run as the raw command. The only way around this (that I know of) would be global to my org, so not an option. (The above number is the search results at the moment for 1 day's worth of data)&lt;/P&gt;

&lt;P&gt;Thanks for the suggestion though, it was worth trying. I do find it interesting that it works only when you wrap the map inside another map though.&lt;/P&gt;</description>
    <pubDate>Tue, 17 Sep 2019 04:17:17 GMT</pubDate>
    <dc:creator>jlr</dc:creator>
    <dc:date>2019-09-17T04:17:17Z</dc:date>
    <item>
      <title>Convert string to command, for dynamic union search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Convert-string-to-command-for-dynamic-union-search/m-p/482277#M135100</link>
      <description>&lt;P&gt;TL;DR - &lt;STRONG&gt;Is there a way (without custom scripts or commands) to run a command from a string in the format of a &lt;CODE&gt;union&lt;/CODE&gt; that contains a dynamic number of subsearches?&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;I have quite a few heavy dashboards that get hit by many users, quite frequently. In order to save both processing power and user time, I've been caching search results (such as in a  &lt;CODE&gt;loadjob&lt;/CODE&gt; or  &lt;CODE&gt;outputlookup&lt;/CODE&gt;) and stitching them together dynamically. These dashboards all use a single  &lt;CODE&gt;savedsearch&lt;/CODE&gt;, and an instance of the search will cache data hourly, usually saving a day's worth of back-data. The dashboards then take the saved data and combine it with a call of the same search from the start of the hour until now(), and it's significantly faster.&lt;/P&gt;

&lt;P&gt;I'm trying to create a macro that can do this intelligently so I don't have to re-build it per dashboard/savedsearch etc. I've created a search that can generate a string that, when used as a command, searches fine. However, I've been unable to find a way to get it to actually run - as far as I or anyone in my team can tell, anything that's a string can only be run as a  &lt;CODE&gt;search&lt;/CODE&gt;, which is insufficient for my needs as it ends up literally searching for a string talking about unions.&lt;/P&gt;

&lt;P&gt;The example string that's being generated by the query below reads nicely as: (though NB, the output string could just as easily be a single line with no  &lt;CODE&gt;union&lt;/CODE&gt; or  &lt;CODE&gt;sort&lt;/CODE&gt;, or a  &lt;CODE&gt;union&lt;/CODE&gt; of two subsearches)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| union
  [ | savedsearch my_savedsearch earliest=1568343703.000000 latest=1568462400.000000 ]
  [ | inputlookup mycsv.csv  ]
  [ | savedsearch my_savedsearch earliest=1568602800.000000 latest=1568602843.000000 ]
| sort - _time
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The generating query has an initial  &lt;CODE&gt;eval&lt;/CODE&gt; statement that I intended to use when moving it into a macro, which is stubbing the various args. Modifying these will dynamically change the number of subsearches/times (as the user might be requesting a time period relative to the cached data that is before, after, during, or some combination of each). Variants of this have been attempted using things like  &lt;CODE&gt;append&lt;/CODE&gt;,  &lt;CODE&gt;appendpipe&lt;/CODE&gt;,  &lt;CODE&gt;multisearch&lt;/CODE&gt;, but all seem to fail due to various reasons except the above (circular dependencies, streaming commands etc). The string generated, when run against a valid dataset, works. When the command is passed as the search itself using subsearches/&lt;CODE&gt;return&lt;/CODE&gt; etc all fail - as far as we can tell, it's trying to run it as a  &lt;CODE&gt;search&lt;/CODE&gt;, or otherwise is unable to handle it.&lt;/P&gt;

&lt;P&gt;Generating query: (replace contents in the initial eval for different dataset examples)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| eval
  timerange_string="| inputlookup mycsv.csv",
  timerange_earliest=relative_time(now(), "-1d@d"),
  timerange_latest=relative_time(now(), "@h"),
  time_string="| savedsearch my_savedsearch",
  time_earliest=relative_time(now(), "-3d"),
  time_latest=relative_time(now(), "-1m")
| eval
  earliest_period=case(
    time_earliest &amp;lt; timerange_earliest,-1,
    (timerange_earliest &amp;lt;= time_earliest AND time_earliest &amp;lt;= timerange_latest),0,
    timerange_latest &amp;lt; time_earliest,1,
    true()=true(),"null"
  ),
  latest_period=case(
    time_latest &amp;lt; timerange_earliest,-1,
    (timerange_earliest &amp;lt;= time_latest AND time_latest &amp;lt;= timerange_latest),0,
    timerange_latest &amp;lt; time_latest,1,
    true()=true(),"null"
  )
| fields - _time
| eval
  union_string=if(earliest_period!=latest_period,"| union [ ",""),
  search=
     if(earliest_period=0 OR latest_period=0,"| search","")
    .if(earliest_period=0," earliest=".time_earliest,"")
    .if(latest_period=0," latest=".time_latest,""),
  inputlookup=timerange_string." ".search,
  inputlookup_string=if(earliest_period*latest_period&amp;lt;1,inputlookup,""),
  ss1=time_string." earliest=".time_earliest." latest=".if(latest_period=-1,time_latest,timerange_earliest),
  ss1_string=if(earliest_period=-1,ss1,""),
  ss2=time_string." earliest=".if(earliest_period=1,time_earliest,timerange_latest)." latest=".time_latest,
  ss2_string=if(latest_period=1,ss2,""),
  sort_string=if(earliest_period!=latest_period," ] | sort - _time","")
| eval search=union_string.ss1_string.if(earliest_period=-1 AND latest_period!=-1," ] [ ","").inputlookup_string.if(latest_period=1 AND earliest_period!=1," ] [ ","").ss2_string.sort_string
| fields search
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;There are some similar questions (don't have enough karma to post links), but none directly tackle this use case, as  &lt;CODE&gt;map&lt;/CODE&gt; doesn't seem to be a viable solution here (triggers circular dependencies) and the general advice given with  &lt;CODE&gt;return&lt;/CODE&gt; doesn't work (as mentioned above).&lt;/P&gt;

&lt;P&gt;Is there an obvious option I'm missing here? The next step I'd be looking at would be writing a custom command, which is less portable than a macro. I'll be working on that at some point, but if there's a way to do this with a macro using a basic Splunk query, it'd be my preference.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 03:26:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Convert-string-to-command-for-dynamic-union-search/m-p/482277#M135100</guid>
      <dc:creator>jlr</dc:creator>
      <dc:date>2019-09-16T03:26:54Z</dc:date>
    </item>
    <item>
      <title>Re: Convert string to command, for dynamic union search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Convert-string-to-command-for-dynamic-union-search/m-p/482278#M135101</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;a little bit ugly but it could work :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;`your_SPL`
| map search="|makeresults 1 | map search=$search$"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;your savedsearch has to have 2 arguments :&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;earliest&lt;/LI&gt;
&lt;LI&gt;latest&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;==&amp;gt;  example: index=_* earliest=$earliest$ latest=$latest$&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 14:35:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Convert-string-to-command-for-dynamic-union-search/m-p/482278#M135101</guid>
      <dc:creator>thomasroulet</dc:creator>
      <dc:date>2019-09-16T14:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: Convert string to command, for dynamic union search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Convert-string-to-command-for-dynamic-union-search/m-p/482279#M135102</link>
      <description>&lt;P&gt;While this does function, it causes the search to be subject to the subsearch limit, e.g.&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;info : [map]: [map]: Search Processor: Subsearch produced 76673 results, truncating to maxout 10000.&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;This doesn't occur when run as the raw command. The only way around this (that I know of) would be global to my org, so not an option. (The above number is the search results at the moment for 1 day's worth of data)&lt;/P&gt;

&lt;P&gt;Thanks for the suggestion though, it was worth trying. I do find it interesting that it works only when you wrap the map inside another map though.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 04:17:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Convert-string-to-command-for-dynamic-union-search/m-p/482279#M135102</guid>
      <dc:creator>jlr</dc:creator>
      <dc:date>2019-09-17T04:17:17Z</dc:date>
    </item>
  </channel>
</rss>

