简体   繁体   English

复杂的neo4j密码查询可遍历图形并提取特定标签的节点,并在可选匹配中使用它们

[英]Complex neo4j cypher query to traverse a graph and extract nodes of a specific label and use them in optional match

I have a huge database of size 260GB, which is storing a ton of transaction information. 我有一个容量为260GB的巨大数据库,该数据库存储大量交易信息。 It has Agent, Customer,Phone,ID_Card as the nodes. 它具有Agent,Customer,Phone,ID_Card作为节点。 Relationships are as follows: Agent_Send, Customer_Send,Customer_at_Agent, Customer_used_Phone,Customer_used_ID. 关系如下:Agent_Send,Customer_Send,Customer_at_Agent,Customer_used_Phone,Customer_used_ID。

A single agent is connected to many customers .And hence hitting the agent node while querying a path is not feasible. 单个代理连接到许多客户,因此在查询路径时触及代理节点是不可行的。 Below is my query: 以下是我的查询:

match p=((ph: Phone {Phone_ID : "3851308.0"})-[r:Customer_Send 
  | Customer_used_ID | Customer_used_Phone *1..5]-(n2)) 
with nodes(p) as ns 
return extract (node in ns | Labels(node) ) as Labels

I am starting with a phone number and trying to extract a big "Customer" network. 我从一个电话号码开始,尝试提取一个大的“客户”网络。 I am intentionally not touching the "Customer_at_Agent" relationship in the above networked query as it is not optimal as far as performance is concerned. 我故意不触及上述网络查询中的“ Customer_at_Agent”关系,因为就性能而言它不是最佳的。

So, the idea is to extract all the "Customer" labeled nodes from the path and match it with [Customer_at_Agent] relationship. 因此,该想法是从路径中提取所有带有“ Customer”标签的节点,并将其与[Customer_at_Agent]关系进行匹配。

For instance , something like: 例如,类似:

match p=((ph: Phone {Phone_ID : "3851308.0"})-[r:Customer_Send 
  | Customer_used_ID | Customer_used_Phone *1..5]-(n2)) 
with nodes(p) as ns 
return extract (node in ns | Labels(node) ) as Labels 
of "type customer as c " 
optional match (c)-[r1:Customer_at_Agent]-(n3) 
return distinct p,r1

I am still new to neo4j and cypher and I am not able to figure out a hack to extract only "customer" nodes from the path and use that in the optional match. 我对neo4j和cypher还是陌生的,而且我无法找出一种从路径中仅提取“客户”节点并将其用于可选匹配的黑客。

Thanks in advance. 提前致谢。

Use filter notation instead of extract and you can drop any nodes that aren't labelled right. 使用过滤器符号而不是提取,您可以删除所有未正确标记的节点。 Try out this query instead: 请尝试以下查询:

MATCH p = (ph:Phone {Phone_ID : "3851308.0"}) - [:Customer_Send|:Customer_used_ID|:Customer_used_Phone*1..5] - ()
WITH ph, [node IN NODES(p) WHERE node:Customer] AS customer_nodes
UNWIND customer_nodes AS c_node
OPTIONAL MATCH (c_node) - [r1:Customer_at_Agent] - ()
RETURN ph, COLLECT(DISTINCT r1)

So the second line takes the phone number and the path generated and gives you a list of nodes that have the Customer label as customer_nodes . 因此,第二行使用电话号码和生成的路径,并为您提供了一个带有Customer标签的节点列表,它们是customer_nodes You then unwind this list so you have individual nodes you can use in path matching. 然后,您可以展开此列表,以便拥有可以在路径匹配中使用的单个节点。 Line 4 performs your optional match and finds the r1 you're interested in, then line 5 will return the phone number node you started with and a collection of all of the r1 relationships that you found on customer nodes hooked up to that phone number. 第4行执行您的可选匹配并找到您感兴趣的r1 ,然后第5行将返回您开始使用的电话号码节点,以及在与该电话号码关联的客户节点上找到的所有r1关系的集合。

UPDATE: I added some modifications to clean up your first query line as well. 更新:我还添加了一些修改以清理您的第一个查询行。 If you aren't going to use an alias (like r or n2 in the first line), then don't assign them in the first place; 如果您不打算使用别名(例如第一行中的rn2 ),则不要首先分配它们; they can affect performance and cause confusion. 它们会影响性能并引起混乱。 Empty nodes and relationships are totally fine if you don't actually have any restrictions to place on them. 如果您实际上对它们没有任何限制,则空节点和关系完全可以。 You also don't need parentheses to mark off a path; 您也不需要括号来标记路径。 they are used as a core part of Cypher's ASCII art to signify nodes, so I find they are more confusing than helpful. 它们被用作Cypher ASCII艺术的核心部分来表示节点,因此我发现它们比帮助更令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM