我想知道是否有人可以帮助我理解Create Unique在Neo4J中的实际效果。我正在尝试将多个地址注册到托运人节点,我希望城市,州和国家/地区在地址中相同,以便它们都指向同一个节点。但问题是Create Unique导致数据库中存在多个相同节点的实例。
这是我必须向托运人注册地址的功能,我使用相同的托运人节点多次调用该地址,但指向同一城市的地址不同。
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;
}
现在假设我用不同的地址调用上面的方法如下:
// 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" }
);
//第二次电话
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" }
);
我最终得到了同一个节点的多个实例。只是想知道是否可以使用Cypher的Create Unique子句来实现这一点。
这是我看到的那种图表..
--> (Address1)-->(MyCity)-->(MyState)-->(MyCountry)
(root) - &gt;(发件人)||
--> (Address2)-->(MyCity)-->(MyState)-->(MyCountry)
同样,在一个事务中一次创建多个节点的最佳方法,我认为在上面的代码中,每个节点都是在一个单独的事务中创建的。
答案 0 :(得分:1)
CREATE UNIQUE
。如果可以的话,请使用Neo4j 2.0.0.M05 ++并在城市,州和国家/地区创建一个独特的约束,请参阅http://docs.neo4j.org/chunked/snapshot/query-constraints.html#constraints-create-uniqueness-constraint然后MERGE
城市节点,创建地址。这是一个graphgist示例: