Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to cypher query through neo4j-shell

Tags:

neo4j

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!

like image 354
DLS Avatar asked Oct 20 '22 00:10

DLS


1 Answers

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.

like image 132
FrobberOfBits Avatar answered Oct 27 '22 21:10

FrobberOfBits