Reputation: 3128
Whas is the difference between the has
and contains
operators in KQL?
Here is the has
operator documentation. Here is the documentation for the contains
operator.
Both of them check for an existence of a case insensitive string. So, does it mean that the usage of one operator over the other is just a matter of taste?
Upvotes: 13
Views: 22232
Reputation: 44981
Why should we prefer has over contains in some scenarios?
TL;DR: performance (use of index vs. data scan).
A term is a sequence of alpha-numeric characters (see What is a term?).
Some examples:
Azure Data Explorer (AKA ADX, AKA Kusto), indexes every term, as long it is 3 characters long or more (for storage engine v3. For v2 it is 4 characters).
The index (Full-text search index) is what enables ADX to return search results in sub-seconds/seconds even when the searched is done on Petabytes.
As of today, the index can be used for a whole term search, or for a prefix search.
Here are how contains & has behave for different searched strings:
P.S. Even if a term is indexed, the index might not be used, e.g., when a term is highly common, scanning the data itself directly might be cheaper than using the index.
Here are some examples for search strings that are found by contains and not by has.
Note the following:
hell
or hello
).hell
.hello
as long as it is not a part of a longer alpha-numeric sequence.datatable(txt:string)
[
"Hello World"
,"<Hello-World>"
,"*Hello*World*"
,"?Hello%World!"
,"_Hello_World_"
,"123Hello-World456"
,"abcHello Worldxyz"
,"HelloWorld"
]
| extend contains_hell = txt contains "hell"
,contains_hello = txt contains "hello"
,has_hell = txt has "hell"
,has_hello = txt has "hello"
txt | contains_hell | contains_hello | has_hell | has_hello |
---|---|---|---|---|
Hello World | true | true | false | true |
<Hello-World> | true | true | false | true |
*Hello*World* | true | true | false | true |
?Hello%World! | true | true | false | true |
Hello_World | true | true | false | true |
123Hello-World456 | true | true | false | false |
abcHello Worldxyz | true | true | false | false |
HelloWorld | true | true | false | false |
Upvotes: 25