py2neo v3 cypher keyError - py2neo

I'm a newbe with py2neo.
Trying to create a cypher statement to which includes some sort of selection like
query = 'MATCH (p:Person {name:"Alice"}) - [r] - b) RETURN p,r,b'
res = Graph.run(query)
I'm getting a KeyError: 'name'
Running the same query directly in neo4j shell or web client runs successfully.
Update
I manage to run the code using the WHERE statement:
query = ('MATCH (p:Person) - [r] - b)
WHERE p.name="Alice"
RETURN p,r,b')
res = Graph.run(query)
Is this the only option to run py2neo queries or is there a way to use the key values of the node properties?
Thank you in advance

The second node b should be enclosed by parenthesis (). I've also created a graph object from the Graph class:
from py2neo import Graph
neo4j_config = dict(
user="neo4j",
password="neo4j_pwd"
)
graph = Graph(**neo4j_config)
query = 'MATCH (p:Person {name:"Alice"}) - [r] - (b) RETURN p,r,b'
res = graph.run(query)

Related

Neo4J Graph Algorithms Cypher Projection should return only numbers?

Hello I make a Graph Algorithm Neo4J request in Cypher of the following kind, which first finds the nodes and then the relations between them:
CALL algo.pageRank.stream('MATCH (u:User{uid:"0ee14110-426a-11e8-9d67-e79789c69fd7"}),
(ctx:Context{name:"news180417"}), (u)<-[:BY]-(c:Concept)-[:AT]->(ctx)
RETURN DISTINCT id(c) as id',
'CALL apoc.index.relationships("TO","user:0ee14110-426a-11e8-9d67-e79789c69fd7")
YIELD rel, start, end WITH DISTINCT rel, start, end MATCH (ctx:Context)
WHERE rel.context = ctx.uid AND (ctx.name="news180417" )
RETURN DISTINCT id(start) AS source, id(end) AS target',
{graph:'cypher', iterations:5});
Which works fine. However, when I try to return c.uid instead of its Neo4J id() the Graph Algorithms don't accept it.
Does it mean I can only operate using Neo4J ids in Graph Algorithms?
When you use Cypher projection with the Graph Algorithms procedures, you pass 2 Cypher statements (and a config map).
The first Cypher statement must return an id variable whose value is the native ID of a node.
The second Cypher statement must return source and target variables whose values are also node IDs.
So, yes, your Cypher statements must always return neo4j native IDs.

How to get all connected nodes in neo4j graph in py2neo

Is there a way with py2neo to get all the connected nodes in a graph. All nodes that are connected by relationship?
You can execute a Cypher query that will return you those :
res = graph.cypher.execute("MATCH (n) WHERE size((n)--()) > 0 RETURN n");
for r in res:
print r
This Cypher query should be good enough:
MATCH (n)--() RETURN DISTINCT n

using set clause in spring cypher query

please provide me some example to use set clause in cypher query using java.
i just want to update property of relationship object but it always fail.
if i am running query like
ExecutionResult executionResult = engine.execute("start n=node:Person(name=\"suresh\"),n2=node:Email(subject=\"Hi\") match (n)-[r]-(n2) return r ");
System.out.println(executionResult);
i am getting proper response like below
+-----------------------------------------------------------------------------------------------------------------------------+
| r |
+-----------------------------------------------------------------------------------------------------------------------------+
| :DynamicRelationshipType[Have][69] {accessed->true,__type__->"org.test.spring.neo.domain.EmailRelationShip",relation->"To"} |
+-----------------------------------------------------------------------------------------------------------------------------+
1 rows, 260 ms
but when i am trying to run query like
start n=node:Person(name=\"suresh\"),n2=node:Email(subject=\"Hi\") match (n)-[r]-(n2) CREATE SET r.accessed=true return r
it always fail
stack Trace
expected return clause
"start n=node:Person(name="suresh"),n2=node:Email(subject="Hi") match (n)-[r]-(n2) CREATE SET r.accessed=true return r "
^
at org.neo4j.cypher.internal.parser.v1_6.CypherParserImpl.parse(CypherParserImpl.scala:65)
at org.neo4j.cypher.CypherParser.parse(CypherParser.scala:42)
at org.neo4j.cypher.ExecutionEngine$$anonfun$prepare$1.apply(ExecutionEngine.scala:60)
at org.neo4j.cypher.ExecutionEngine$$anonfun$prepare$1.apply(ExecutionEngine.scala:60)
at org.neo4j.cypher.internal.LRUCache.getOrElseUpdate(LRUCache.scala:31)
at org.neo4j.cypher.ExecutionEngine.prepare(ExecutionEngine.scala:60)
at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:54)
at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:51)
at org.neo4j.cypher.javacompat.ExecutionEngine.execute(ExecutionEngine.java:63)
at org.test.spring.neo.controller.MediatorController.main(MediatorController.java:34)
i tried to execute like below also but this one is also failing
ExecutionResult executionResult = engine.execute("start n=node:Person(name=\"suresh\"),n2=node:Email(subject=\"Hi\") match (n)-[r]-(n2) SET r.accessed=false return r ");
i am referring http://docs.neo4j.org/refcard/1.9/ to create a read and write query
i am using sdn version 1.8.
Please Help
As the error message says: "expected return clause"
Neo4j 1.8 is more restricted than e.g. 2.0
e.g. something like this works:
start n=node:Person(name="abc"), n2=node:Email(subject="Hi")
match (n)-[r]-(n2)
set r.accessed=true
return count(*)
You should also always use parameters:
start n=node:Person(name={person}), n2=node:Email(subject={subject})
match (n)-[r]-(n2)
set r.accessed=true
return count(*)

Creating relationship between two previously disconnected nodes

Given query:
start n=node(*)
match p:Person, b:Book
where p.name = 'John' AND b.title = 'KJV'
create p-[r:OWNS]->b
return r
error: Expected return clause is thrown, with a caret pointing at the S]
What is the syntactical error?
May be you are using older version of Neo4j (< 2.0) which doesn't support Labels. I was able to successfully create the relationships using the below Cypher. Tried it on console.neo4j.org
CREATE (n:Person { name : 'John' })
CREATE (n:Book { title : 'KJV' })
start n=node(*)
match p:Person, b:Book
where p.name = 'John' AND b.title = 'KJV'
create p-[r:OWNS]->b
return r
EDIT
As I've guessed, you are using 1.9.2 which doesn't support Labels. You however are using the Neo4j 2.0 syntax with Labels (p:Person, b:Book)

neo4j cypher return top node or start node

I have a hierarchical org structure like this:
OrgNode(3)-[HAS_PARENT]->OrgNode(2)-[HAS_PARENT]->OrgNode(1)
I want a cypher query that gives me the top org given any of the node ids:
topOrg(3) = OrgNode(1)
topOrg(2) = OrgNode(1)
topOrg(1) = OrgNode(1)
I'm able to write the query to return the top org when the starting node has at least one parent. But I cannot figure out how to return starting node when there is no parent link in the same query:
start n=node(3)
match (n)-[:PARENT*]->(m)-[r?:PARENT]->()
WHERE r is null
return m
You might use the UNION operator to combine your result with the result of another query that handles a starting node without a parent,
start n=node(3)
match (n)-[:PARENT*]->(m)-[r?:PARENT]->()
WHERE r is null
return m as result
UNION
Start n=node(3)
Match n
Where not(n-[:PARENT]-())
Return n as result

Resources