Refine your search:

4
3

I have a subsearch that calculates a field call 'MyLatestTime' and I want to use that to set the latest field in my outer search. How would I do that?

For example:

"outer search" earliest="01/19/2010:00:00:00" latest=MyLatestTime ["inner search" | eval MyLatestTime = _time ]

I hope that made sense.

Thanks.

asked 20 Jan '10, 07:49

dinh's gravatar image

dinh
190111
accept rate: 33%

edited 15 Apr '10, 08:13

dskillman's gravatar image

dskillman ♦
487110


3 Answers:

Hi Dinh,

To pass a field from the inner search to the outer search you must use the 'fields' command. Otherwise, Splunk will pass the results of the inner search as a set of events. Also, in the outer search, the assignment latest=MyLatestTime can be done in the inner search instead. Try this:

... earliest="01/19/2010:00:00:00" [search ... | eval MyLatestTime=_time | fields + MyLatestTime | rename MyLatestTime as latest]

link

answered 20 Jan '10, 15:38

hulahoop's gravatar image

hulahoop ♦
2.5k3239
accept rate: 40%

What Vi Ly replied is right. It may also help you to use the "format" command. When a subsearch is used as an argument to a "search" command, its output is implicitly passed through "format" (unless it has already been explicitly sent through format) to convert it into a single string value (called either "search" or "query"). That string is then expanded into arguments to the original search command.

So for example:

| stats count | eval latest="-18d" | fields + latest | format

yields a string:

( ( lastest="-18d" ) )

or something like that. That string would then be made part of your original search command in place of the subsearch. The "format" command take take an entire table and format it, so if your subsearch returned the table:

field1   field2   field3
------   ------   ------
red          14   cow
black         7   wolf

"format" would return:

( ( field1=red AND field2=14 AND field3=cow ) OR ( field1=black AND field2=7 AND field3=cow ) )

by default. If used in a subsearch in a "search" command, then, that query would be part of your search. Note that arguments to "format" can change the "AND", "OR" and parentheses to other characters (e.g., it is sometimes useful to generate a string like the above, but where "AND" is replaced with "OR").

link

answered 20 Jan '10, 20:54

gkanapathy's gravatar image

gkanapathy ♦
26.2k1622
accept rate: 42%

edited 20 Jan '10, 21:01

This doesn't work for me. Here's what I'm trying to do:

sourcetype="ST" [search sourcetype="ST" foo=bar | stats max(_time) as latest min(_time) as earliest by host | eval latest=latest+.001 | fields + earliest latest host]

I want this to produce: sourcetype="ST" latest=1234567.500 earliest=1234566.799 host=host1

I believe the search that is actually being created is: sourcetype="ST" ( ( latest=1234567.500 AND earliest=1234566.799 AND host=host1 ) )

Running that search produces the error "Error in 'UnifiedSearch': Unable to parse the 'Missing LHS for AND' search.".

Removing the parentheses, it works as expected.

Anybody know a way to add the arguments from the subsearch onto the search without the parentheses?

EDIT...

Thank you, Dr. Wooden, that works a treat. I kept going and made a workflow action that can be used generically, and should actually be fast, since it uses the results of the active search instead of rerunning the initial search. It should also work for complicated searches, since it's just pulling the results off disk.

* [loadjob $@sid$ events=t | stats max(_time) as latest min(_time) as earliest by host sourcetype | eval latest=latest+.001 | format "(" "(" "" ")" "OR" ")" ] | sort _time

Here's the full config entry:

[show_context]
display_location = event_menu
fields = *
label = Show Context
search.preserve_timerange = 1
search.search_string = * [loadjob $@sid$ events=t | stats max(_time) as latest min(_time) as earliest by host sourcetype | eval latest=latest+.001 | format "(" "(" "" ")" "OR" ")" ] | sort _time
search.target = blank
type = search

The only downer is that it doesn't build a reusable search, but it should work well for interactive use.

link

answered 17 Nov '10, 16:37

vbumgarn's gravatar image

vbumgarn
6011213
accept rate: 9%

edited 18 Nov '10, 04:31

We can get rid of the extra parenthesis like this (but it doesn't appear to fix the reported error): sourcetype="ST" [search sourcetype="ST" foo=bar | stats max(_time) as latest min(_time) as earliest by host | eval latest=latest+.001 | fields + earliest latest host | format "" "(" "AND" ")" "OR" ""]

(17 Nov '10, 17:05) bwooden ♦

I removed the outer parenthesis as well and it appeared to work in my lab. Does this work in your environment: sourcetype="ST" [search sourcetype="ST" foo=bar | stats max(_time) as latest min(_time) as earliest by host | eval latest=latest+.001 | fields + earliest latest host | format "" "" "AND" "" "OR" ""]

(17 Nov '10, 17:13) bwooden ♦
Post your answer
toggle preview

Follow this question

Log In to enable email subscriptions

RSS:

Answers

Answers + Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×1,083
×130

Asked: 20 Jan '10, 07:49

Seen: 1,690 times

Last updated: 18 Nov '10, 04:31

Copyright © 2005-2012 Splunk, Inc. All rights reserved.