Splunk Search

How to handle no results from sub-search when using 'format' or 'return'

jonfrancais
Explorer

We would like to use a sub-search to query an input and re-write the search query to alter the search used in the main search. The input may sometimes contain no data.

The input data contains a list of KEY/VALUE fields. We would like the resultant query returned from the sub-search to be:

(ID1=<KEY> OR ID2=<KEY>) AND ID3=<VALUE>

For the purposes of this question, we can use an inputlookup sub-search as an example. The following works when the input contains data. We use 'return $search' to ensure the query returned is not surrounded by double-quotes (which 'format' would do).

[| inputlookup myfile | eval search="((ID1=".KEY." OR ID2=".KEY.") AND ID3=".VALUE.")" | return $search]

However, when there is no data returned from the inputlookup and the 'return' command returns nothing, e.g '', it creates a main search which looks for everything. If 'format' is used instead, then 'NOT()' is returned correctly, but then when there are results, the search query string is returned with the double-quotes.

How should the query be returned from a sub-search to ensure that 'NOT ()' is returned when there are no results and also that the search string is returned without quotes when there are?

0 Karma

gfuente
Motivator

Hello

Have you tried to include the NOT into the subsearch, so if there are values it will be included, and if there is no results the subsearch will no return anything at all, and the query will execute. Modify your eval this way

eval search=" NOT ((ID1=".KEY." OR ID2=".KEY.") AND ID3=".VALUE.")"

Try it and let me know if it works.

Regards

0 Karma
Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...