Dashboards & Visualizations

Boolean Operators as Variables

Tisiphone_1
Explorer

I've made a form that allows a user to select with radio buttons whether multiple search fields are treated as an AND or as an OR. They have the option to fill out some or all of the form fields. However, the problem I run into when I have null fields is that Splunk is unhappy with duplicate boolean operators (OR OR or AND AND). I can't simply use a default * because that will mess up OR.

My codes is as follows:

<form>
  <label>Search</label>
   <searchTemplate>index=Stuff ($Field1$ $andor$ $Field2$ $andor$ $Field3$)</searchTemplate>

  <fieldset>
    <input type="text" token="Field1"><label>Search 1</label>
     <prefix>DBField1="</prefix>
     <suffix>"</suffix>
     </input>
  </fieldset>

  <fieldset>
    <input type="text" token="Field2"><label>Search 2</label>
     <prefix>DBField2="</prefix>
     <suffix>"</suffix>
     </input>
  </fieldset>

  <fieldset>
     <input type="text" token="Field3"><label>Search 3</label>
     <prefix>DBField3="</prefix>
     <suffix>"</suffix>
     </input>

     <input type="time">
     <default>Last 30 days</default>
     </input>

     <input type="radio" token="andor">
     <label>AND or OR</label>
     <choice value="AND">AND</choice>
     <choice value="OR">OR</choice>
     <default>OR</default>
    </input>
  </fieldset>

</form>

Is there anything that can be done? I've tried numerous rows of eval if(Field1="Field1=&qu ot;&qu ot;"), but all I end up is Splunk Handler exceptions.

Tags (1)
0 Karma
1 Solution

gkanapathy
Splunk Employee
Splunk Employee

Make your entire search into a macro:

<searchTemplate>index=Stuff `searchquery($Field1$,$Field2$,$Field3$,$andor$)`</searchTemplate>


[searchquery(4)]
args = f1,f2,f3,ao
iseval = true
definition = "$f1$" + if(len("$f2$")>0," $ao$ $f2$","") + if(len("$f2")>0," $ao$ $f3","")

View solution in original post

0 Karma

gkanapathy
Splunk Employee
Splunk Employee

Or possibly, add the operator into the prefix:

<prefix>$andor$ DBField3="</prefix>

But I'm not sure that will actually work.

0 Karma

Tisiphone_1
Explorer

The macro is what is exploding.

0 Karma

gkanapathy
Splunk Employee
Splunk Employee

right. if it's quotes in XML, replace it with the string &quot; or else wrap the whole string in a cdata tag, e.g. <![CDATA[stuff="]]>. but I suspect this won't work anyway and suggest you use the macro method instead.

0 Karma

Tisiphone_1
Explorer

Hmm, something about this statement is making Splunk explode. I think it may have to do with quotes in XML. Still working on it.

0 Karma

Tisiphone_1
Explorer

I tried that previously and it did not work, chicken and the egg thing. I am still working on implementing the previous - working out syntax errors. I'll let you know how it goes.

0 Karma

gkanapathy
Splunk Employee
Splunk Employee

Make your entire search into a macro:

<searchTemplate>index=Stuff `searchquery($Field1$,$Field2$,$Field3$,$andor$)`</searchTemplate>


[searchquery(4)]
args = f1,f2,f3,ao
iseval = true
definition = "$f1$" + if(len("$f2$")>0," $ao$ $f2$","") + if(len("$f2")>0," $ao$ $f3","")
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Fuel Your Journey: What’s Waiting for You at the .conf26 Acceleration Station

Navigating the show floor at .conf26 isn't just about keynotes and technical breakout sessions; it's also ...

Join the Final Session of the Data Management & Federation Bootcamp Series

Over the past three sessions of the Data Management & Federation Bootcamp Series, we've explored how to build ...

From Data to Insight: Announcing the Winners of the Splunk Dashboard Contest

Hi Splunkers, First off, thank you to everyone who participated in our very first From Data to Insight: The ...