יום חמישי, 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 .

אין תגובות:

הוסף רשומת תגובה