I was wondering whether there is a way to pass parameters to a Cypher query through the neo4j-shell. Although the details of the query may not matter, I want to execute the following query through neo4j-shell:
MATCH (src:Node{id:1}),(dst:Node{id:2}),
p = shortestPath((src)-[*..15]-(dst))
RETURN p;
What I do is put this inside a file query.cql and then execute it by running neo4j-shell -file query.cql. However, every time I run it I may want to change the source and destination IDs. So ideally, I'd like my query to be something like this:
MATCH (src:Node{id:srcid}),(dst:Node{id:dstid}),
p = shortestPath((src)-[*..15]-(dst))
RETURN p;
and define srcid and dstid in the command line. Is this possible?
Thanks!
Yes, use the export command.
neo4j-sh (?)$ export myParam="Foo"
neo4j-sh (?)$ CREATE (u:Node {label: {myParam}});
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 1
Labels added: 1
10 ms
neo4j-sh (?)$ MATCH (u:Node {label: {myParam}}) return u.label;
+---------+
| u.label |
+---------+
| "Foo" |
+---------+
1 row
19 ms
EDIT if you wanted to do this from the shell before entering interactive query, you can use a neat little bash trick, like this:
$ { echo "export foo='bar'" ; cat; } | neo4j-shell -path my.db
This has a big drawback though; the line-editing functions of neo4j-shell won't be available; you'll just be feeding it interactive data via cat
so it's not truly interactive.
Normally, this would be solved by using an initfile containing some cypher commands that would get loaded every time during each interactive session, but as far as I know neo4j-shell doesn't support that, so this bash trick is the best I can offer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With