Neo4j驱动程序未获取向后关系驱动程序记录

时间:2019-05-24 14:26:33

标签: neo4j neo4j-dotnet-driver

我正在尝试使用Neo4j C#驱动程序将所有关系连接到节点,我想获取所有传入和传出关系。

但是问题是我只能使用C#Neo4j驱动程序从节点获取传出关系,而不能使用传入关系,但是在查询窗口中,我可以同时运行两个查询。

这是我的图形数据:

enter image description here

如您所见,我想要传入我为其编写此Cypher查询的Group1节点的所有独特关系。

MATCH path=(e1)-[*]->(`GROUP1`: TRAVEL_PLAN { name: 'GROUP1' })
UNWIND [relationship in relationships(path) | type(relationship)] as pathRelationship
WITH DISTINCT pathRelationship
ORDER BY pathRelationship
RETURN pathRelationship as relationships

在浏览器中运行正常并且结果正确,但是问题是,当我将.NET驱动程序用于Neo4j时,它给了我一个空数组。

我还创建了一个 Neo4j sandbox editor online 供任何人尝试并解决。密码为:installation-fishes-evidence

如果有人可以解决,这就是要创建的示例数据:

MERGE (T4: COMPETENCY { name: 'T4' })
ON CREATE SET
    T4.id = randomUUID(),
    T4.type = 'COMPETENCY',
    T4.created = timestamp()
ON MATCH SET
    T4.lastUpdated = timestamp()

MERGE (C1: COMPETENCY { name: 'C1' })
ON CREATE SET
    C1.id = randomUUID(),
    C1.type = 'COMPETENCY',
    C1.created = timestamp()
ON MATCH SET
    C1.lastUpdated = timestamp()

MERGE (C2: COMPETENCY { name: 'C2' })
ON CREATE SET
    C2.id = randomUUID(),
    C2.type = 'COMPETENCY',
    C2.created = timestamp()
ON MATCH SET
    C2.lastUpdated = timestamp()

MERGE (C3: COMPETENCY { name: 'C3' })
ON CREATE SET
    C3.id = randomUUID(),
    C3.type = 'COMPETENCY',
    C3.created = timestamp()
ON MATCH SET
    C3.lastUpdated = timestamp()

MERGE (C4: COMPETENCY { name: 'C4' })
ON CREATE SET
    C4.id = randomUUID(),
    C4.type = 'COMPETENCY',
    C4.created = timestamp()
ON MATCH SET
    C4.lastUpdated = timestamp()

MERGE (GROUP1: TRAVEL_PLAN { name: 'GROUP1' })
ON CREATE SET
    GROUP1.id = randomUUID(),
    GROUP1.type = 'TRAVEL_PLAN',
    GROUP1.created = timestamp()
ON MATCH SET
    GROUP1.lastUpdated = timestamp()

---------------------------------------------------------------------------------------------------

MATCH (GROUP1: TRAVEL_PLAN {name:'GROUP1'})

MATCH (T4: COMPETENCY {name:'T4'}) 
MATCH (C1: COMPETENCY {name:'C1'})
MATCH (C2: COMPETENCY {name:'C2'})
MATCH (C3: COMPETENCY {name:'C3'})
MATCH (C4: COMPETENCY {name:'C4'})

CREATE (T4)-[:BELONG_TO]->(GROUP1)
CREATE (C1)-[:BELONG_TO]->(GROUP1)
CREATE (C2)-[:BELONG_TO]->(GROUP1)
CREATE (C3)-[:BELONG_TO]->(GROUP1)
CREATE (C4)-[:BELONG_TO]->(GROUP1)

---------------------------------------------------------------------------------------------------
CREATE INDEX ON :COMPETENCY(name)
----------------------------------------------------------------------------------------------------
CREATE INDEX ON :TRAVEL_PLAN(name)
----------------------------------------------------------------------------------------------------

这是我正在尝试的C#程序(DotNetFiddle here):

using Neo4j.Driver.V1;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GraphDemo
{
    public class Program
    {
        public static void Main()
        {
            IDriver driver = GraphDatabase.Driver("bolt://100.26.232.160:35027", AuthTokens.Basic("neo4j", "installation-fishes-evidence"));



            string forwardRelationStatement = new StringBuilder()
                                          .AppendLine(string.Format("MATCH path=(e1)<-[*]-(`{0}`: {1} {{ name: '{0}' }})", "GROUP1", "TRAVEL_PLAN"))
                                          .AppendLine("UNWIND [relationship in relationships(path) | type(relationship)] as pathRelationship")
                                          .AppendLine("WITH DISTINCT pathRelationship")
                                          .AppendLine("ORDER BY pathRelationship")
                                          .AppendLine("RETURN pathRelationship as relationships")
                                          .ToString();

            string backwardRelationStatement = new StringBuilder()
                                          .AppendLine(string.Format("MATCH path=(e1)-[*]->(`{0}`: {1} {{ name: '{0}' }})", "GROUP1", "TRAVEL_PLAN" ))
                                          .AppendLine("UNWIND [relationship in relationships(path) | type(relationship)] as pathRelationship")
                                          .AppendLine("WITH DISTINCT pathRelationship")
                                          .AppendLine("ORDER BY pathRelationship")
                                          .AppendLine("RETURN pathRelationship as relationships")
                                          .ToString();

            using (ISession session = driver.Session())
            {
                var resultCursor = session.Run(backwardRelationStatement);
                var res = resultCursor.SelectMany(x => x.Values.Values);

                var s = Newtonsoft.Json.JsonConvert.SerializeObject(res, Formatting.Indented);

                IEnumerable<string> relations = JsonConvert.DeserializeObject<IEnumerable<string>>(s);

                foreach (string relation in relations)
                    Console.WriteLine(relation);
            }
        }
    }
}

如果您提供任何提示,这将是一个很大的帮助。现在head了大约四个小时。

0 个答案:

没有答案