運算子 graph-match 會搜尋輸入圖形來源中所有出現的圖形模式。
備註
這個運算子會與 make-graph 運算子搭配使用,。
語法
G|graph-match [cycles] = [where條件約束] project [ColumnName=] 表達式 [, ...]
參數
| 名稱 | 類型 | 為必填項目 | 說明 |
|---|---|---|---|
| G | string |
✔️ | 輸入圖表來源。 |
| 模式 | string |
✔️ | 一或多個以逗號分隔的圖形節點元素序列,使用圖形表示法由圖形邊緣元素連接。 請參閱 圖形模式表示法。 |
| 條件約束 | string |
布爾表達式,由 Pattern 中具名變數的屬性所組成。 每個圖形元素 (node/edge) 都有一組在圖形建構期間附加至它的屬性。 條件約束會定義模式會比對哪些專案(節點和邊緣)。 屬性是由變數名稱所參考,後面接著點 (.) 和屬性名稱。 |
|
| 表達 | string |
✔️ | 子句會將 project 每個模式轉換成表格式結果中的數據列。 項目表達式必須是 Pattern 中所定義具名變數的純量和參考屬性。 屬性是由變數名稱所參考,後面接著點 (.) 和屬性名稱。 |
| CyclesOption | string |
控制模式中是否比對迴圈,允許的值:all、、noneunique_edges。 如果 all 已指定,則會比對所有迴圈,如果 none 指定的迴圈不相符,則 unique_edges 如果指定了迴圈,則會比對迴圈,但前提是迴圈未包含相同邊緣一次以上。 |
圖形模式表示法
下表顯示支援的圖形表示法:
| 元素 | 具名變數 | 匿名 |
|---|---|---|
| 節點 |
(
n) |
() |
| 導向邊緣:由左至右 |
-[
e]-> |
--> |
| 導向邊緣:由右至左 |
<-[
e]- |
<-- |
| 任何方向邊緣 |
-[
e]- |
-- |
| 可變長度邊緣 |
-[
e*3..5]- |
-[*3..5]- |
可變長度邊緣
可變長度邊緣可讓特定模式在定義的限制內重複多次。 這種類型的邊緣是以星號 (*) 表示,後面接著 最小..最大值格式的最小值和最大出現值。 最小值和最大值都必須是 整數 純量。 如果序列中的所有邊緣都符合 子句中 where 概述的條件約束,則落在此出現範圍中的任何邊緣都符合模式的變數邊緣。
多個序列
多個逗號分隔序列可用來表示非線性模式。 若要描述不同序列之間的連線,它們必須共享節點的一或多個變數名稱。 例如,若要代表位於中心連線到節點 a、b、c 和 d 之節點 n 的星形模式,可以使用下列模式:
(
一個)--(n)--(b,c)()--(n)--(d)
僅支援單一連接的元件模式。
退貨
運算符 graph-match 會傳回表格式結果,其中每個記錄都會對應至圖形中模式的相符專案。
傳回的數據行會使用模式中定義的邊緣和/或節點屬性,在運算子的 project 子句中定義。 可變長度邊緣屬性的屬性和函式會當做動態數位傳回,陣列中的每個值都會對應至可變長度邊緣的出現次數。
範例
下列範例代表組織階層。 它示範如何使用可變長度邊緣,在單一查詢中尋找階層不同層級的員工。 圖表中的節點代表員工,而邊緣則來自員工到其主管。 使用 make-graph建置圖形之後,我們會搜尋小於 Alice之組織中的員工30。
let employees = datatable(name:string, age:long)
[
"Alice", 32,
"Bob", 31,
"Eve", 27,
"Joe", 29,
"Chris", 45,
"Alex", 35,
"Ben", 23,
"Richard", 39,
];
let reports = datatable(employee:string, manager:string)
[
"Bob", "Alice",
"Chris", "Alice",
"Eve", "Bob",
"Ben", "Chris",
"Joe", "Alice",
"Richard", "Bob"
];
reports
| make-graph employee --> manager with employees on name
| graph-match (alice)<-[reports*1..5]-(employee)
where alice.name == "Alice" and employee.age < 30
project employee = employee.name, age = employee.age, reportingPath = map(reports, manager)
輸出
| 員工 | 年齡 | reportingPath |
|---|---|---|
| 喬 | 二十九 | [ “Alice” ] |
| 前夕 | 二十七 | [ “Alice”, “Bob” ] |
| 本 | 23 | [ “Alice”, “Chris” ] |
下列範例會從 Actions 和 Entities 數據表建置圖形。 實體是人員和系統,而動作會描述實體之間的不同關聯性。
make-graph遵循建置圖形的運算子,是使用搜尋系統攻擊路徑的graph-match圖形模式呼叫 "Apollo" 。
let Entities = datatable(name:string, type:string, age:long)
[
"Alice", "Person", 23,
"Bob", "Person", 31,
"Eve", "Person", 17,
"Mallory", "Person", 29,
"Apollo", "System", 99
];
let Actions = datatable(source:string, destination:string, action_type:string)
[
"Alice", "Bob", "communicatesWith",
"Alice", "Apollo", "trusts",
"Bob", "Apollo", "hasPermission",
"Eve", "Alice", "attacks",
"Mallory", "Alice", "attacks",
"Mallory", "Bob", "attacks"
];
Actions
| make-graph source --> destination with Entities on name
| graph-match (mallory)-[attacks]->(compromised)-[hasPermission]->(apollo)
where mallory.name == "Mallory" and apollo.name == "Apollo" and attacks.action_type == "attacks" and hasPermission.action_type == "hasPermission"
project Attacker = mallory.name, Compromised = compromised.name, System = apollo.name
輸出
| 攻擊者 | 遭洩漏 | 系統 |
|---|---|---|
| 馬婁裡 | 鮑勃 | 阿波羅 |
下列範例類似於先前的攻擊路徑範例,但具有額外的條件約束:我們希望遭入侵的實體也與 Alice 通訊。 模式 graph-match 前置詞與上一個範例相同,我們會新增另一個序列,並將 遭入侵 作為序列之間的連結。
let Entities = datatable(name:string, type:string, age:long)
[
"Alice", "Person", 23,
"Bob", "Person", 31,
"Eve", "Person", 17,
"Mallory", "Person", 29,
"Apollo", "System", 99
];
let Actions = datatable(source:string, destination:string, action_type:string)
[
"Alice", "Bob", "communicatesWith",
"Alice", "Apollo", "trusts",
"Bob", "Apollo", "hasPermission",
"Eve", "Alice", "attacks",
"Mallory", "Alice", "attacks",
"Mallory", "Bob", "attacks"
];
Actions
| make-graph source --> destination with Entities on name
| graph-match (mallory)-[attacks]->(compromised)-[hasPermission]->(apollo), (compromised)-[communicates]-(alice)
where mallory.name == "Mallory" and apollo.name == "Apollo" and attacks.action_type == "attacks" and hasPermission.action_type == "hasPermission" and alice.name == "Alice"
project Attacker = mallory.name, Compromised = compromised.name, System = apollo.name
輸出
| 攻擊者 | 遭洩漏 | 系統 |
|---|---|---|
| 馬婁裡 | 鮑勃 | 阿波羅 |