‏הצגת רשומות עם תוויות cypher. הצג את כל הרשומות
‏הצגת רשומות עם תוויות cypher. הצג את כל הרשומות

יום חמישי, 27 ביוני 2013

Outer join in cyper

The following code :

  1: def handle_row(row):
  2:     print ("cypher query result:")
  3:     print ( len (row))
  4:     for nextNode in row:
  5:         print (nextNode)
  6:         print (type(nextNode))
  7: 
  8: 
  9: theCyperCode = "START n=node(1) ,other=node(2, 3)  RETURN  n,other"
 10: 
 11: cypher.execute(graph_db, theCyperCode, row_handler=handle_row)

returns
cypher query result:
2
(1 {"name":"Bruce Willis"})
<class 'py2neo.neo4j.Node'>
(2 {"name":"John McClane"})
<class 'py2neo.neo4j.Node'>
cypher query result:
2
(1 {"name":"Bruce Willis"})
<class 'py2neo.neo4j.Node'>
(3 {"name":"Alan Rickman"})
<class 'py2neo.neo4j.Node'>
Start

יום רביעי, 12 ביוני 2013

Cypher named path

Cypher allows returning paths along with discrete nodes and connections .
The following code demonstart returning a path :

  1: from py2neo import neo4j
  2: from py2neo import node, rel
  3: from py2neo import *
  4: 
  5: graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")
  6: 
  7: def CreateTheDB ():
  8:     die_hard = graph_db.create(
  9:         node(name="Bruce Willis"),
 10:         node(name="John McClane"),
 11:         node(name="Alan Rickman"),
 12:         node(name="Hans Gruber"),
 13:         node(name="Nakatomi Plaza"),
 14:         rel(0, "PLAYS", 1),
 15:         rel(2, "PLAYS", 3),
 16:         rel(1, "VISITS", 4),
 17:         rel(3, "STEALS_FROM", 4),
 18:         rel(1, "KILLS", 3),
 19:     )
 20: 
 21: CreateTheDB()
 22: 
 23: def handle_row(row):
 24:     print ("cypher query result:")
 25:     node = row[0]
 26:     print (node)
 27:     print (type(node)   )
 28: 
 29: theCyperCode = "START n=node(2) MATCH path =a--n RETURN path"
 30: 
 31: cypher.execute(graph_db, theCyperCode, row_handler=handle_row)

And the result:
cypher query result:
(1)-[:"PLAYS"]->(2)
<class 'py2neo.neo4j.Path'>
cypher query result:
(5)-[:"VISITS"]->(2)
<class 'py2neo.neo4j.Path'>
cypher query result:
(4)-[:"KILLS"]->(2)
<class 'py2neo.neo4j.Path'>

Note the type of the return value :'py2neo.neo4j.Path'


We can extend an complicate the query :
theCyperCode = "START n=node(2) MATCH path =n-->friend-[?]->friend_of_friend RETURN path"
And get and Result:
cypher query result:
(2)-[:"KILLS"]->(4)-[:"STEALS_FROM"]->(5)
<class 'py2neo.neo4j.Path'>

יום חמישי, 6 ביוני 2013

cypher “with” command

In order to perform aggregation method in Cypher query  the just in time nature of the language where the query process append only when the query results are fetched should be break.
For an example if we wants to find out only nodes that has 0..n connections we should sum all the connections of all nodes and only when finished iterate all the nodes  filter the nodes with connection > 0 .
Chpher uses the with statement in order to notify the query engine to execute the query parts before it call the other part .

The following sample works with the bruce willis db from prev samples.
The execution of the querey:

  1: theCyperCode = "START n=node(*) MATCH n-[]->ConnectedNode WITH n, count(ConnectedNode) as ConnectedNodeCount WHERE ConnectedNodeCount > 0 RETURN n, ConnectedNodeCount"
  2: 
  3: cypher.execute(graph_db, theCyperCode, row_handler=handle_row)
  4: 

The callback method


  1: def handle_row(row):
  2:     print ("cypher query result:")
  3:     node = row[0]
  4:     print (node)
  5:     print (row[1])

The result :


cypher query result:
(467 {"name":"John McClane"})
2
cypher query result:
(3 {"name":"Alan Rickman"})
1
cypher query result:
(933 {"name":"John McClane"})
2
cypher query result:
(466 {"name":"Bruce Willis"})
1


Nakatomi Plaza has no forword connections and there for it is missing from the results set

In the above sample START n=node(*) MATCH n-(x)-ConnectedNode is executing  and retrieving all the nodes in the graph with there connection, only then the n, count(ConnectedNode) as ConnectedNodeCounr WHERE friendsCount > 0 RETURN n, friendsCount part is executed in just in time manner .

יום חמישי, 30 במאי 2013

cypher variable length relationship

Note the demo db DataBase creation code can be found in prev posts.
Mark with an asterisk (*)
The following example gets all connected nodes in all levels:

  1: theCyperCode = "START Brusc=node(1) MATCH Brusc-[*]->(ConnectedNode) RETURN  ConnectedNode"
  2:          
  3: cypher.execute(graph_db, theCyperCode, row_handler=handle_row)


The results :
cypher query result:
(2 {"name":"John McClane"})
cypher query result:
(5 {"name":"Nakatomi Plaza"})
cypher query result:
(4 {"name":"Hans Gruber"})
cypher query result:
(5 {"name":"Nakatomi Plaza"})
Note that node 5 was visited twice and its apear twice in the result set

We can limit the depth by specifing a range in the form:[*MinDepth..MaxDepth]
for en example


  1: theCyperCode = "START Brusc=node(1) MATCH Brusc-[*2..3]->(ConnectedNode) RETURN  ConnectedNode"
  2: cypher.execute(graph_db, theCyperCode, row_handler=handle_row)

The result:
cypher query result:
(5 {"name":"Nakatomi Plaza"})
cypher query result:
(4 {"name":"Hans Gruber"})
cypher query result:
(5 {"name":"Nakatomi Plaza"})
Only the 2nd and the 3rd level were returned

יום שישי, 24 במאי 2013

Cypher Optional relationships

The cypher optional relationships is equivalent to the SQL outer join .
It is declared by using the [?] sign and it is used in the case where  if there is no relationship or node found we wants the query to return a null node instead of not returning a result at all .

The sample DB used in this post :
from py2neo import neo4j
from py2neo import node, rel
from py2neo import  cypher

graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")

die_hard = graph_db.create(
    node(name="Bruce Willis"),
    node(name="John McClane"),
    node(name="Alan Rickman"),
    node(name="Hans Gruber"),
    node(name="Nakatomi Plaza"),
    rel(0, "PLAYS", 1),
    rel(2, "PLAYS", 3),
    rel(1, "VISITS", 4),
    rel(3, "STEALS_FROM", 4),
    rel(1, "KILLS", 3),
)

Running the following example :

theCyperCode = "START Bruce=node(1) MATCH Bruce-->ConnectedNode-[?]->NodeConectedToConnectedNode RETURN ConnectedNode, NodeConectedToConnectedNode"

cypher.execute(graph_db, theCyperCode, row_handler=handle_row)
produce the following results :
cypher query result:
(2 {"name":"John McClane"})
(5 {"name":"Nakatomi Plaza"})
cypher query result:
(2 {"name":"John McClane"})
(4 {"name":"Hans Gruber"})

node 2 has 2  connections
Try a case where there is no results :
theCyperCode = "START Hans=node(4) MATCH Hans-->ConnectedNode-[?]->NodeConectedToConnectedNode RETURN ConnectedNode, NodeConectedToConnectedNode"

cypher.execute(graph_db, theCyperCode, row_handler=handle_row)
produce the following results :
cypher query result:
(5 {"name":"Nakatomi Plaza"})
None


Node 5 has no connection going out from it. therefore the optional relation chip return null as the connected node.
In case where the optional relationship is not used :
theCyperCode = "START Hans=node(4) MATCH Hans-->ConnectedNode-->NodeConectedToConnectedNode RETURN ConnectedNode, NodeConectedToConnectedNode"

cypher.execute(graph_db, theCyperCode, row_handler=handle_row)
No results will return at all.

יום שבת, 18 במאי 2013

cypher cont

The neo4j graph creation program for this post :

from py2neo import neo4j
from py2neo import node, rel
from py2neo import  cypher

graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")

die_hard = graph_db.create(
        node(name="Bruce Willis"),
        node(name="John McClane"),
        node(name="Alan Rickman"),
        node(name="Hans Gruber"),
        node(name="Nakatomi Plaza"),
        rel(0, "PLAYS", 1),
        rel(2, "PLAYS", 3),
        rel(1, "VISITS", 4),
        rel(3, "STEALS_FROM", 4),
        rel(1, "KILLS", 3),
    )

#the callback function for the Cypher query 
def handle_row(row):
    node = row[0]
    print (node)
    print (type (node))

Simple Cypher queries :
Find the related nodes of the related nodes of a node:

theCypherCode = "START n=node(1) MATCH (n)--(x)--(t) RETURN t"
cypher.execute(graph_db, theCypherCode, row_handler=handle_row)

The results:

(5 {"name":"Nakatomi Plaza"})
<class 'py2neo.neo4j.Node'>
(4 {"name":"Hans Gruber"})
<class 'py2neo.neo4j.Node'>

Note the return type  is  'py2neo.neo4j.Node

Find the related relationship of the related nodes of a node:

theCypherCode = "START n=node(1) MATCH (n)--(x)-[r]->() RETURN r”

cypher.execute(graph_db, theCypherCode, row_handler=handle_row)

The results:

(2)-[:VISITS]->(5)
<class 'py2neo.neo4j.Relationship'>
(2)-[:KILLS]->(4)
<class 'py2neo.neo4j.Relationship'>

Note the return type  is  'py2neo.neo4j.Relationship'

Get the relationsship object of a specific relationship

theCypherCode = "START n=node(1) MATCH (n)--(u)-[r:KILLS]->() RETURN r”