找到你要的答案

Q:Creating unique structures in Neo4j with them having nodes that are part of another structure

Q:创造独特的结构与他们的Neo4j是另一部分的节点结构

Let's say that we have n nodes with label :Test and a unique property called type.

UNWIND[{ type:"a" }, { type:"b" }, { type:"c" }, { type:"d" }] AS x
MERGE (t:Test { type: x.type })
RETURN t

That looks like this

Now let's introduce a node of label :Collection. The purpose if this node is to have a unique relationship pattern with the :Test nodes.

MATCH (a:Test { type:"a" }),(b:Test { type:"b" })
CREATE UNIQUE (x:Collection)-[:HAS]->(a),(x:Collection)-[:HAS]->(b)
Return *

The problem that I face starts occurring when I try to make another unique structure, like the previous one, but with some nodes in common.

MATCH (a:Test { type:"a" })
CREATE UNIQUE (x:Collection)-[:HAS]->(a)
RETURN *

The expected result is that another node of label :Collection gets created and linked to :Test {type:"a"} but the actual result is that it matches the previous data structure and returns that instead of creating a new one.

The expected result should have 2 :Collection nodes, one linked to type:"a", the other one linked to type:"a" and type:"b".

Any input kind of input will be very appreciated :D

假设我们有n个带标签的节点:测试和一个称为类型的唯一属性。

UNWIND[{ type:"a" }, { type:"b" }, { type:"c" }, { type:"d" }] AS x
MERGE (t:Test { type: x.type })
RETURN t

看起来像这样

现在让我们介绍一个标签节点:集合。如果此节点与测试节点具有唯一关系模式的用途。

MATCH (a:Test { type:"a" }),(b:Test { type:"b" })
CREATE UNIQUE (x:Collection)-[:HAS]->(a),(x:Collection)-[:HAS]->(b)
Return *

当我尝试做另一个独特的结构,像以前的一个,但有一些共同的节点,我面临的问题开始发生。

MATCH (a:Test { type:"a" })
CREATE UNIQUE (x:Collection)-[:HAS]->(a)
RETURN *

预期的结果是,另一个节点的标签:集合创建并链接到:测试{类型:“A”},但实际结果是,它匹配以前的数据结构,并返回,而不是创建一个新的。

预期的结果应该有2个:集合节点,一个链接类型:“A”,另一个链接类型:“A”和类型:“B”。

任何输入类型的输入将非常赞赏:D

answer1: 回答1:

From the neo4j docs on CREATE UNIQUE:

CREATE UNIQUE is in the middle of MATCH and CREATE — it will match what it can, and create what is missing. CREATE UNIQUE will always make the least change possible to the graph — if it can use parts of the existing graph, it will.

You add Collection nodes without any properties. I think if CREATE UNIQUE finds a Collection node, it will use it. This is how CREATE UNIQUE is supposed to work.

So if you want a new Collection that is linked to some Test nodes, you can either add some unique properties to the node:

MATCH (a:Test { type:"a" })
CREATE UNIQUE (x:Collection {key: 'unique value'})-[:HAS]->(a)
RETURN *

Or create it in a separate step:

MATCH (a:Test { type:"a" })
CREATE (x:Collection)
CREATE (x)-[:HAS]->(a)
RETURN *

Or use MERGE instead of CREATE UNIQUE.

从Neo4j的文档创建独特的:

CREATE UNIQUE is in the middle of MATCH and CREATE — it will match what it can, and create what is missing. CREATE UNIQUE will always make the least change possible to the graph — if it can use parts of the existing graph, it will.

您添加集合节点没有任何属性。我认为如果创建唯一的集合节点,它将使用它。这就是如何创造独一无二的工作。

因此,如果希望与某些测试节点链接的新集合,可以向该节点添加一些独特的属性:

MATCH (a:Test { type:"a" })
CREATE UNIQUE (x:Collection {key: 'unique value'})-[:HAS]->(a)
RETURN *

或创建它在一个单独的步骤:

MATCH (a:Test { type:"a" })
CREATE (x:Collection)
CREATE (x)-[:HAS]->(a)
RETURN *

或使用合并而不是创建唯一的。

data-structures  graph  neo4j  cypher