在查询中定义IN子句时,Neo4J Cypher查询会创建多个节点

时间:2013-11-24 16:18:09

标签: neo4j cypher

我看到一个非常奇怪的问题,当我运行以下查询时,Cypher查询正在创建多个节点,并且创建的节点数取决于查询的IN子句中定义的元素数量

MATCH (shipper:Shipper)-[:SHIPS_PRODUCT_TYPE]->(prodType:ProductType) 
WHERE shipper.InternalId = 1
AND prodType.InternalId IN [11,3,1]
CREATE (load:Shipment {
      InternalId: 5,
      NodeType: "Shipment",
      Created: "2013-11-24T18:25:12.3179997+05:30",
      Modified: "0001-01-01T00:00:00+00:00"})
RETURN distinct load AS load, shipper AS shipper 

我希望此查询只返回一行数据,但返回3行作为创建3" load:Shipment" 节点具有相同的数据

我是否遗漏了有关如何使用此IN子句的内容,因为我甚至不在本示例的create语句中使用匹配项?

修改

顺便说一下,我忘了提到我也尝试在下面介绍With语句,即使那时我最终得到了多个节点

MATCH (shipper:Shipper)-[:SHIPS_PRODUCT_TYPE]->(prodType:ProductType) 
WHERE shipper.InternalId = 1
AND prodType.InternalId IN [11,3,1]
WITH shipper, prodType
CREATE (shipper)-[:HAS_PUBLISHED]->(load:Shipment {
      InternalId: 5,
      NodeType: "Shipment",
      Created: "2013-11-24T18:25:12.3179997+05:30",
      Modified: "0001-01-01T00:00:00+00:00"})-[:CONTAINS_PRODUCT]->(prodType)
RETURN distinct load AS load, shipper AS shipper 

此致 基兰

1 个答案:

答案 0 :(得分:1)

这与IN无关。您正在匹配初始MATCH / WHERE中的多个结果,因此它为每个结果创建一个节点。从示例中,我甚至不确定您为什么要进行初始查询,因为它根本没有在第二部分中使用。我想你的意思是通过关系连接你的新节点?也许您应该在RC1中查看MERGE命令,它允许您“匹配或创建”节点或关系。

更新

您可能希望使用CREATE UNIQUE或MERGE而不仅仅是CREATE,以避免重复的节点。也许是这样的:

MATCH (shipper:Shipper)-[:SHIPS_PRODUCT_TYPE]->(prodType:ProductType) 
WHERE shipper.InternalId = 1
AND prodType.InternalId IN [11,3,1]
WITH shipper, prodType
CREATE UNIQUE (shipper)-[:HAS_PUBLISHED]->(load:Shipment {
    InternalId: 5,
    NodeType: "Shipment",
    Created: "2013-11-24T18:25:12.3179997+05:30",
    Modified: "0001-01-01T00:00:00+00:00"})
WITH shipper, load, prodType
CREATE UNIQUE (load)-[:CONTAINS_PRODUCT]->(prodType)
RETURN distinct load AS load, shipper AS shipper