为什么Neo4j查询没有用第一个查询返回wright数据?

时间:2014-11-04 14:31:36

标签: neo4j cypher

我有一个奇怪的问题我无法理解为什么Neo4j查询会返回不同的结果。

WITH shipper
MATCH (shipper)<-[:SHIPPED_BY]-(shipment:ActiveShipment)-[:SHIP_TO_CITY]->(toCity)
      <-[:HAS_CITY]-(toState:State), 
      **(veh:Vehicle)<-[:HAS_VEHICLE_TYPE]-(shipment)-[:SHIP_FROM_CITY]->(frmCity:City)
      <-[:HAS_CITY]-(frmState:State)**
RETURN shipment, toCity, frmCity, frmState, veh

返回

enter image description here

但只是对查询稍作修改会得到正确的结果:

WITH shipper
MATCH (shipper)<-[:SHIPPED_BY]-(shipment:ActiveShipment)-[:SHIP_TO_CITY]->(toCity)
      <-[:HAS_CITY]-(toState:State), 
     (veh:Vehicle)<-[:HAS_VEHICLE_TYPE]-(shipment)
**MATCH (shipment)-[:SHIP_FROM_CITY]->(frmCity:City)<-[:HAS_CITY]-(frmState:State)**
RETURN shipment, toCity, frmCity, frmState, veh

看起来像这样

enter image description here

只有返回的货物之间的差异才是它们具有相同的toCity和fromCity。有人可以帮我理解我的第一个问题是什么问题吗?

更新

易于重现,

Create (st:State{Name:'ABC'})-[:HAS_CITY]->(c:City{Name:'XYZ'})<-[:SHIPPED_FROM]-(sh:Shipment{InternalId:1})-[:HAS_VEHICLE_TYPE]->(vh:Vehicle{Name:'Vheicle 1'}), (sh)-[:SHIPPED_TO]->(c)

现在,如果我添加此查询,它将失败

MATCH (shipment:Shipment)-[:SHIPPED_TO]->(toCity) <-[:HAS_CITY]-(toState:State), (veh:Vehicle)<-[:HAS_VEHICLE_TYPE]-(shipment)-[:SHIPPED_FROM]->(frmCity:City)<-[:HAS_CITY]-(frmState:State) RETURN shipment, toCity, frmCity, veh

我认为问题是同一个州出现两次,如果我删除它接缝工作。

此致 基兰

1 个答案:

答案 0 :(得分:0)

与同一州有相同关系的同一城市,

由于这只是附加信息,您也可以将其拉出到单独的匹配条款中。

喜欢这里:

MATCH (shipment:Shipment)-[:SHIPPED_TO]->(toCity)<-[:HAS_CITY]-(toState:State),(veh:Vehicle)<-[:HAS_VEHICLE_TYPE]-(shipment)-[:SHIPPED_FROM]->(frmCity:City)
MATCH (frmCity)<-[:HAS_CITY]-(frmState:State)
RETURN shipment, toCity, frmCity, veh