Splunk Search

Searchmatch with AND does not work

christi2019
New Member

Notifications and ChangeNotifications present in both indices and I want to separate them by index type and count them. Looks like searchmatch with AND function seems not working.Is there better way to have these fields seperated by index type.

index=service1 OR index=service2
*/application/Notification OR
*/application/changeNotification
| eval timeevents=case(searchmatch("Notifcations" AND "index=service1" ),"Service1NewNotification",
searchmatch("Notifcations" AND "index=service2" ),"service2NewNotification",
searchmatch("changeNotifications"AND "index=service1"), "service1newChangeNotifications",
searchmatch("changeNotifications"AND "index=service2"), "service2newChangeNotifications",

| stats count by index, timeevents

Tags (1)
0 Karma
1 Solution

niketn
Legend

@christi2019 try the following

1) Since index is already a field you do not need to do searchmatch which looks at entire raw data for pattern match.
2) Since your stats is already splitting data by index and timeevents, there is no need to merge index while evaluating timeevents.

| eval timeevents=case(searchmatch("Notifcations"),"NewNotification",
                       searchmatch("Notifcations"),"NewNotification",
                       searchmatch("changeNotifications"), "newChangeNotifications",
                       searchmatch("changeNotifications"), "newChangeNotifications")
| stats count by index, timeevents

However, if you want to perform multiple matches inside a case expression you can try one of the following approaches. All should give same output but with different performance.
Option 2

| eval timeevents=case(searchmatch("Notifcations") AND index="service1","Service1NewNotification",
                       searchmatch("Notifcations") AND index="service2","service2NewNotification",
                       searchmatch("changeNotifications") AND index="service1", "service1newChangeNotifications",
                       searchmatch("changeNotifications") AND index="service2", "service2newChangeNotifications")

Option 3: Following may not work unless your raw data actually has text like "index=service"

| eval timeevents=case(searchmatch("Notifcations") AND searchmatch("index=service1" ),"Service1NewNotification",
                       searchmatch("Notifcations") AND searchmatch("index=service2"),"service2NewNotification",
                       searchmatch("changeNotifications") AND searchmatch("index=service1"), "service1newChangeNotifications",
                       searchmatch("changeNotifications") AND searchmatch("index=service2"), "service2newChangeNotifications")
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

View solution in original post

0 Karma

christi2019
New Member

thanks Niketnilay.

0 Karma

niketn
Legend

@christi2019 try the following

1) Since index is already a field you do not need to do searchmatch which looks at entire raw data for pattern match.
2) Since your stats is already splitting data by index and timeevents, there is no need to merge index while evaluating timeevents.

| eval timeevents=case(searchmatch("Notifcations"),"NewNotification",
                       searchmatch("Notifcations"),"NewNotification",
                       searchmatch("changeNotifications"), "newChangeNotifications",
                       searchmatch("changeNotifications"), "newChangeNotifications")
| stats count by index, timeevents

However, if you want to perform multiple matches inside a case expression you can try one of the following approaches. All should give same output but with different performance.
Option 2

| eval timeevents=case(searchmatch("Notifcations") AND index="service1","Service1NewNotification",
                       searchmatch("Notifcations") AND index="service2","service2NewNotification",
                       searchmatch("changeNotifications") AND index="service1", "service1newChangeNotifications",
                       searchmatch("changeNotifications") AND index="service2", "service2newChangeNotifications")

Option 3: Following may not work unless your raw data actually has text like "index=service"

| eval timeevents=case(searchmatch("Notifcations") AND searchmatch("index=service1" ),"Service1NewNotification",
                       searchmatch("Notifcations") AND searchmatch("index=service2"),"service2NewNotification",
                       searchmatch("changeNotifications") AND searchmatch("index=service1"), "service1newChangeNotifications",
                       searchmatch("changeNotifications") AND searchmatch("index=service2"), "service2newChangeNotifications")
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Where Innovation Takes Flight: The Splunk4Aviation Flight Sim Lands at .conf26

If you hear someone at .conf26 shouting "gear down, GEAR DOWN" across the show floor, you have found us.  The ...

Turn Cisco Telemetry Into Action with Cisco Data Fabric, powered by the Splunk ...

The surge in machine data is already hitting enterprise budgets, and the agentic era will only intensify it. ...

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...