Reputation: 3832
I use neo4j-rest-binding API to develop, but I face a problem when using parameters of RestCypherQueryEngine.
QueryResult<Map<String,Object>> result = engine.query("MATCH (n:{label}) RETURN n", MapUtil.map("label", label));
label is the parameter I assign in the map structure, but it has an error:
org.neo4j.rest.graphdb.RestResultException: Invalid input '{': expected whitespace or an identifier (line 1, column 10)
"MATCH (n:{label}) RETURN n"
^ at
SyntaxException
org.neo4j.cypher.internal.compiler.v2_0.parser.CypherParser$$anonfun$parse$1.apply(CypherParser.scala:51)
org.neo4j.cypher.internal.compiler.v2_0.parser.CypherParser$$anonfun$parse$1.apply(CypherParser.scala:41)
...
I can use another method to solve this problem:
QueryResult<Map<String,Object>> result = engine.query("MATCH (n:" + label +") RETURN n", null);
But I think the above method is not appropriate when I want to pass multiple parameters.
Upvotes: 1
Views: 322
Reputation: 32680
:{
is a syntactical error. As the exception tells you, Cypher expects an identifier after a colon - namely, the name of a label - and an identifier (as in most languages) cannot contain a bracket.
It sounds like you're confused about the difference between labels and parameters:
The following would be valid: MATCH (n:employee{name:"foo"})
Here, employee
is a label. You can apply an arbitrary number of labels delimited by colons. {name:"foo"}
is a parameter block - note that it contains both the field you want to match and the value. So, this query will return all nodes labelled employee
with a name
value of "foo". MATCH (n:employee:custodian{name:"foo"})
will give you all employees who are custodians named "foo".
If you want all nodes with a name
value of "foo", use MATCH (n {name:"foo"})
(note the absence of a colon).
Edit (responding to your comment) There are two differences between your query and the one in the example you're referring to, start n=node({id}) return n
is, obviously, a START clause, which do very different things and have different syntactical rules from MATCH clauses: The id
in ({id)}
is simply a value to look up in an index. In a MATCH clause, what goes inside a { }
block are key-value pairs, as is explained above. Inside a parameter block (i.e. a set of braces), colons are used to separate keys from values. A colon outside the brackets in a MATCH clause are used to separate labels which are different different things entirely.
The second difference is that, if you look more closely at the START clause, there is a parenthesis separating the colon from the bracket. :{
is never okay, which is what your error message is telling you.
Upvotes: 1