has vs contains

Both operators look for text inside a string, and both ignore case by default. The difference is what counts as a match.

  • contains matches any substring, anywhere: "webadmins" contains "admin" is true.
  • has matches whole terms. Kusto splits text into terms at every character that is not a letter or a digit (spaces, dots, dashes, slashes, @, _, and so on). "webadmins" has "admin" is false, while "it-admin@corp.example" has "admin" is true, because admin is a term there.
Text has "admin" contains "admin"
it-admin@corp.example true true
webadmins false true
cadmium false true
ADMIN true true

Which one to use

  • has is the better default in real workspaces. Kusto indexes terms, so has is much faster on big tables, and it avoids accidental matches inside longer words.
  • Use contains when you need part of a term: a fragment of a file name, a parameter written in several ways, or a string that starts with punctuation.

Case and negation

  • Add _cs for a case-sensitive search: has_cs, contains_cs.
  • Put ! in front for “does not”: !has, !contains, !has_cs, !contains_cs.
  • For a match at the start or end of the string, see startswith and endswith.
  • For patterns, see matches regex.

Things to know

  • A value with punctuation inside, like has "cmd.exe", matches the text cmd.exe as long as it is not part of a longer term on either side: xcmd.exe does not match.
  • A search for a value that is an exact field value is clearer and faster with == or =~.

Examples

1 The same word with has and contains
DeviceNetworkEvents
| summarize HasCloud = countif(RemoteUrl has "cloud"), ContainsCloud = countif(RemoteUrl contains "cloud"), HasCloudsuite = countif(RemoteUrl has "cloudsuite")

cloud is only part of the term cloudsuite in chat.cloudsuite.example, so has doesn't match it, but contains does.

2 Terms are split at punctuation
print Text = "powershell.exe -ExecutionPolicy Bypass"
| extend HasPolicy = Text has "policy", ContainsPolicy = Text contains "policy", HasWholeTerm = Text has "executionpolicy", HasCs = Text has_cs "bypass"

ExecutionPolicy is one term; has ignores case but needs the whole term. has_cs also checks case, so lower-case bypass fails.

3 Find URLs that contain a term
DeviceNetworkEvents
| where RemoteUrl has "cloudsuite"
| summarize Connections = count() by RemoteUrl

Practice it