是否删除节点之间不必要的关系



我试图使用数据构建一个图模型,下面是密码查询

LOAD CSV WITH HEADERS FROM 'file:///y.csv' AS line
MERGE (a:Employee {empid:line.EmpID})
ON CREATE SET a.firstname = line.FirstName, a.lastname = line.LastName
MERGE (y:Year {year:toInteger(line.YearofJoining)}) 
ON CREATE SET y.month = line.MonthNamofJoining
MERGE (c:Location {city:line.City}) 
ON CREATE SET c.pincode = line.PinCode,c.county = line.County,c.state = 
line.State,c.region = line.Region
MERGE (ag:Age {age:toInteger(line.AgeinYrs)})
MERGE (a)-[:AGE]->(ag)
MERGE (ag)-[:LOCALITY]->(c)
MERGE (c)-[:JOINING_YEAR]->(y)

我需要返回四名员工之间的所有连接路径,所以我尝试了以下查询

MATCH p = (a:Employee)-[:AGE]->(ag)-[:LOCALITY]-(c)-[:JOINING_YEAR]-(y)
WHERE a.empid IN ['840300','840967','346058','320954']
return p limit 25

但是我得到的结果是正确的,但是有很多不必要的路径。我正在上传结果图,请检查并更正我做错的地方。结果图

在导入查询中可能需要修复以下几点:

年份节点具有误导性。我认为您应该将月份属性提取到一个单独的节点,如下所示:

MERGE (y:Year {year:toInteger(line.YearofJoining))
MERGE (m:Month {month:line.MonthNamofJoining})-[:MONTH_IN_YEAR]->(y)

此外,模型似乎是错误的。目前,仓位通过JOINING_year链接到年份(或很快:一年中的某个月(。年龄与位置相关联。这似乎是有道理的。

您可能想要一个中间节点来表示员工已经加入了一个位置(假设Neo4j不支持2个以上节点之间的关系(。

LOAD CSV WITH HEADERS FROM 'file:///y.csv' AS line
MERGE (a:Employee {empid:line.EmpID})
ON CREATE SET a.firstname = line.FirstName, a.lastname = line.LastName
MERGE (ag:Age {age:toInteger(line.AgeinYrs)})
MERGE (a)-[:AGE]->(ag)
MERGE (y:Year {year:toInteger(line.YearofJoining))
MERGE (m:Month {month:line.MonthNamofJoining})-[:MONTH_IN_YEAR]->(y)
MERGE (c:Location {city:line.City}) 
ON CREATE SET c.pincode = line.PinCode,c.county = line.County,c.state = 
line.State,c.region = line.Region
MERGE (j:Join {empid:line.EmpID}) // need a property to merge on
MERGE (a)-[:JOINED]->(j)
MERGE (j)-[:LOCALITY]->(c)
MERGE (j)-[:JOINING_MONTH]->(m)

您的读取查询变为:

MATCH p = (:Location)<-[:LOCALITY]-(:Join)<-[:JOINED]-(a:Employee)-[:AGE]->(:Age)
WHERE a.empid IN ['840300','840967','346058','320954']
return p limit 25

不相关的格式说明:属性的推荐事例是camelCase(例如empId而不是empId(,关系类型的推荐事例为SNAKE_case(例如JOINING_YEAR而不是JOININGYEAR(。按照惯例,关系类型通常是动词。

相关内容

  • 没有找到相关文章

最新更新