Neo4jClient Create Unique results in multiple duplicate nodes

╄→гoц情女王★ 提交于 2019-12-13 05:11:12

问题


I was wondering if some one can help me understand how Create Unique actually works in Neo4J. I am trying to register multiple addresses to a shipper node, where I expect the City, State and country to be same across the addresses so that they all point to the same node. However the issue is that the Create Unique is resulting in multiple instance of same node in the database.

Here is the function I have to register an address with the shipper which I call multiple times with same Shipper node, but different address pointing to same city.

public Node<Address> RegisterShipperAddress(Node<Shipper> shipper,AddressType addressType ,Address address, City city, State state, Country country)
    {
        var shipperRef = shipper.Reference;
        var addressRef = neo4jClient.Cypher.Start(new { shipperNode = shipperRef }).CreateUnique("(shipperNode)-[r:HAS_ADDRESS { TypeName:{TypeName} }]->(addressRef{address})").WithParam("TypeName", addressType.TypeName).WithParam("address", address).Return<Node<Address>>("addressRef").Results.FirstOrDefault();
        var cityRef = neo4jClient.Cypher.Start(new { addressNode = addressRef }).CreateUnique("(addressNode)-[:BELONGS_TO_CITY]->(cityRef{cityData})").WithParam("cityData", city).Return<Node<City>>("cityRef").Results.FirstOrDefault();
        var stateRef = neo4jClient.Cypher.Start(new { cityNode = cityRef }).CreateUnique("(cityNode)-[:BELONGS_TO_STATE]->(stateRef{stateData})").WithParam("stateData", state).Return<Node<State>>("stateRef").Results.FirstOrDefault();
        var countryRef = neo4jClient.Cypher.Start(new { stateNode = stateRef }).CreateUnique("(stateNode)-[:BELONGS_TO_COUNTRY]->(countryRef{stateData})").WithParam("stateData", state).Return<Node<Country>>("countryRef").Results.FirstOrDefault();
        return addressRef;
    }

Now suppose I call the the above method with different address as below:

//First Call....

RegisterShipperAddress(shipperNode,
                new AddressType { TypeName = "REGISTRED" },
                new Address
                {
                    Address1 = "151/1 ABC Main",
                    Address2 = "My PO 1234",
                    Email = "abc@abc.com",
                    Mobile = "123456",
                    Phone = "123456"
                },
                new City { InternalId = 1, Label = "MyCity" },
                new State { InternalId = 1, Label = "MyState" },
                new Country { InternalId = 1, Label = "MyCountry" }
                );

// Second call

RegisterShipperAddress(shipperNode,
                new AddressType { TypeName = "BILLING" },
                new Address
                {
                    Address1 = "1 X Main Road",
                    Address2 = "PO 555",
                    Email = "abc@abc.com",
                    Mobile = "123456",
                    Phone = "123456"
                },
                new City { InternalId = 1, Label = "MyCity" },
                new State { InternalId = 1, Label = "MyState" },
                new Country { InternalId = 1, Label = "MyCountry" }
                );

I end up with multiple instances of same node. Just wondering if its even possible to achieve this using the Create Unique clause of Cypher.

Here is the kind if graph I see..

               --> (Address1)-->(MyCity)-->(MyState)-->(MyCountry)

(root) -->(Shipper) ||

               --> (Address2)-->(MyCity)-->(MyState)-->(MyCountry)

Also whats the best way to create multiple nodes at once in one transaction, I think in the above code, each nodes are created in a separate Transactions.


回答1:


CREATE UNIQUE in Cypher 1.9 is not encouraged.Please, if you can, use Neo4j 2.0.0.M05 ++ and create a unique constraint on Cities, States and Countries, see http://docs.neo4j.org/chunked/snapshot/query-constraints.html#constraints-create-uniqueness-constraint and then MERGE the city node, the create the address. Here a graphgist example:

http://gist.neo4j.org/?6549962



来源:https://stackoverflow.com/questions/18740104/neo4jclient-create-unique-results-in-multiple-duplicate-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!