Splunk Search

What is the order that executes in query when I use both AND, OR in splunk query?

pavanae
Builder

I have two different queries like below

Query 1 :-

field_1!="A" AND field_2="B" OR field_1!="A" AND field_2="C" OR field_1!="A" AND field_2="D" OR field_1!="A" AND field_2="E"  

Query 2 :-

field_1!="A" AND (field_2="B" OR field_2="C" OR field_2="D" OR field_2="E" )

In my view both the queries should give the same result but it's giving me the different result. What's the difference between both the queries and what's the actual order splunk executes?

0 Karma
1 Solution

somesoni2
Revered Legend

Your query 1 is hochpoch because of no clear distinction of the grouping of AND conditions and due to order in which the logical operators are executed (see here) they're not doing what you want to do, which is query 2. Below two query should give you same result.

Reformatted query 1:

(field_1!="A" AND field_2="B") OR (field_1!="A" AND field_2="C") OR (field_1!="A" AND field_2="D") OR (field_1!="A" AND field_2="E" ) 

Query 2 :-

 field_1!="A" AND (field_2="B" OR field_2="C" OR field_2="D" OR field_2="E" )

View solution in original post

0 Karma

somesoni2
Revered Legend

Your query 1 is hochpoch because of no clear distinction of the grouping of AND conditions and due to order in which the logical operators are executed (see here) they're not doing what you want to do, which is query 2. Below two query should give you same result.

Reformatted query 1:

(field_1!="A" AND field_2="B") OR (field_1!="A" AND field_2="C") OR (field_1!="A" AND field_2="D") OR (field_1!="A" AND field_2="E" ) 

Query 2 :-

 field_1!="A" AND (field_2="B" OR field_2="C" OR field_2="D" OR field_2="E" )
0 Karma

pavanae
Builder

Cool thanks @somesoni2. That makes sense now. PLease post the same as a answer. I am going to accept that. Thanks again.

0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...