Over a million developers have joined DZone.

neo4j/cypher: 400 response – Paths can’t be created inside of foreach

· Performance Zone

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

In the neo4j 1.9 milestone releases if we wanted to create multiple relationships from a node we could use the following cypher syntax:

require 'neography'
 
neo = Neography::Rest.new
 
neo.execute_query("create (me {name: 'Mark'})")
 
query =  " START n=node:node_auto_index(name={name})"
query << " FOREACH (friend in {friends} : CREATE f=friend, n-[:FRIEND]->f)"
 
neo.execute_query(query, {"name" => "Mark", 
                          "friends" => [{ "name" => "Will"}, {"name" => "Paul"}]})

To check that the ‘FRIEND’ relationships have been created we’d write the following query:

START p = node:node_auto_index(name="Mark") 
MATCH p-[:FRIEND]-f 
RETURN f

==> +----------------------+
==> | f                    |
==> +----------------------+
==> | Node[2]{name:"Will"} |
==> | Node[3]{name:"Paul"} |
==> +----------------------+
==> 2 rows
==> 37 ms
In between the M04 and RC2 releases there was a change in the cypher parser which meant that this approach doesn’t work anymore:
/Users/markhneedham/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/neography-1.0.10/lib/neography/connection.rb:168:in `handle_4xx_500_response': 
Paths can't be created inside of foreach (Neography::SyntaxException)
	from /Users/markhneedham/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/neography-1.0.10/lib/neography/connection.rb:143:in `return_result'
	from /Users/markhneedham/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/neography-1.0.10/lib/neography/connection.rb:126:in `evaluate_response'
	from /Users/markhneedham/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/neography-1.0.10/lib/neography/connection.rb:45:in `post'
	from /Users/markhneedham/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/neography-1.0.10/lib/neography/rest/cypher.rb:19:in `query'
	from /Users/markhneedham/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/neography-1.0.10/lib/neography/rest.rb:335:in `execute_query'
	from create.rb:12:in `<main>'

Instead we can just use CREATE to achieve the same thing:

require 'neography'
 
neo = Neography::Rest.new
 
neo.execute_query("create (me {name: 'Mark'})")
 
query =  " START n = node:node_auto_index(name={name})"
query << " CREATE friend = {friends} "
query << " CREATE n-[:FRIEND]->friend"
 
neo.execute_query(query, {"name" => "Mark", 
                          "friends" => [{ "name" => "Will"}, { "name" => "Paul" }]})

Learn tips and best practices for optimizing your capacity management strategy with the Market Guide for Capacity Management, brought to you in partnership with BMC.

Topics:

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}