Splunk Cloud

Search Manual

Download manual as PDF

Download topic as PDF

Difference between NOT and !=

When you want to exclude results from your search you can use the NOT operator or the != field expression. However there is a significant difference in the results that are returned from these two methods.

Suppose you have the following events. As you can see, some events have missing values.

ID Name Color Location
101M3 McIntosh Chestnut Marin Meadows
104F5 Lyra Bay
104M6 Rutherford Dun Placer Pastures
101F2 Rarity Marin Meadows
102M7 Dash Black Calaveras Farms
102M1 Roan
101F6 Chestnut Marin Meadows
104F4 Pinkie Sorrel Placer Pastures
102M8 Spike Grey Calaveras Farms

Searching with !=

If you search with the != expression, every event that has a value in the field, where that values does not match the value you specify, is returned. Events that do not have a value in the field are not included in the results.

For example, if you search for Location!="Calaveras Farms", events that do not have Calaveras Farms as the Location are returned. Events that do not have Location value are not included in the results.

source="Ponies.csv" Location!="Calaveras Farms"

ID Name Color Location
101M3 McIntosh Chestnut Marin Meadows
104M6 Rutherford Dun Placer Pastures
101F2 Rarity Marin Meadows
101F6 Chestnut Marin Meadows
104F4 Pinkie Sorrel Placer Pastures

If you search for a Location that does not exist using the != expression, all of the events that have a Location value are returned.

Searching with NOT

If you search with the NOT operator, every event is returned except the events that contain the value you specify. This includes events that do not have a value in the field.

For example, if you search using NOT Location="Calaveras Farms", every event is returned except the events that contain the value "Calaveras Farms". This includes events that do not have a Location value.

source="Ponies.csv" NOT Location="Calaveras Farms"

ID Name Color Location
101M3 McIntosh Chestnut Marin Meadows
104F5 Lyra Bay
104M6 Rutherford Dun Placer Pastures
101F2 Rarity Marin Meadows
102M1 Roan
101F6 Chestnut Marin Meadows
104F4 Pinkie Sorrel Placer Pastures

If you search for a Location that does not exist using NOT operator, all of the events are returned.

Searching with != or NOT is not efficient

Using the != expression or NOT operator to exclude events from your search results is not an efficient method of filtering events. The execution cost for a search is actually less when you explicitly specify the values that you want to include in the search results. For more tips on search optimization, see Quick tips for optimization.

PREVIOUS
Field expressions
  NEXT
Use CASE() and TERM() to match phrases

This documentation applies to the following versions of Splunk Cloud: 6.6.3, 7.1.3, 7.0.0, 7.0.2, 7.0.3, 7.0.5


Comments

Hello IRHM73
Thank you for your comment. I looked over the example and it was not correct. I, with the help of Splunk Trust member DalJeanis, have updated the example.

Lstewart splunk, Splunker
May 8, 2018

Hi, is the example correct please.

In this line it talks about Field A "Each of these fields has 3 different values. For example fieldA has value1, value2, and value3."

Then goes onto Field B.

Regards

IRHM73
April 8, 2018

Was this documentation topic helpful?

Enter your email address, and someone from the documentation team will respond to you:

Please provide your comments here. Ask a question or make a suggestion.

You must be logged into splunk.com in order to post comments. Log in now.

Please try to keep this discussion focused on the content covered in this documentation topic. If you have a more general question about Splunk functionality or are experiencing a difficulty with Splunk, consider posting a question to Splunkbase Answers.

0 out of 1000 Characters