简体   繁体   English

在 Neo4j / Cypher 中正确建模数据的问题

[英]Trouble with correctly modeling data in Neo4j / Cypher

I am a beginner with Neo4j/Cypher and I'm having trouble modeling my data correctly.我是 Neo4j/Cypher 的初学者,我无法正确建模我的数据。

The Data has following relationship:数据有如下关系:

MANUFACTURER_A (unique) -> PRODUCT_A (unique just withing MANUFACTURER_A) -> CUSTOMER_A (globally unique)
MANUFACTURER_A (unique) -> PRODUCT_A (unique just withing MANUFACTURER_A) -> CUSTOMER_B (globally unique) 
MANUFACTURER_B (unique) -> PRODUCT_A (unique just withing MANUFACTURER_B) -> CUSTOMER_A (globally unique)

I am not able to make Product_A unique within a Manufacturer .我无法使Product_AManufacturer独一无二。 I always get one line from Manufacturer_A to Product_A and another one from Manufacturer_B but I actually want two Product_A nodes (one from each Manufacturer ), but just one Product_A node per Manufacturer .我总是从一行Manufacturer_AProduct_A从另一个Manufacturer_B但其实我想要两个Product_A节点(每个Manufacturer ),但只是一个Product_A每个节点Manufacturer

I tried the following:我尝试了以下方法:

CREATE CONSTRAINT ON (c:MANUFACTURER) ASSERT c.MANUFACTURER IS UNIQUE;
CREATE CONSTRAINT ON (c:CUSTOMER) ASSERT c.CUSTOMER IS UNIQUE;

LOAD CSV WITH HEADERS FROM
'file:///small.csv' AS line

WITH line LIMIT 20
MERGE (CUSTOMER:Customer {Name: line.CUSTOMER})
MERGE (MANUFACTURER:Manufacturer {Name:line.MANUFACTURER})
MERGE (PRODUCT:Product {Name: line.PRODUCT})

MERGE (MANUFACTURER)<-[:PRODUCES]-(PRODUCT)
MERGE (CUSTOMER)<-[:CONSUMES]-(PRODUCT)

;

How would I model that correctly?我将如何正确建模?

In this case, you don't want to MERGE the :PRODUCT node alone, but as part of a pattern connected to your merged manufacturer variable, that provides the context that the pattern you are looking for must be connected, and if no such pattern exists, the non-bound parts will be created.在这种情况下,您不想单独 MERGE :PRODUCT 节点,而是作为连接到合并制造商变量的模式的一部分,它提供了您正在寻找的模式必须连接的上下文,如果没有这样的模式存在,将创建非绑定部分。

...
MERGE (MANUFACTURER:Manufacturer {Name:line.MANUFACTURER})
MERGE (MANUFACTURER)<-[:PRODUCES]-(PRODUCT:Product {Name: line.PRODUCT})
...

So it won't matter if a product of that name exists elsewhere in the graph, as long as one isn't found connected to that manufacturer, it will be created as part of the MERGE.因此,该名称的产品是否存在于图中的其他位置并不重要,只要未找到与该制造商相关的产品,它将作为 MERGE 的一部分创建。

This knowledge base article may be helpful as well, as it covers this application and more:这篇知识库文章也可能有所帮助,因为它涵盖了此应用程序以及更多内容:

https://neo4j.com/developer/kb/understanding-how-merge-works/ https://neo4j.com/developer/kb/understanding-how-merge-works/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM