<?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 How to use conditional search in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-conditional-search/m-p/708604#M239610</link>
    <description>&lt;P&gt;Hi All,&lt;BR /&gt;&lt;BR /&gt;I have a main search where name1 filed will have multiple values&lt;BR /&gt;I need to run sub search based on the value of name1.&amp;nbsp;&lt;BR /&gt;The structure goes like this:&lt;BR /&gt;mail_search&lt;BR /&gt;which has name1=a&lt;BR /&gt;sub search&lt;BR /&gt;if name1=a&lt;BR /&gt;&amp;nbsp; then run search1&lt;BR /&gt;if name1=b&lt;BR /&gt;&amp;nbsp; then run search2&lt;BR /&gt;&lt;BR /&gt;I have tried this with the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval name1="a" 
| eval condition=case(name1="a", "index=_internal | head 1 | eval val=\"Query for a1\" | table val", 
name1="b", "index=_internal | head 1 | eval val=\"Query for b\" | table val", 1=1, "search index=_internal | head 1 | eval val=\"Default query\" | table val")
|table condition
| map search=$condition$&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am getting the following error&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Unable to run query '"index=_internal | head 1 | eval val=\"Query for a1\" | table val"'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 13 Jan 2025 14:07:21 GMT</pubDate>
    <dc:creator>nelaturivijay</dc:creator>
    <dc:date>2025-01-13T14:07:21Z</dc:date>
    <item>
      <title>How to use conditional search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-conditional-search/m-p/708604#M239610</link>
      <description>&lt;P&gt;Hi All,&lt;BR /&gt;&lt;BR /&gt;I have a main search where name1 filed will have multiple values&lt;BR /&gt;I need to run sub search based on the value of name1.&amp;nbsp;&lt;BR /&gt;The structure goes like this:&lt;BR /&gt;mail_search&lt;BR /&gt;which has name1=a&lt;BR /&gt;sub search&lt;BR /&gt;if name1=a&lt;BR /&gt;&amp;nbsp; then run search1&lt;BR /&gt;if name1=b&lt;BR /&gt;&amp;nbsp; then run search2&lt;BR /&gt;&lt;BR /&gt;I have tried this with the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval name1="a" 
| eval condition=case(name1="a", "index=_internal | head 1 | eval val=\"Query for a1\" | table val", 
name1="b", "index=_internal | head 1 | eval val=\"Query for b\" | table val", 1=1, "search index=_internal | head 1 | eval val=\"Default query\" | table val")
|table condition
| map search=$condition$&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am getting the following error&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Unable to run query '"index=_internal | head 1 | eval val=\"Query for a1\" | table val"'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2025 14:07:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-conditional-search/m-p/708604#M239610</guid>
      <dc:creator>nelaturivijay</dc:creator>
      <dc:date>2025-01-13T14:07:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to use conditional search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-conditional-search/m-p/708609#M239615</link>
      <description>&lt;P&gt;If you re trying to do this in a dashboard, try setting a token to the variable part of the search and using that&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2025 14:56:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-conditional-search/m-p/708609#M239615</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2025-01-13T14:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to use conditional search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-conditional-search/m-p/708644#M239629</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/257811"&gt;@nelaturivijay&lt;/a&gt;&amp;nbsp;Please have a look.&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/9.4.0/Viz/tokens#Using_tokens_in_a_search" target="_blank"&gt;https://docs.splunk.com/Documentation/Splunk/9.4.0/Viz/tokens#Using_tokens_in_a_search&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jan 2025 17:51:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-conditional-search/m-p/708644#M239629</guid>
      <dc:creator>kiran_panchavat</dc:creator>
      <dc:date>2025-01-13T17:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to use conditional search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-conditional-search/m-p/708724#M239633</link>
      <description>&lt;P&gt;First, map is usually not the solution to the problem you are trying to solve.&lt;/P&gt;&lt;P&gt;Secondly, could you explain the relationship between values "a", "b" and the searches "index=_internal | head 1 | eval val=\"Query for a1\" | table val" and "index=_internal | head 1 | eval val=\"Query for b\" | table val"?&lt;/P&gt;&lt;P&gt;Confusingly, everyone of the three searches will result in a predetermined string value of a single field. &amp;nbsp;Why bother with index=_internal? &amp;nbsp;If you are just trying to make a point of map, you can compose them with makeresults just as easily.&lt;/P&gt;&lt;P&gt;If you really want to use map, study the syntax and examples in&amp;nbsp;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map" target="_blank" rel="noopener"&gt;map&lt;/A&gt;. &amp;nbsp;The whole idea of map is to NOT use case function. &amp;nbsp;To produce the result you intended, here is a proper construct:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval name1 = mvappend("c", "b", "a")
| mvexpand name1
| map search="search index=_internal
  | head 1
  | eval val=if(\"$name1$\" IN (\"a\", \"b\"), \"Query for $name1$\", \"Default query\")
  | table val"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the output no matter what data you have in _internal.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;val&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="108.953125px" height="25px"&gt;Default query&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="108.953125px" height="25px"&gt;Query for b&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="108.953125px" height="25px"&gt;Query for a&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;However, there are often much easier and better ways to do this. &amp;nbsp;To illustrate, forget&amp;nbsp;val="Query for a". &amp;nbsp;Let's pick more realistic mock values "info", "warn". &amp;nbsp;This is a construct using map.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults | eval searchterm = mvappend("info", "warn", "nosuchterm")
| mvexpand searchterm
| map search="search index=_internal log_level=\"$searchterm$\"
  | stats count by log_level
  | eval val=if(\"$searchterm$\" IN (\"info\", \"warn\"), \"Query for $searchterm$\", \"Default query\")"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you examine _internal events, you will know that, even though searchterm is given three values, the above should only give two rows, like&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;log_level&lt;/TD&gt;&lt;TD&gt;count&lt;/TD&gt;&lt;TD&gt;val&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;INFO&lt;/TD&gt;&lt;TD&gt;500931&lt;/TD&gt;&lt;TD&gt;Query for info&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;WARN&lt;/TD&gt;&lt;TD&gt;17262&lt;/TD&gt;&lt;TD&gt;Query for warn&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;However, the syntax of map makes the search much harder to maintain. &amp;nbsp;Here is an alternative using subsearch. (There are other alternatives based on actual search term and data characteristics.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=_internal
    [makeresults
    | eval searchterm = mvappend("info", "warn", "nosuchterm")
    | fields searchterm
    | rename searchterm as log_level]
| stats count by log_level
| eval val = if(log_level IN ("INFO", "WARN"), "Query for " . log_level, "Default query")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you apply this to the exact same time interval, it will give you exactly the same output.&lt;/P&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 08:55:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-conditional-search/m-p/708724#M239633</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2025-01-14T08:55:59Z</dc:date>
    </item>
  </channel>
</rss>

