Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-29-2018 01:03 PM
Hello,
I'm trying to create relations between already created nodes. I'm able to create normal relations (fixed names) without any problems but I'm not able to create relation names dynamically.
I explain:
I'm trying to replicate the model explained in this interesting website:
I create Airports Nodes (success):
session.run("CREATE (a: Airport {airportCode: {airportCode}, countryName: {countryName}, cityName: {cityName}})", parameters("airportCode", airportCode, "countryName", countryName, "cityName", cityName));
I create AirportDay Nodes (success):
session.run("CREATE (ad: AirportDay {Code: {Code}, AirportCode: {AirportCode}, DateCode: {DateCode}})", parameters("Code", code, "AirportCode", airportCode, "DateCode", dateCode));
I create Leg (or Flight) Nodes (success)
session.run("CREATE (l: Leg {LegCode: {LegCode}, Origin: {Origin}, Destination: {Destination}, LegFlightNum: {LegFlightNum}, FlightNum: {FlightNum}, DepartureDate: {DepartureDate}, DepartureTime: {DepartureTime}, DepartureDateTime: {DepartureDateTime}, ArrivalDate: {ArrivalDate}, ArrivalTime: {ArrivalTime}, ArrivalDateTime: {ArrivalDateTime} })", parameters("LegCode", legCode, "Origin", origin, "Destination", destination, "LegFlightNum", legFlightNum, "FlightNum", flightNum, "DepartureDate", departureDate, "DepartureTime", departureTime, "DepartureDateTime", departureDateTime, "ArrivalDate", arrivalDate, "ArrivalTime", arrivalTime, "ArrivalDateTime", arrivalDateTime));
I link correctly Airport & AirportDay (success):
session.run("MATCH (a:Airport), (ad:AirportDay) WHERE a.airportCode = ad.AirportCode MERGE (a)-[:HAS_DAY]->(ad)");
I'm trying to link AirportDay & Leg but this way, relation names are not dynamic (I would like to have the name of the destination airport +_FLIGHT instead of HAS_FLIGHT):
session.run("MATCH (ad:AirportDay), (l:Leg) WHERE ad.DateCode = l.DepartureDate MERGE (ad)-[:HAS_FLIGHT]->(l)");
My problem is that the code from this website gives an example with an embedded Neo4j database, here is the code of this website:
departureAirportDayNode.createRelationshipTo(leg, RelationshipType.withName(arrivalCity + "_FLIGHT"));
But in my case, I'm using the Neo4J Java Driver and I found no example on how to deal with my problem.
I understand that I need to build the name of the relation dynamically but how to do this... ?
Any help will be appreciated,
(I'm a beginner with Neo4J and not an expert in Java...)
Don't hesitate if any information is missing.
Martin.
11-30-2018 03:11 AM
Cypher on its own cannot use dynamic relationship types. However the apoc library solves this by call apoc.create.relationship(from, type, props, to)
11-30-2018 05:56 AM
if you use the java API you can also create relationships with:
Relationship rel = node1.createRelationshipTo(node2, RelationshipType.withName(relTypeName)
11-30-2018 06:02 AM
@stefan: Many thanks for these useful information. I will see if I can use APOC library.
@michael: Not sure what is the difference between using Java Driver code and Java API. I will read about this before commenting. Big thanks for your help.
12-12-2018 03:13 PM
Driver is client side in your own application.
Java API is if you write a procedure or server extension.
All the sessions of the conference are now available online