Search code examples
neo4jcypherneo4jclient

How to create a unique path inside a sub graph using Neo4J


This is kind of an extension to my earlier question to which I have not received any replies yet which is posted here

I am trying to figure out how to create a graph like below enter image description here

But instead I am ending up with

enter image description here

Issue is that the examples of MERGE I see are all based on Label and as label is kind of universal, it ends up picking up warehouses of Shipper 1 while creating shipper 2 as they have same names. Kind of same issue I have with date and month in the previous post.

my code in Neo4JClient would look somewhat like this (hand typed example here)

var qry = GraphClient.Cypher
                .Merge("(whse:Warehouse{ Name: {whseName}})")
                .OnCreate("whse").Set("whse= {newWhseData}")
                .With("whse")
                .Start(new { root = shipper2Node})
                .CreateUnique("(root)-[:HAS_WAREHOUSE]->(whse)")
                .WithParams(new { whseName = newWhse.Name, newWhseData= newWhse})
                .Return(whse => whse.Node<Warehouse>());
 var whseNode = qry.Results.Single();

I really need to ensure that i don't create a duplicate warehouses for the same shipper, and hence the use of Merge in my code, I understand that Merge and Match is replacing the Create Unique in 2.0

I apologize here for re-posting here, but I was not sure how else to get help.

Thanks in advance, Kiran


Solution

  • You would use create-unique for the sub-graph, but you're making a good point that we should take this into consideration when evolving MERGE to work with Paths

    Something like:

    var qry = GraphClient.Cypher
                .Start(new { root = shipper2Node})
                .CreateUnique("(root)-[:HAS_WAREHOUSE]->(whse Name: {whseName})")
                .Set("whse= {newWhseData}")
                .WithParams(new { whseName = newWhse.Name, newWhseData= newWhse})
                .Return(whse => whse.Node<Warehouse>());