Splunk Search

Searcing with empty index

omend
Path Finder

Hi all,

I have a Splunk index with records of the following format:

  • recordIndex - an integer key I automatically assign to this record upon insert
  • recordName - name of the record
  • recordComment - comment

(I temporarily use the recordIndex because both other fields consist of wildcards and it allows me to edit/delete them more easily. Later on, this index will be transfered to relational DB).

When I want to add a record to this index, I look for the largest recordIndex exists, add 1 to it, and use it for the new record. I use the following (simplified) search code:

index=myindex | sort -num(recordIndex) | head 1 | eval recordIndex=recordIndex+1 | eval recordName="$args.recordName$" | eval recordComment="$args.comment$" | table recordIndex recordName recordComment | collect index=index=myindex

However, there is a corner case where the index is empty. For this case only, I could use the following search:
index=_internal | head 1 | eval recordIndex=1 | eval recordName="$args.recordName$" | eval recordComment="$args.comment$" | table recordIndex recordName recordComment | collect index=index=myindex

My question is how to "combine" these 2 searches so they could work together in all cases.

Thanks,
Ori.

0 Karma
1 Solution

omend
Path Finder

I solved this by using the append function:

index=myindex | table recordIndex | sort -num(recordIndex) | head 1 | append [search index=_internal | head 1] | eval recordIndex=if(recordIndex>0,recordIndex+1,1) | eval recordName="$args.recordName$" | eval recordComment="$args.comment$" | table recordIndex recordName recordComment | sort -recordIndex | head 1 | collect index=myindex

View solution in original post

0 Karma

omend
Path Finder

I solved this by using the append function:

index=myindex | table recordIndex | sort -num(recordIndex) | head 1 | append [search index=_internal | head 1] | eval recordIndex=if(recordIndex>0,recordIndex+1,1) | eval recordName="$args.recordName$" | eval recordComment="$args.comment$" | table recordIndex recordName recordComment | sort -recordIndex | head 1 | collect index=myindex

0 Karma
Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...