If field1 exists always but is only sometimes error, then ...
Hmm. There's several ways to do this (some outlined by the inestimable somesoni2 already). Let's try this one:
sourcetype=abc
| eval is_error = if(field1=="error", "error", null())
| streamstats window=50 last(is_error) as trigger_error
| transaction maxevents=50 trigger_error
That should create one "event" out of the error line, plus the preceeding 50 events.
NOTES:
Change window=50 and maxevents=50 to 5 each for testing - it might make it easier to see/test. They should match each other, but otherwise the number is up to you.
Also, for debugging it might be useful to run that same search only replace the | transaction... line with | table _time, field1, is_error, trigger_error (and maybe include another field or two if it makes sense). If you do that, you'll see better how it works.
It works by
a) searching all the data - you can't throw out the non-error ones at the front or else how would you include them later?
b) creating a new field "is_error" (change that name if it conflicts with an existing field!) that only exists when field1 equals "error". This way if field1 is NOT "error" then there's no new field "is_error" on that event.
c) now the magic - streamstats in this case is watching a window or 50 events. For each event, it copies the most recent "is_error" to the all 50 events in its window as the field "trigger_error". This means that when "is_error" doesn't exist, nothing gets copied, but when it does, the preceeding 50 events also get a copy of it.
d) last we just group them together to make it easy to alert on it.
Line d) may require a little fiddling depending on exactly how you are going to use this.
As reference, here's a very similar thing done with 100 items before the "alerting event". It's not quite the same scenario, but it is close enough that it may help to read through that answer too.
Happy Splunking!
-Rich
... View more