簡體   English   中英

選擇具有特定起點的子圖中的所有連接節點以顯示在R可視化中

[英]Selecting all connected nodes in a sub-graph with a specific starting point to display in an R visualization

我有一個用於社交網絡分析的簡單neo4j數據庫。 該數據庫由用戶節點和用戶可能共有的其他節點(例如電話或地址)組成。 關系只有一種類型:[:HAS]。 為了使一個用戶與另一個用戶匹配,他們必須遍歷它們之間的至少一個節點。

我們的目標是將這些數據存儲在圖形中,並部署R Shiny應用程序以輸入用戶ID並查看已連接用戶的完整網絡。 為了做到這一點,我們需要從連接的子圖中將所有節點和關系拉到邊緣數據幀中。

通過使用以下密碼查詢,我們已經取得了一些成功。 但是,此查詢將僅拉出最多5個連接度的節點。 對於任何高度連接的節點,它也會失敗-在此過程中凍結了neo4j實例。 我們應該使用一種更有效的方法將圖形數據轉換為邊緣數據幀嗎?

edges_query=paste('MATCH (c0:user {userID:',as.character(cust_id),'})-[]->(l1) 
               OPTIONAL MATCH (l1)<-[]-(c1)
               where id(c1) <> id(c0)
               OPTIONAL MATCH (c1)-[]->(l2)
               where id(l2) <> id(l1)
               OPTIONAL MATCH (l2)<-[]-(c2)
               where id(c2) <> id(c0)
               OPTIONAL MATCH (c2)-[]->(l3)
               where id(l3) <> id(l2)
               OPTIONAL MATCH (l3)<-[]-(c3)
               where id(c3) <> id(c2)
               OPTIONAL MATCH (c3)-[]->(l4)
               where id(l4) <> id(l3)
               OPTIONAL MATCH (l4)<-[]-(c4)
               where id(c4) <> id(c3)
               OPTIONAL MATCH (c4)-[]->(l5)
               where id(l5) <> id(l4)
               OPTIONAL MATCH (l5)<-[]-(c5)
               where id(c5) <> id(c4)


               return 
               ID(c0) as c0_node_id
               , c0.userID as c0_user_id
               , ID(l1) as l1_node_id
               , LABELS(l1) as l1_node_type
               , ID(c1) as c1_node_id
               , c1.userID as c1_user_id
               , id(l2) as l2_node_id
               , labels(l2) as l2_node_type
               , ID(c2) as c2_node_id
               , c2.userID as c2_user_id
               , id(l3) as l3_node_id
               , labels(l3) as l3_node_type
               , ID(c3) as c3_node_id
               , c3.userID as c3_user_id
               , id(l4) as l4_node_id
               , labels(l4) as l4_node_type
               , ID(c4) as c4_node_id
               , c4.userID as c4_user_id
               , id(l5) as l5_node_id
               , labels(l5) as l5_node_type
               , ID(c5) as c5_node_id
               , c5.userID as c5_user_id
               ',sep='')

您應該在Cypher中使用可變長度路徑匹配語法。 該語法為[:REL_TYPE*min..max] ,例如[:HAS*..5] ,其中默認min為1。

您還應該使用參數而不是構建字符串。 在運行cypher函數時,例如在查詢中使用命名參數並將其替換為其值,而不是使用paste來嵌入cust_id

cypher(graph, "MATCH (n:User {userID: {cust_id} }) RETURN n.userID", cust_id=12345)

讓我為您展示一個示例圖形示例。

library(RNeo4j)
library(visNetwork)

vis = function(edges) {
  nodes = data.frame(id=unique(c(edges$from, edges$to)))
  nodes$label = nodes$id
  visNetwork(nodes, edges)
}

graph = startGraph("http://localhost:7474/db/data")

query = "
MATCH p = (:User {userID: {cust_id}})-[:HAS*..5]-(:User)
WITH [x IN nodes(p) WHERE x:User] AS users
UNWIND range(1, size(users) - 1) AS idx
WITH users[idx - 1] AS from, users[idx] AS to
RETURN DISTINCT from.userID AS from, to.userID AS to;
"

edges = cypher(graph, query, cust_id="Tom Cruise")
vis(edges)

我編輯了Neo4j隨附的電影圖以適合您的模型。 上面的代碼在RStudio中為我提供了以下內容:

visNetwork

然后,您可以在帶有renderVisNetwork的Shiny應用程序中輕松使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM