How to query for properties with dashes in Neo4j using Cypher - neo4j

I am trying to query for properties in Neo4j using the Cypher Query API. The query I am attempting is as follows:
String query = "start n=node(*) where (n.property-id = 'someid') return ID(n)"
I get an error when executing as follows:
Exception in thread "main" Unknown identifier id.
So, this means that Neo4j is treating the dash in property-id as a keyword. How does one go about formulating queries with dashes in a node/relationship property?
Thank you.

Escape the property with backticks:
String query = "start n=node(*) where (n.`property-id` = 'someid') return ID(n)"

Related

How to get nodes with unique properties in neo4j?

In Dr.who dataset that is available in neo4j, I want to get all the nodes having properties character. The cypher query I am using is :-
START n=node(*)
WHERE
HAS(n.property)
RETURN n
But this query, even returns me some nodes which has character and other property keys too (as shown in http://imgur.com/ujizTZj) but I want to get nodes having only character property key only.
If you use Neo4j 2.2+, forget the "START" clause and use the "MATCH" instead.
Also, for your use case I don't think before 2.2 it is possible, but in 2.2+ you can do :
MATCH (n)
WHERE HAS(n.character)
AND size(keys(n)) = 1
RETURN n

Identifying parameters of Cypher query

Is there an API for identifying the named parameters of a given Cypher query? When taking the following query as example:
MATCH (n) WHERE n.firstName = { name } AND n.LastName = { lastName } RETURN n
Then this API should return "name" and "lastName".
Does Neo4j provide such an API or would I have to manually parse the query string to identify any parameters it contains?
There is no API for obtaining the names of the properties used in a Cypher query. There is generally no need for such a utility, since the code making the query should already know that information.
Maybe you can begin your query with parameters in a transaction, get the result/errors and rollback it ?
http://docs.neo4j.org/chunked/stable/rest-api-transactional.html

Py2neo cypher query return instance method

Using the py2neo tutorial (http://book.py2neo.org/en/latest/cypher/):
from py2neo import neo4j, cypher
graph_db = neo4j.GraphDatabaseService()
query = "START a=node(1) RETURN a"
data, metadata = cypher.execute(graph_db, query)
a = data[0][0] # first row, first column
Trying to replicate this, I get:
>data[0][0]
Node('http://localhost:7474/db/data/node/1')
How do I get this to return the actual data, instead of the abstract information?
Your Cypher query returns a node (RETURN a) and so that's what's being passed back: a Node object. If it's the node's properties that you need, you can either then inspect the properties on that node with the get_properties method or return specific properties from the Cypher query instead (RETURN a.name).

Cypher UNION query

I have the simple following graph:
http://console.neo4j.org/?id=v0cvwn
And I wonder why the following query:
START n=node(2)
match n-[:KNOWS]-node
return node.name as name
UNION
START n=node(2)
match n-[:ACTS_IN]-node
return node.name as name
throws an exception:
java.lang.AssertionError: assertion failed: Can't profile the same pipe twice
at scala.Predef$.assert(Predef.scala:179)
at org.neo4j.cypher.internal.profiler.Profiler.decorate(Profiler.scala:47)
at org.neo4j.cypher.internal.pipes.Pipe$class.createResults(Pipe.scala:35)
at org.neo4j.cypher.internal.pipes.NullPipe$.createResults(Pipe.scala:47)
at org.neo4j.cypher.internal.pipes.PipeWithSource.createResults(Pipe.scala:61)
at org.neo4j.cypher.internal.pipes.PipeWithSource.createResults(Pipe.scala:61)
at org.neo4j.cypher.internal.pipes.PipeWithSource.createResults(Pipe.scala:61)
at org.neo4j.cypher.internal.pipes.UnionIterator.loadNextIterator$1(UnionIterator.scala:60)
at org.neo4j.cypher.internal.pipes.UnionIterator.step$1(UnionIterator.scala:68)
at org.neo4j.cypher.internal.pipes.UnionIterator.stepIfNeccessary(UnionIterato
Couple of questions regarding UNION:
Is it possible to perform another final match clause on all the data which was accumulate in the result set as a result from the UNION in previous sub-queries?
Is it possible to perform an order by on that data?
Thanks.
According to these github comments, UNION is only support in version 2.0 or higher.
https://github.com/neo4j/neo4j/issues/125

Wildcard search with cypher query and spring neo4j

When I execute the below query in neo4j console, i get the correct result set.
start n=node:search('username:*') return n.username;
I am using spring data neo4j in my java web app.
In the repository code, i defined query as:
#Query("START n=node:search({0}) RETURN n.name as name, n.username as username
Parameter passed
{0} = 'username:*'
There is no exception but the result set size is 0.
Can you please help me resolve the issue?
Unfortunately, the whole lucene query cannot be a parameter in Cypher, as far as I know. You'll need to resort to string concatenation/interpolation, probably. Similar to the issue I posted about relationship types as parameters: https://github.com/neo4j/neo4j/issues/340

Resources