Neo4J关系建模问题

时间:2019-01-29 08:02:18

标签: neo4j cypher

我正在尝试根据以下层次结构对城市间和城市间的关系进行建模:

domestic travel/
├── intra-city/
│   ├── bangalore
│   ├── pune
│   ├── hyderabad
│   ├── delhi
│   ├── mumbai
│   ├── kolkata
│   └── chennai
├── inter-city/
│   ├── bangalore -> pune
│   └── bangalore -> hyderabad
│   └── bangalore -> chennai
│   └── bangalore -> delhi
│   └── bangalore -> mumbai
│   └── bangalore -> kolkata
│   └── hyderabad -> bangalore
│   └── hyderabad -> pune
│   └── hyderabad -> chennai
│   └── hyderabad -> delhi
│   └── hyderabad -> mumbai
│   └── hyderabad -> kolkata

家庭旅行是我的根结点,可以结成子结点

  • 城市间(同一城市内旅行)
  • 城际旅行(两人之间旅行 不同的城市)

我还有一个城市根节点,上面附加了所有城市。


当前,我将其建模为具有2个关系(:FROM_CITY和:TO_CITY)

如果旅行类型为城市间,则我直接标记与城市的:TO_CITY关系,如果其城市间,我将第一个城市标记为关系:FROM_CITY。下面的屏幕快照对此进行了描述。

enter image description here


下面是我用来建立关系模型的密码查询。

1)旅行树:

MERGE (Travel: TravelType { name: 'Travel' })
ON CREATE SET
    Travel.id = randomUUID(),
    Travel.type = 'TravelType',
    Travel.created = timestamp()
ON MATCH SET
    Travel.lastUpdated = timestamp()

MERGE (InternationTravel:TravelType { name: 'InternationTravel' })
ON CREATE SET
    InternationTravel.id = randomUUID(),
    InternationTravel.type = 'TravelType',
    InternationTravel.subtype = 'Internation Travel',
    InternationTravel.created = timestamp()
ON MATCH SET
    InternationTravel.lastUpdated = timestamp()

MERGE (DomesticTravel:TravelType { name: 'DomesticTravel' })
ON CREATE SET
    DomesticTravel.id = randomUUID(),
    DomesticTravel.type = 'TravelType',
    DomesticTravel.subtype = 'Domestic Travel',
    DomesticTravel.created = timestamp()
ON MATCH SET
    DomesticTravel.lastUpdated = timestamp()

MERGE (IntraCity: TravelType { name: 'IntraCity' })
ON CREATE SET
    IntraCity.id = randomUUID(),
    IntraCity.type = 'Domestic Travel',
    IntraCity.created = timestamp()
ON MATCH SET
    IntraCity.lastUpdated = timestamp()

MERGE (InterCity: TravelType { name: 'InterCity' })
ON CREATE SET
    InterCity.id = randomUUID(),
    InterCity.type = 'Domestic Travel',
    InterCity.created = timestamp()
ON MATCH SET
    InterCity.lastUpdated = timestamp()

MERGE (Travel)-[:OF_TYPE_TRAVEL]->(DomesticTravel)
MERGE (Travel)-[:OF_TYPE_TRAVEL]->(InternationTravel)

MERGE (DomesticTravel)-[:OF_TYPE_DOMESTIC_TRAVEL]->(IntraCity)
MERGE (DomesticTravel)-[:OF_TYPE_DOMESTIC_TRAVEL]->(InterCity)
RETURN *

2)城市树:

MERGE (City: Location { name: 'City' })
ON CREATE SET
    City.id = randomUUID(),
    City.type = 'Location',
    City.created = timestamp()
ON MATCH SET
    City.lastUpdated = timestamp()

MERGE (Bangalore:Location { name: 'Bangalore' })
ON CREATE SET
    Bangalore.id = randomUUID(),
    Bangalore.type = 'Location',
    Bangalore.created = timestamp(),
    Bangalore.IATACode = 'BLR',
    Bangalore.CityType = 1
ON MATCH SET
    Bangalore.lastUpdated = timestamp()

MERGE (Chennai:Location { name: 'Chennai' })
ON CREATE SET
    Chennai.id = randomUUID(),
    Chennai.type = 'Location',
    Chennai.created = timestamp(),
    Chennai.IATACode = 'MAA',
    Chennai.CityType = 1
ON MATCH SET
    Chennai.lastUpdated = timestamp()

MERGE (Hyderabad:Location { name: 'Hyderabad' })
ON CREATE SET
    Hyderabad.id = randomUUID(),
    Hyderabad.type = 'Location',
    Hyderabad.created = timestamp(),
    Hyderabad.IATACode = 'HYD',
    Hyderabad.CityType = 1
ON MATCH SET
    Hyderabad.lastUpdated = timestamp()

MERGE (Pune:Location { name: 'Pune' })
ON CREATE SET
    Pune.id = randomUUID(),
    Pune.type = 'Location',
    Pune.created = timestamp(),
    Pune.IATACode = 'PNQ',
    Pune.CityType = 1
ON MATCH SET
    Pune.lastUpdated = timestamp()

MERGE (DelhiNCR:Location { name: 'DelhiNCR' })
ON CREATE SET
    DelhiNCR.id = randomUUID(),
    DelhiNCR.type = 'Location',
    DelhiNCR.created = timestamp(),
    DelhiNCR.IATACode = 'DEL',
    DelhiNCR.CityType = 2
ON MATCH SET
    DelhiNCR.lastUpdated = timestamp()

MERGE (Mumbai:Location { name: 'Mumbai' })
ON CREATE SET
    Mumbai.id = randomUUID(),
    Mumbai.type = 'Location',
    Mumbai.created = timestamp(),
    Mumbai.IATACode = 'BOM',
    Mumbai.CityType = 2
ON MATCH SET
    Mumbai.lastUpdated = timestamp()

MERGE (Kolkata:Location { name: 'Kolkata' })
ON CREATE SET
    Kolkata.id = randomUUID(),
    Kolkata.type = 'Location',
    Kolkata.created = timestamp(),
    Kolkata.IATACode = 'CCU',
    Kolkata.CityType = 2
ON MATCH SET
    Kolkata.lastUpdated = timestamp()

MERGE (`Other Cities`:Location { name: 'Other Cities' })
ON CREATE SET
    `Other Cities`.id = randomUUID(),
    `Other Cities`.type = 'Location',
    `Other Cities`.created = timestamp(),
    `Other Cities`.IATACode = [],
    `Other Cities`.CityType = 3
ON MATCH SET
    `Other Cities`.lastUpdated = timestamp()

MERGE (City)-[:OF_TYPE_CITY]->(Bangalore)
MERGE (City)-[:OF_TYPE_CITY]->(Hyderabad)
MERGE (City)-[:OF_TYPE_CITY]->(Pune)
MERGE (City)-[:OF_TYPE_CITY]->(Chennai)

MERGE (City)-[:OF_TYPE_CITY]->(DelhiNCR)
MERGE (City)-[:OF_TYPE_CITY]->(Mumbai)
MERGE (City)-[:OF_TYPE_CITY]->(Kolkata)

MERGE (City)-[:OF_TYPE_CITY]->(`Other Cities`)

MERGE (Bangalore)-[:TO_CITY]->(Hyderabad)
MERGE (Bangalore)-[:TO_CITY]->(Pune)
MERGE (Bangalore)-[:TO_CITY]->(Chennai)
MERGE (Bangalore)-[:TO_CITY]->(DelhiNCR)
MERGE (Bangalore)-[:TO_CITY]->(Mumbai)
MERGE (Bangalore)-[:TO_CITY]->(Kolkata)
MERGE (Bangalore)-[:TO_CITY]->(`Other Cities`)

MERGE (Hyderabad)-[:TO_CITY]->(Bangalore)
MERGE (Hyderabad)-[:TO_CITY]->(Pune)
MERGE (Hyderabad)-[:TO_CITY]->(Chennai)
MERGE (Hyderabad)-[:TO_CITY]->(DelhiNCR)
MERGE (Hyderabad)-[:TO_CITY]->(Mumbai)
MERGE (Hyderabad)-[:TO_CITY]->(Kolkata)
MERGE (Hyderabad)-[:TO_CITY]->(`Other Cities`)

MERGE (Pune)-[:TO_CITY]->(Bangalore)
MERGE (Pune)-[:TO_CITY]->(Hyderabad)
MERGE (Pune)-[:TO_CITY]->(Chennai)
MERGE (Pune)-[:TO_CITY]->(DelhiNCR)
MERGE (Pune)-[:TO_CITY]->(Mumbai)
MERGE (Pune)-[:TO_CITY]->(Kolkata)
MERGE (Pune)-[:TO_CITY]->(`Other Cities`)

MERGE (Chennai)-[:TO_CITY]->(Bangalore)
MERGE (Chennai)-[:TO_CITY]->(Hyderabad)
MERGE (Chennai)-[:TO_CITY]->(Pune)
MERGE (Chennai)-[:TO_CITY]->(DelhiNCR)
MERGE (Chennai)-[:TO_CITY]->(Mumbai)
MERGE (Chennai)-[:TO_CITY]->(Kolkata)
MERGE (Chennai)-[:TO_CITY]->(`Other Cities`)

MERGE (DelhiNCR)-[:TO_CITY]->(Bangalore)
MERGE (DelhiNCR)-[:TO_CITY]->(Hyderabad)
MERGE (DelhiNCR)-[:TO_CITY]->(Pune)
MERGE (DelhiNCR)-[:TO_CITY]->(Chennai)
MERGE (DelhiNCR)-[:TO_CITY]->(Mumbai)
MERGE (DelhiNCR)-[:TO_CITY]->(Kolkata)
MERGE (DelhiNCR)-[:TO_CITY]->(`Other Cities`)

MERGE (Mumbai)-[:TO_CITY]->(Bangalore)
MERGE (Mumbai)-[:TO_CITY]->(Hyderabad)
MERGE (Mumbai)-[:TO_CITY]->(Pune)
MERGE (Mumbai)-[:TO_CITY]->(Chennai)
MERGE (Mumbai)-[:TO_CITY]->(DelhiNCR)
MERGE (Mumbai)-[:TO_CITY]->(Kolkata)
MERGE (Mumbai)-[:TO_CITY]->(`Other Cities`)

MERGE (Kolkata)-[:TO_CITY]->(Bangalore)
MERGE (Kolkata)-[:TO_CITY]->(Hyderabad)
MERGE (Kolkata)-[:TO_CITY]->(Pune)
MERGE (Kolkata)-[:TO_CITY]->(Chennai)
MERGE (Kolkata)-[:TO_CITY]->(Mumbai)
MERGE (Kolkata)-[:TO_CITY]->(DelhiNCR)
MERGE (Kolkata)-[:TO_CITY]->(`Other Cities`)

MERGE (`Other Cities`)-[:TO_CITY]->(Bangalore)
MERGE (`Other Cities`)-[:TO_CITY]->(Hyderabad)
MERGE (`Other Cities`)-[:TO_CITY]->(Pune)
MERGE (`Other Cities`)-[:TO_CITY]->(Chennai)
MERGE (`Other Cities`)-[:TO_CITY]->(Mumbai)
MERGE (`Other Cities`)-[:TO_CITY]->(Kolkata)
MERGE (`Other Cities`)-[:TO_CITY]->(DelhiNCR)
RETURN *

3)将城市间旅行和城市间旅行链接到城市树:

MATCH (IntraCity: TravelType { name: 'IntraCity' })
MATCH (InterCity: TravelType { name: 'InterCity' })
MATCH (Bangalore: Location { name: 'Bangalore' })
MATCH (Hyderabad:Location { name: 'Hyderabad' })
MATCH (Chennai: Location { name: 'Chennai' })
MATCH (Pune: Location { name: 'Pune' })
MATCH (DelhiNCR: Location { name: 'DelhiNCR' })
MATCH (Mumbai: Location { name: 'Mumbai' })
MATCH (Kolkata: Location { name: 'Kolkata' })
MATCH (`Other Cities`: Location { name: 'Other Cities' })
CREATE (IntraCity)-[:TO_CITY]->(Bangalore)
CREATE (InterCity)-[:FROM_CITY]->(Bangalore)
CREATE (IntraCity)-[:TO_CITY]->(Hyderabad)
CREATE (InterCity)-[:FROM_CITY]->(Hyderabad)
CREATE (IntraCity)-[:TO_CITY]->(Chennai)
CREATE (InterCity)-[:FROM_CITY]->(Chennai)
CREATE (IntraCity)-[:TO_CITY]->(Pune)
CREATE (IntraCity)-[:TO_CITY]->(Pune)
CREATE (IntraCity)-[:TO_CITY]->(DelhiNCR)
CREATE (InterCity)-[:FROM_CITY]->(DelhiNCR)
CREATE (IntraCity)-[:TO_CITY]->(Mumbai)
CREATE (InterCity)-[:FROM_CITY]->(Mumbai)
CREATE (IntraCity)-[:TO_CITY]->(Kolkata)
CREATE (InterCity)-[:FROM_CITY]->(Kolkata)
CREATE (IntraCity)-[:TO_CITY]->(`Other Cities`)
CREATE (InterCity)-[:FROM_CITY]->(`Other Cities`)
RETURN *

  

我面临的问题是它很快变得无法管理和查询这些节点和关系。结果,我无法   正确地查询它。   有没有更好的方法来模拟城市之间的城际和城际旅行,您可以指出我犯的任何明显错误。

任何建议将不胜感激。

0 个答案:

没有答案