Difference between != and NOT
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 value 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.
Using != with the regex command
If you use regular expressions in conjunction with !=
in searches, see regex
.
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.
Boolean expressions | Field expressions |
This documentation applies to the following versions of Splunk Cloud Platform™: 8.2.2112, 8.2.2201, 8.2.2202, 8.2.2203, 9.0.2205, 9.0.2208, 9.0.2209, 9.0.2303, 9.0.2305, 9.1.2308, 9.1.2312, 9.2.2403, 9.2.2406 (latest FedRAMP release), 9.3.2408
Feedback submitted, thanks!