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!

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...