Cypher supports querying with parameters. This allows developers to not to have to do string building to create a query, and it also makes caching of execution plans much easier for Cypher.
Parameters can be used for literals and expressions in the WHERE
clause, for the index key and index value in the START
clause, index queries, and finally for node/relationship ids. Parameters can not be used as for property names, relationship types
and labels, since since these patterns are part of the query structure that is compiled into a query plan.
Accepted names for parameter are letters and number, and any combination of these.
Here follows a few examples of how you can use parameters from Java.
Parameter for node id.
Map<String, Object> params = new HashMap<String, Object>(); params.put( "id", 0 ); ExecutionResult result = engine.execute( "start n=node({id}) return n.name", params );
Parameter for node object.
Map<String, Object> params = new HashMap<String, Object>(); params.put( "node", andreasNode ); ExecutionResult result = engine.execute( "start n=node({node}) return n.name", params );
Parameter for multiple node ids.
Map<String, Object> params = new HashMap<String, Object>(); params.put( "id", Arrays.asList( 0, 1, 2 ) ); ExecutionResult result = engine.execute( "start n=node({id}) return n.name", params );
Parameter for string literal.
Map<String, Object> params = new HashMap<String, Object>(); params.put( "name", "Johan" ); ExecutionResult result = engine.execute( "start n=node(0,1,2) where n.name = {name} return n", params );
Parameter for index key and value.
Map<String, Object> params = new HashMap<String, Object>(); params.put( "key", "name" ); params.put( "value", "Michaela" ); ExecutionResult result = engine.execute( "start n=node:people({key} = {value}) return n", params );
Caution | |
---|---|
Using a parameter for index keys has been deprecated. |
Parameter for index query.
Map<String, Object> params = new HashMap<String, Object>(); params.put( "query", "name:Andreas" ); ExecutionResult result = engine.execute( "start n=node:people({query}) return n", params );
Numeric parameters for SKIP
and LIMIT
.
Map<String, Object> params = new HashMap<String, Object>(); params.put( "s", 1 ); params.put( "l", 1 ); ExecutionResult result = engine.execute( "start n=node(0,1,2) return n.name skip {s} limit {l}", params );
Parameter for regular expression.
Map<String, Object> params = new HashMap<String, Object>(); params.put( "regex", ".*h.*" ); ExecutionResult result = engine.execute( "start n=node(0,1,2) where n.name =~ {regex} return n.name", params );
Parameter setting properties on node.
Map<String, Object> n1 = new HashMap<String, Object>(); n1.put( "name", "Andres" ); n1.put( "position", "Developer" ); Map<String, Object> params = new HashMap<String, Object>(); params.put( "props", n1 ); engine.execute( "START n=node(0) SET n = {props}", params );
Copyright © 2013 Neo Technology