重新计算基于关系链的Neo4J关系。

huangapple go评论58阅读模式
英文:

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 属于 T1T2T3
A1 存在于 T1T4
A2 存在于 T3T4
A3 存在于 T1T2T4
A1 发出了 I1
A2 发出了 I2
A3 发出了 I3

在这里,我将简化为:
U1 可以看到 I1I2I3

针对 Question #1 的后续:

我收到了一个消息,ERP 将 A1A3T1 中分离出来。
我删除了 A1->T1A3->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 -&gt; :Territory &lt;- :Account -&gt; :Invoice.<br>
:User -&gt; :Territory relation is provided by the Active Directory.<br>
:Account -&gt; :Territory and :Account -&gt; 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]-&gt;: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-&gt;T1, A3-&gt;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

  1. 你使用这个查询创建了:CAN_SEE关系:

    match (u:User)-->(:Territory)<--(:Account)-->(i:Invoice)
    merge (u)-[:CAN_SEE]->(i)
    

    MERGE应该能够防止创建多个关系。

  2. & 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,

  1. 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

  1. & 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

  1. 使用以下查询来更新图形,当特定帐户(例如,A1)被删除时。该查询使用 DETACH DELETE 来删除匹配的路径以及所有关系(包括 BELONGS_TOCAN_SEE)的所有已删除节点。

    MATCH p=(a:Account)-[:ISSUED]->(:Invoice)-[:HAS]->(:InvoiceEntry)
    WHERE a.id = 'A2'
    DETACH DELETE p
    
  2. 使用此查询来断开用户(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.

  1. 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 (including BELONGS_TO and CAN_SEE) for all deleted nodes.

    MATCH p=(a:Account)-[:ISSUED]-&gt;(:Invoice)-[:HAS]-&gt;(:InvoiceEntry)
    WHERE a.id = &#39;A2&#39;
    DETACH DELETE p
    
  2. Use this query to disconnect a user (U1) from a territory (T3), and to delete all the CAN_SEE relationships from that user to all invoices issued by all accounts in the territory:

    MATCH  (u:User)-[bt:BELONGS_TO]-&gt;(t:Territory)&lt;-[:EXISTS_IN]-(:Account)-[:ISSUED]-&gt;(:Invoice)&lt;-[cs:CAN_SEE]-(u)
    WHERE u.id = &#39;U1&#39; AND t.id = &#39;T3&#39;
    DELETE bt, cs
    

huangapple
  • 本文由 发表于 2023年6月15日 23:56:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定