从neo4j Graph数据库中获取特定路径

时间:2014-11-07 05:37:17

标签: neo4j cypher

我们正在使用Neo4j 2.1.4社区版。

我们在获取neo4j中的特定路径方面遇到了一些问题。

以下是使用的csv文件。

enter image description here

在图形数据库中,我们正在创建Product,Company,Country和Zipcode节点以及每个级别的关系类型为“MyRel”。在上面的数据中,我们想要区分每个路径,

Mobile,Google,US,88888 - as path1

Mobile,Goolge,US - as path2

Mobile,Goolge - as path3

这就是为什么我们在数据文件中再创建了一个名为Path的列,并将Path值保留为relatioship属性。因此,每当有人想要查看不同的路径时,他可以根据Relationship属性查询1或2或3.例如,每当我们查询关系属性时,我们应该得到Mobile,Google ,US

但每当我这样做时,在图中它会为Country和Zipcode创建虚拟节点。这是由于第2和第3行的zip和国家/地区值为空(null)。

使用的查询:

LOAD CSV WITH HEADERS FROM "file:C:\\WorkingFolder\\Neo4j\\EDGE_Graph_POC\\newdata\\trial1.csv " as file
MERGE (p:Product {Name:file.Product})
MERGE (comp:Company {Name:file.Company})
MERGE (c:Country {Name:file.Country})
MERGE (zip:Zipcode{Code:file.Zipcode})
CREATE (p)-[:MyRel{Path:file.Path}]->(comp)-[:MyRel{Path:file.Path}]->(c)-[:MyRel{Path:file.Path}]->(zip)

结果图:

enter image description here

那么如何避免创建虚拟节点?

有没有更好的替代选择来获得正确的路径?

谢谢,

2 个答案:

答案 0 :(得分:1)

首先,一个简单的解决方案是与其他清理图表的人一起遵循LOAD CSV查询。运行查询

MATCH (zip:Zipcode { Code : ''})<-[r]-()
DELETE zip, r

MATCH (c:Country { Name : ''})<-[r]-()
DELETE c, r

然后您将获得所需的图表。

答案 1 :(得分:0)

您可以使用

过滤掉它们
WHERE file.Country <> '' and file.Zipcode <> ''

并拆分你的CREATE例如。

CREATE (p)-[:MyRel{Path:file.Path}]->(comp)
WHERE file.Country <> '' and file.Zipcode <> ''
MERGE (c:Country {Name:file.Country})
MERGE (zip:Zipcode{Code:file.Zipcode})
CREATE (comp)-[:MyRel{Path:file.Path}]->(c)-[:MyRel{Path:file.Path}]->(zip)