簡體   English   中英

Neo4J - 將所有關系從一個節點復制到另一個節點(C# Wrapper)

[英]Neo4J - Copy all relationships from one to another node (C# Wrapper)

我目前正在努力解決一個問題,目前我還沒有找到任何解決方法。 (我正在使用 NEO4J C# 庫)

我需要將兩個節點合並到第三個節點中,並將這兩個節點中的所有關系(類型和屬性)復制到我新創建的第三個節點:

(a:Label)-[r]-()
(b:Label)-[r2]-()
(c:Label)

我已經能夠正確檢索我的兩個第一個節點並將其合並到在數據庫中創建的第三個節點中,但我正在努力將所有關系從兩個第一個節點復制到第三個節點。

我嘗試了幾件事但沒有成功,例如:

        using (GraphClient graphClient = GetGraphClient())
        {
            var inputString = string.Format("({0}:{1})-[r]->(n), ({2}:{3})", "a", typeof(Label).Name, "b", typeof(Label).Name);
            var query = graphClient.Cypher
                    .SendQueryOnMaster()
                    .Match(inputString)
                    .Where((Label a) => a.Id == from.Id)
                    .AndWhere((Label b) => b.Id == to.Id)
                    .Create("(b)-[r2:type(r)]->(n)");
            query.ExecuteWithoutResults();
        }

將所有關系從一個節點復制到另一個節點可能是人們可能遇到的用例:)

有沒有辦法讓它工作?

謝謝

更新,我找到了一種使用 C# 包裝器將所有關系從一個節點復制到另一個節點的方法。

    internal static void DuplicateRelationships<T>(T from, T to) where T : IDataObject
    {
        string aVariable = "a";
        string bVariable = "b";
        string nVariable = "n";
        string relationVariable = "r";
        string newRelation = "r2";
        string relationsVariable = "rels";
        string relationPostCollectVariable = "rel";

        Guid fromId = from.Id;
        Guid toId = to.Id;

        foreach (string relation in CypherVerbs.GetAllVerbs())
        {
            using (GraphClient graphClient = GetGraphClient())
            {
                /*-[r]->*/
                graphClient.Cypher
                    .SendQueryOnMaster()
                    .Match(string.Format("({0}:{1})-[{2}:{3}]->({4}), ({5}:{6})", aVariable, from.GetType().Name, relationVariable, relation, nVariable, bVariable, to.GetType().Name))
                    .Where((T a) => a.Id == fromId)
                    .AndWhere((T b) => b.Id == toId)
                    .With(string.Format("COLLECT({0}) AS {1}, {2}, {3}, {4}", relationVariable, relationsVariable, aVariable, bVariable, nVariable))
                    .ForEach(string.Format("({0} in {1} | ", relationPostCollectVariable, relationsVariable))
                    .Create(string.Format("({0})-[{1}:{2}]->({3})", bVariable, newRelation, relation, nVariable))
                    .Set(string.Format("{0} += {1})", newRelation, relationPostCollectVariable))
                    .ExecuteWithoutResults();

                /*<-[r]-*/
                graphClient.Cypher
                    .SendQueryOnMaster()
                    .Match(string.Format("({0}:{1})<-[{2}:{3}]-({4}), ({5}:{6})", aVariable, from.GetType().Name, relationVariable, relation, nVariable, bVariable, to.GetType().Name))
                    .Where((T a) => a.Id == fromId)
                    .AndWhere((T b) => b.Id == toId)
                    .With(string.Format("COLLECT({0}) AS {1}, {2}, {3}, {4}", relationVariable, relationsVariable, aVariable, bVariable, nVariable))
                    .ForEach(string.Format("({0} IN {1} | ", relationPostCollectVariable, relationsVariable))
                    .Create(string.Format("({0})<-[{1}:{2}]-({3})", bVariable, newRelation, relation, nVariable))
                    .Set(string.Format("{0} += {1})", newRelation, relationPostCollectVariable))
                    .ExecuteWithoutResults();
            }
        }
    }

這是密碼:

MATCH (a:Test {props1:"1"}), (b:Test {props3:"3"})
WITH a,b
MATCH (a)-[r:LINKED_TO]->(c)
WITH COLLECT(r) AS rels, a, b, c
FOREACH (rel in rels |
       CREATE (b)-[r:LINKED_TO]->(c)
       SET r+=rel
)

編輯:從 Neo4j 3.0 開始,您可以使用存儲過程來更有效地執行此操作,Michael Hunger 開發了幾個模板存儲過程,包括將所有關系從一個節點復制到另一個過程。

這是存儲過程存儲庫的鏈接: https : //github.com/neo4j-contrib/neo4j-apoc-procedures

這是圖形重構文檔的鏈接: https : //neo4j-contrib.github.io/neo4j-apoc-procedures/#_graph_refactoring

不幸的是,這在Neo4j是不可能的,我想你已經在 StackOverflow 上找到了其他帖子:

如果您在編譯時知道關系的 Label 是可能的,但它不適用於多個/動態標簽類型。 很抱歉,沒有更好的答案可以給您。

暫無
暫無

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

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