Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-16-2021 02:06 AM
Hi, I'm trying to create relation to link event
MATCH (c:Car {carID:car_ID})-[:takePlace]->(e:Event)
with e ORDER BY e.start_time
with collect(e) as events
FOREACH(i in RANGE(0, size(events)-2) |
create (events[i])-[:NEXT]->(events[i+1]))
And I got the fullowing error
Thanks,
Oli
Solved! Go to Solution.
07-18-2021 11:17 PM
According to the documentation: within the FOREACH
parentheses, you can do any of the updating commands — SET
, REMOVE
, CREATE
, MERGE
, DELETE
, and FOREACH
. So in your case, I don't see how you can use it since you need to match both nodes.
The best solution is this one:
MATCH (c:Car {carID: car_ID})-[:takePlace]->(e:Event)
WITH e
ORDER BY e.start_time
WITH collect(e) AS events
UNWIND RANGE(0, size(events)-2) AS i
MATCH (a:Event {eventID: events[i].eventID})
MATCH (b:Event {eventID: events[i+1].eventID})
CREATE (a)-[:NEXT]->(b)
07-16-2021 02:25 AM
Hello @oli
You cannot use a list item to match a node, you must do like this:
MATCH (c:Car {carID: car_ID})-[:takePlace]->(e:Event)
WITH e
ORDER BY e.start_time
WITH collect(e) AS events
UNWIND RANGE(0, size(events)-2) AS i
MATCH (a:Event) WHERE a.your_property = events[i].your_property
MATCH (b:Event) WHERE b.your_property = events[i+1].your_property
CREATE (a)-[:NEXT]->(b)
Regards,
Cobra
07-16-2021 02:56 AM
Hello Cobra,
Thanks for your reply, my purpose is that I wish to link all my nodes inside the list without searching outside. If I'm not understanding wrong, neo4j doesn't support event[i] to represent a node even if it does a node?(I tried like "with event[i] as e1", and it works, but the with clause cannot be inside FOREACH clause)
07-16-2021 02:58 AM
Yes, that's why you must use a MATCH
clause, do you have a unique property for your Event
nodes?
07-18-2021 05:21 PM
Yes, I do. I have a generated eventID(combined with several properties), May I ask that what if I would to use FOREACH, how could I deal with the event[i]?
07-18-2021 11:17 PM
According to the documentation: within the FOREACH
parentheses, you can do any of the updating commands — SET
, REMOVE
, CREATE
, MERGE
, DELETE
, and FOREACH
. So in your case, I don't see how you can use it since you need to match both nodes.
The best solution is this one:
MATCH (c:Car {carID: car_ID})-[:takePlace]->(e:Event)
WITH e
ORDER BY e.start_time
WITH collect(e) AS events
UNWIND RANGE(0, size(events)-2) AS i
MATCH (a:Event {eventID: events[i].eventID})
MATCH (b:Event {eventID: events[i+1].eventID})
CREATE (a)-[:NEXT]->(b)
07-19-2021 12:01 AM
Thanks Cobra,
I'm just wandering that what if I don't match the event but just iterate the node and link it?
MATCH (c:Car {carID: car_ID})-[:takePlace]->(e:Event)
WITH e
ORDER BY e.start_time
WITH collect(e) AS events
UNWIND RANGE(0, size(events)-2) AS i
WITH events[i] AS a, events[i+1] AS b
CREATE (a)-[:NEXT]->(b)
If I wrote the code in this way, it will be no problem (use WITH...AS... instead events[i] directly)
I don't know if this will be more efficient since there is no extra matching cost?
Hope to discuess with you
07-19-2021 12:05 AM
Yes, your solution with the WITH
clause should be more efficient, I did not have to wake up fully . You can compare both queries with the PROFILE keyword.
All the sessions of the conference are now available online