<?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: Is my map search being parsed before variable substitution? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Is-my-map-search-being-parsed-before-variable-substitution/m-p/212769#M62362</link>
    <description>&lt;P&gt;$summary_search$ would be the name of a populating search.  $summary_selector$ would be part of a search string used to select data from the summary for forecasting.  The use case is something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| localop
| rest /services/saved/searches 
| search title="XYZ - Predict - ATM volume" 
| rex field=search "search_name=\"(?&amp;lt;summary_search&amp;gt;[^\"]+)\" (?&amp;lt;summary_selector&amp;gt;[^\|]+)" 
| rename title as forecast_search 
| map search="search index=summary search_name="$forecast_search$" OR (search_name="$summary_search$" $summary_selector$) 
   | timechart span=10m sum(low) as low, sum(pred) as forecast, sum(high) as high, sum(count) as actual "
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;where the "* - Predict - *" search is of the form:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=summary search_name="XYZ - Summary - Count by Type and Action" MessageType=Atm
| timechart span=10m sum(count) as count
| `predict5w(count, 90, +1d, 1)`
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The basic idea is a form that lets the user select a forecasting (* - Predict - *) search, gets the underlying summary search and selection criteria, and displays the actual (summery) vs forecast data for that metric.  In this example, summary_selector="MessageType=Atm " so the search fails.  If I adjust the regex so summary_selector="MessageType=Atm" it works, but only because MessageType=Atm is functionally equivalent to "MessageType=Atm" for this particular search.  In the case where summary_selector="MessageType=Atm Action=Denied", the search would fail since "MessageType=Atm Action=Denied" is not equivalent to MessageType=Atm Action=Denied.&lt;/P&gt;

&lt;P&gt;I figured out one possible workaround using a where/searchmatch clause in the map search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| localop 
| rest /services/saved/searches 
| search title="XYZ - Predict - ATM volume" 
| rex field=search "search_name=\"(?&amp;lt;summary_search&amp;gt;[^\"]+)\" (?&amp;lt;summary_selector&amp;gt;[^\|]+)" 
| rename title as forecast_search
| eval filter="search_name=\"".forecast_search."\" OR (search_name=\"".summary_search."\" ".summary_selector.")"
| map search="search index=summary search_name=\"$forecast_search$\" OR search_name=\"$summary_search$\" | where searchmatch($filter$) | timechart span=10m sum(low) as low, sum(pred) as forecast, sum(high) as high, sum(count) as actual "
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I'm still checking edge cases, but it seems to work so far.  Not as efficient as it could be though. &lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 08:18:18 GMT</pubDate>
    <dc:creator>mlf</dc:creator>
    <dc:date>2020-09-29T08:18:18Z</dc:date>
    <item>
      <title>Is my map search being parsed before variable substitution?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-my-map-search-being-parsed-before-variable-substitution/m-p/212767#M62360</link>
      <description>&lt;P&gt;Having issues with the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| map search="search index=summary search_name=\"$summary_search$\" $summary_selector$"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It returns the expected results when $summary_selector$ evaluates to a simple string, "Foo" or "Bar=Foo", but silently fails in cases where it contains a space, or multiple space-separated terms, "Bar=Foo " or "Bar=Foo Baz=xxx".  This leads me to believe that the map search is being parsed before variable substitution and $summary_selector$ is being forced to a string literal.  I.e. it seems the  search is evaluated as:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search index=summary search_name="Mysearch" "Bar=Foo Baz=xxx"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;rather than the intended:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search index=summary search_name="Mysearch" Bar=Foo Baz=xxx
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Can anyone confirm the parsing/substitution order or suggest a workaround?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:18:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-my-map-search-being-parsed-before-variable-substitution/m-p/212767#M62360</guid>
      <dc:creator>mlf</dc:creator>
      <dc:date>2020-09-29T08:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: Is my map search being parsed before variable substitution?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-my-map-search-being-parsed-before-variable-substitution/m-p/212768#M62361</link>
      <description>&lt;P&gt;Are those variables field names from the previous search command? With Map $field$ should evaluate to the value for field  for each input to the map command. &lt;/P&gt;

&lt;P&gt;So if you summary_selector is a multivalue field, then split it first and then change your map. Something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex yourMVfield="Foo=(?&amp;lt;Foo&amp;gt;foo?)Baz=(?&amp;lt;Baz&amp;gt;baz?)"  | map search="search index=summary search_name=$summary_search$ Foo=$Foo$ Baz=$Baz$"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;See 'Usage' at  &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.3.2/SearchReference/Map"&gt;http://docs.splunk.com/Documentation/Splunk/6.3.2/SearchReference/Map&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Also, theres a good chance you don't need map at all depending on what your base search is. &lt;/P&gt;</description>
      <pubDate>Mon, 04 Jan 2016 14:38:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-my-map-search-being-parsed-before-variable-substitution/m-p/212768#M62361</guid>
      <dc:creator>jplumsdaine22</dc:creator>
      <dc:date>2016-01-04T14:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: Is my map search being parsed before variable substitution?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-my-map-search-being-parsed-before-variable-substitution/m-p/212769#M62362</link>
      <description>&lt;P&gt;$summary_search$ would be the name of a populating search.  $summary_selector$ would be part of a search string used to select data from the summary for forecasting.  The use case is something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| localop
| rest /services/saved/searches 
| search title="XYZ - Predict - ATM volume" 
| rex field=search "search_name=\"(?&amp;lt;summary_search&amp;gt;[^\"]+)\" (?&amp;lt;summary_selector&amp;gt;[^\|]+)" 
| rename title as forecast_search 
| map search="search index=summary search_name="$forecast_search$" OR (search_name="$summary_search$" $summary_selector$) 
   | timechart span=10m sum(low) as low, sum(pred) as forecast, sum(high) as high, sum(count) as actual "
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;where the "* - Predict - *" search is of the form:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=summary search_name="XYZ - Summary - Count by Type and Action" MessageType=Atm
| timechart span=10m sum(count) as count
| `predict5w(count, 90, +1d, 1)`
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The basic idea is a form that lets the user select a forecasting (* - Predict - *) search, gets the underlying summary search and selection criteria, and displays the actual (summery) vs forecast data for that metric.  In this example, summary_selector="MessageType=Atm " so the search fails.  If I adjust the regex so summary_selector="MessageType=Atm" it works, but only because MessageType=Atm is functionally equivalent to "MessageType=Atm" for this particular search.  In the case where summary_selector="MessageType=Atm Action=Denied", the search would fail since "MessageType=Atm Action=Denied" is not equivalent to MessageType=Atm Action=Denied.&lt;/P&gt;

&lt;P&gt;I figured out one possible workaround using a where/searchmatch clause in the map search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| localop 
| rest /services/saved/searches 
| search title="XYZ - Predict - ATM volume" 
| rex field=search "search_name=\"(?&amp;lt;summary_search&amp;gt;[^\"]+)\" (?&amp;lt;summary_selector&amp;gt;[^\|]+)" 
| rename title as forecast_search
| eval filter="search_name=\"".forecast_search."\" OR (search_name=\"".summary_search."\" ".summary_selector.")"
| map search="search index=summary search_name=\"$forecast_search$\" OR search_name=\"$summary_search$\" | where searchmatch($filter$) | timechart span=10m sum(low) as low, sum(pred) as forecast, sum(high) as high, sum(count) as actual "
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I'm still checking edge cases, but it seems to work so far.  Not as efficient as it could be though. &lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:18:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-my-map-search-being-parsed-before-variable-substitution/m-p/212769#M62362</guid>
      <dc:creator>mlf</dc:creator>
      <dc:date>2020-09-29T08:18:18Z</dc:date>
    </item>
    <item>
      <title>Re: Is my map search being parsed before variable substitution?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Is-my-map-search-being-parsed-before-variable-substitution/m-p/212770#M62363</link>
      <description>&lt;P&gt;OK I see. &lt;/P&gt;

&lt;P&gt;Well you can tokenize the search with the following logic&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| localop 
| rest /services/saved/searches 
| search title="XYZ - Predict - ATM volume"\
| makemv delim=" " search
| table search 
| mvexpand search 
| rex field=search "(?&amp;lt;key&amp;gt;\w+?)=(?&amp;lt;value&amp;gt;.*)" 
| eval {key}=value 
| fields - key value search 
| stats values(*) as *
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The problem is it will be hard to use that with map. I would try using a subsearch instead&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jan 2016 18:02:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Is-my-map-search-being-parsed-before-variable-substitution/m-p/212770#M62363</guid>
      <dc:creator>jplumsdaine22</dc:creator>
      <dc:date>2016-01-04T18:02:31Z</dc:date>
    </item>
  </channel>
</rss>

