cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

New to cypher/neo4j Modularize Scripts

Hello, I'm sorry if this is a very common question, but I am pretty new to this, and I'm trying to use neo4j and cypher to build a simple generate model of agents in a network. I have a good idea of the type of graph modelling I want to use, how the nodes all relate to each other. However, I would like the graph model to be updated over time through scripts. Such an example of a script might be: "Person moves to office." and that would change their "Located at" relation from whatever it's pointed to now, to the same as that person's "works at" relation.

Now of course, this is pretty trivial with cypher query language, and I have been able to do this command with cypher-cli and with parameter notation. However, I would really like to be able to define this type of function as a procedure, or as a script file with parameters than I could call from other script files. I can only find neo4j documentation about writing custom code to make my own procedures, but I don't really need that level of detail yet, I just need to define some custom parameterized cypher queries that I can organize into simple functions. Is there any idea about how to go about this?

5 REPLIES 5

If you're using Desktop this is pretty simple, but not sure it buys you much unless the params are used more than once in the queries/updates. You can create and use params either separately or within a favorite. I typically set up params (separately) for a set of repetitive queries which works well. I save both the set param and the queries as favorites.

//set name
:param name => 'John Smith';

//find name
MATCH (p:Party {name:$name}) return p

you can combined them into one script, not useable for reads, since it doesn't return the result, but for updates it works

// have a node 'robert' update name and address
:param name => 'robert';
MATCH (p:Party {name:$name})
SET p.name = "Joe Smith"
CREATE (l:Location:Test {city:"foo"})
MERGE (p) -[:has]-> (l)

hope that helps,

This helps somewhat but what I really want to do is define the parameters as inputs into the script, so I can have a generic “move person to office” script that takes in a persons node id so that it’s a reusesble script to be use with any node of type “person”

help me understand your environment/stack. what scripting language are you using? how are you running the scripts, browser or cypher shell?. are you running them manually or you want something automated?

Since I am building a proof of concept, I would like to run the scripts manually. So far, I have been using the neo4j desktop tool for manual queries, but I am moving towards building a small cli tool with python to call top level functions against the database

using neo4j desktop, I believe you have to set the params manually using :param command. it can be a single value or a map.

for py2neo you can do something like this
graph_db.data("OPTIONAL MATCH (n:Node) WHERE id(n) = $pName RETURN n", pName='1234')

or pass in the locals() dictionary; to use local variables

graph_db.data("OPTIONAL MATCH (n:Node) WHERE n.property = $idValue RETURN n", **locals())