Splunk Search

What's the best way to only do a Lookup based on the results of the main search?

finchy
Explorer

Hi,

What's the best way to only do a Lookup based on the results of the main search?  I want to only run this when 2 fields don't match.  Pseudo would be

If field1!=field2 THEN | lookup accounts department as field2 OUTPUT

So like an if then statement most programming languages allow

Thanks

Lee

Labels (1)
Tags (1)
0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

How to save CPU will depends on the actual flow of your search.  For example, if field1!=<fixed string pattern> is exceedingly rare in a large dataset, you can include the lookup in an append subsearch, like

<your main search>
| append
  [search <somesearch> field1 != <fixed string pattern>
   | lookup accounts department as field2 OUTPUT]

Then  manipulate the combined stream to utilize lookup output.

Because field1 != field2 is inapplicable in search command, this technique will not save you index search time.  However, if you have a situation where index search is cheap but lookup is exceedingly expensive (it can happen), you can still do it, like

<your main search>
| append
  [search <same main search>
   | where field1 != field2
   | lookup accounts department as field2 OUTPUT]

Alternatively, you are ONLY interested in annotating field2 for which field1 != field2, you can use appendpipe (which is very efficient)

<your main search>
| appendpipe
  [stats count by field1 field2
   | where field1 != field2
   | lookup accounts department as field2 OUTPUT
   | fields - count]

In all cases, you will need to massage these annotations back into your final results.  Hope this helps.

View solution in original post

bowesmana
SplunkTrust
SplunkTrust

You CAN actually do conditional lookup, as long as your lookup is a CSV

https://docs.splunk.com/Documentation/Splunk/9.1.1/SearchReference/ConditionalFunctions#lookup.28.26...

I don't think it's very commonly used, but works well

... search...
| eval output=if(field1!=field2, 
          lookup("mylookup.csv", json_object("department", field2), 
                 json_array("output_field1","output_field2")), 
          "{}")

 You will get back a field output with a JSON representation of the output fields listed in the JSON array

yuanliu
SplunkTrust
SplunkTrust

Nice ref.  Thanks, @bowesmana! (Looks like something since 8.)

0 Karma

fredclown
Contributor

You cannot do a conditional lookup, but you could do the lookup across all the data and then only conditionally display the data that was looked up.

0 Karma

finchy
Explorer

Hi, i wanted to avoid doing a lookup if certain conditions are in place if that's not possible will just have to do it which returns the data if it finds any was trying to just save some cpu and time

0 Karma

yuanliu
SplunkTrust
SplunkTrust

How to save CPU will depends on the actual flow of your search.  For example, if field1!=<fixed string pattern> is exceedingly rare in a large dataset, you can include the lookup in an append subsearch, like

<your main search>
| append
  [search <somesearch> field1 != <fixed string pattern>
   | lookup accounts department as field2 OUTPUT]

Then  manipulate the combined stream to utilize lookup output.

Because field1 != field2 is inapplicable in search command, this technique will not save you index search time.  However, if you have a situation where index search is cheap but lookup is exceedingly expensive (it can happen), you can still do it, like

<your main search>
| append
  [search <same main search>
   | where field1 != field2
   | lookup accounts department as field2 OUTPUT]

Alternatively, you are ONLY interested in annotating field2 for which field1 != field2, you can use appendpipe (which is very efficient)

<your main search>
| appendpipe
  [stats count by field1 field2
   | where field1 != field2
   | lookup accounts department as field2 OUTPUT
   | fields - count]

In all cases, you will need to massage these annotations back into your final results.  Hope this helps.

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 ...