英文:
Recalculating Neo4J relationships based on chain of relationships
问题
Question #1:
在将 :Account 从 :Territory 中移除并保留有效的 :CAN_SEE 关系后,刷新 :User 和 :Invoice 之间的关系的查询是什么?
Question #2:
同上,但是 :User 从 :Territory 中移除,不能再看到 :Invoice。
UPDATE 16.06.23:
我的主要关系链是 :User -> :Territory <- :Account -> :Invoice
。
:User -> :Territory
关系由 Active Directory 提供。
:Account -> :Territory
和 :Account -> :Invoice
关系来自 ERP 系统。
为了避免昂贵的计算,因为单个 :User 可能有数百个 :Territory,而一个 :Account 可能在数百个 :Territory 中,我想引入一个“快捷”关系 :User-[:CAN_SEE]->:Invoice
。
示例案例:
U1
属于 T1
、T2
、T3
。
A1
存在于 T1
、T4
。
A2
存在于 T3
、T4
。
A3
存在于 T1
、T2
、T4
。
A1
发出了 I1
。
A2
发出了 I2
。
A3
发出了 I3
。
在这里,我将简化为:
U1
可以看到 I1
、I2
、I3
。
针对 Question #1 的后续:
我收到了一个消息,ERP 将 A1
和 A3
从 T1
中分离出来。
我删除了 A1->T1
、A3->T1
关系。
我需要重新计算 [:CAN_SEE]
,因为 U1 失去了对 I1 的 [:CAN_SEE]
,因为 U1 到 A1 不再具有有效的领地关系。U1 仍然可以看到 I3,因为 A3 失去了 T1,但 T2 确保了到 I3 的链。
查询应该是什么样的?
针对 Question #2 的后续:
Active Directory 已更新:
U1 与 T1 分离。
U1 不能再看到 I1。
在这种情况下,查询应该是什么样的。
用另一种方式表达我的问题:
我知道如何基于现有关系创建新关系。我不知道如何重新使旧关系无效并删除不再有效的关系。
英文:
I have a long chain of relations:
:User can have multiple :Territory(ies)
:Account can belong in multiple :Territory(ies)
:Account can be assigned to a multiple :User(s)
:Account can have an :Invoice(s)
:Invoice can have an :InvoiceEntry(ies)
To sum up:
:User-[:BELONGS_TO]->:Territory<-[:EXISTS_IN]-:Account-[:ISSUED]->:Invoice-[:HAS]->:InvoiceEntry
I can simplify the relation to
:User-[:CAN_SEE]->:Invoice
Since I'm not proficient with Cypher, my questions are:
Question #1.
What would be the query to refresh the relationships between :User and :Invoice once the :Account is being removed from :Territory and leave valid relationships :CAN_SEE untouched.
Question #2.
Same as above but :User is removed from :Territory and can no longer see :Invoice.
UPDATE 16.06.23
My primary relation chain is :User -> :Territory <- :Account -> :Invoice
.<br>
:User -> :Territory
relation is provided by the Active Directory.<br>
:Account -> :Territory
and :Account -> Invoice
relations are fetched from ERP system.
In order to avoid costly calculation, because single :User
can have hundreds of :Territory
and one :Account
can be in a hundreds of :Territory
, I want to introduce a "shortcut" relationship :User-[:CAN_SEE]->:Invoice
.
Example case:
U1
belongs to T1
, T2
, T3
.<br>
A1
exists in T1
, T4
.<br>
A2
exists in T3
, T4
.<br>
A3
exists in T1
, T2
, T4
.<br>
A1
issued I1
.<br>
A2
issued I2
.<br>
A3
issued I3
. <br>
Here, I would end up having it simplified to:<br>
U1
can see I1
, I2
, I3
.
Follow-up to Question #1:<br>
I receive a payload that the ERP detached A1
and A3
from T1
.<br>
I remove A1->T1
, A3->T1
relationships.<br>
I need to recalculate [:CAN_SEE]
since U1 loses [:CAN_SEE]
to I1
because U1
to A1
has no longer valid territory relations. U1
still can see I3
because A3
loses T1
but T2
ensures the chain to I3
.
How would the query look like?
Follow-up to Question #2:<br>
Active Directory got updated:<br>
U1
is detached from T1
.
U1
is no longer able to see I1
.
How would the query look like in this situation.
Putting my questions in another words:
I know how to create new relationships based on existing ones. I have trouble understanding how to re-invalidate old ones and remove those if no longer valid.
答案1
得分: 1
-
你使用这个查询创建了:CAN_SEE关系:
match (u:User)-->(:Territory)<--(:Account)-->(i:Invoice) merge (u)-[:CAN_SEE]->(i)
MERGE应该能够防止创建多个关系。
-
& 3. 如果我理解正确,无论在哪种情况下,您都希望检查从用户到发票的路径是否仍然存在,并且如果路径断开,则删除CAN_SEE关系。以下查询应该可以做到这一点:
match (u:User)-[r:CAN_SEE]-(i:Invoice) where not exists((u)-->(:Territory)<--(:Account)-->(i)) delete r
在生产环境中应用之前,请进行测试,我没有进行测试。
英文:
if I get the questions right,
-
you create :CAN_SEE relations with this query
match (u:User)-->(:Territory)<--(:Account)-->(i:Invoice)
merge (u)-[:CAN_SEE]->(i)
MERGE should prevent creation of multiple relations
-
& 3. if I get you right, in both case, you want to check that the path from user to invoice still exists and delete the CAN_SEE relation if the path is broken. the following query should do that
match (u:User)-[r:CAN_SEE]-(i:Invoice) where not exists((u)-->(:Territory)<--(:Account)-->(i))
delete r
do test before applying in production though, I did not test
答案2
得分: 0
-
使用以下查询来更新图形,当特定帐户(例如,
A1
)被删除时。该查询使用 DETACH DELETE 来删除匹配的路径以及所有关系(包括BELONGS_TO
和CAN_SEE
)的所有已删除节点。MATCH p=(a:Account)-[:ISSUED]->(:Invoice)-[:HAS]->(:InvoiceEntry) WHERE a.id = 'A2' DETACH DELETE p
-
使用此查询来断开用户(
U1
)与领Territory(T3
)之间的关系,并删除该用户到Territory中所有帐户发出的所有发票的CAN_SEE
关系:MATCH (u:User)-[bt:BELONGS_TO]->(t:Territory)<-[:EXISTS_IN]-(:Account)-[:ISSUED]->(:Invoice)<-[cs:CAN_SEE]-(u) WHERE u.id = 'U1' AND t.id = 'T3' DELETE bt, cs
英文:
I assume that your questions are for updating a graph that already contains the appropriate CAN_SEE
relationships.
-
Use the following query to update the graph when a specific account (say,
A1
) is deleted. The query uses DETACH DELETE to delete matching paths and all relationships (includingBELONGS_TO
andCAN_SEE
) for all deleted nodes.MATCH p=(a:Account)-[:ISSUED]->(:Invoice)-[:HAS]->(:InvoiceEntry) WHERE a.id = 'A2' DETACH DELETE p
-
Use this query to disconnect a user (
U1
) from a territory (T3
), and to delete all theCAN_SEE
relationships from that user to all invoices issued by all accounts in the territory:MATCH (u:User)-[bt:BELONGS_TO]->(t:Territory)<-[:EXISTS_IN]-(:Account)-[:ISSUED]->(:Invoice)<-[cs:CAN_SEE]-(u) WHERE u.id = 'U1' AND t.id = 'T3' DELETE bt, cs
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论