Rakesh Singha
Rakesh Singha

Reputation: 21

py2neo return number of nodes and relationships created

I need to create a python function such that it adds nodes and relationship to a graph and returns the number of created nodes and relationships.

I have added the nodes and relationship using graph.cypher.execute().

arr_len = len(dic_st[story_id]['PER'])                  
for j in dic_st[story_id]['PER']:
    graph.cypher.execute("MERGE (n:PER {name:{name}})",name = j[0].upper())     #creating the nodes of PER in the story
    print j[0]    
for j in range(0,arr_len):
    for k in range(j+1,arr_len):
        graph.cypher.execute("MATCH (p1:PER {name:{name1}}), (p2:PER {name:{name2}}) WHERE upper(p1.name)<>upper(p2.name) CREATE UNIQUE (p1)-[r:in_same_doc {st_id:{st_id}}]-(p2)", name1=dic_st[story_id]['PER'][j][0].upper(),name2=dic_st[story_id]['PER'][k][0].upper(),st_id=story_id)     #linking the edges for PER nodes

What I need is to return the number of new nodes and relationships created.

What I get to know from the neo4j documentation is that there is something called "ON CREATE" and "ON MATCH" for MERGE in cypher, but thats not being very useful. The browser interface for neo4j do actually shows the number of nodes and relationship updated. This is what I need to return, but I am not getting quite the way for it to access it.

Any help please.

Upvotes: 1

Views: 1371

Answers (3)

c z
c z

Reputation: 8977

After executing your query using x = session.run(...) you can use x.summary.counters to get the statistics noted in Martin Perusse's answer. See the documentation here.

In older versions the counters are available as a "private" field under x._summary.counters.

Upvotes: 0

Martin Preusse
Martin Preusse

Reputation: 9369

When you post your query against the Cypher endpoint of the neo4j REST API without using py2neo, you can include the argument "includeStats": true in your post request to get the node/relationship statistics. See this question for an example.

As far as I can tell, py2neo currently does not support additional parameters for the Cypher query (even though it is using the same API endpoints under the hood).

In Python, you could do something like this (using the requests and json packages):

import requests
import json

payload = {
    "statements": [{
            "statement": "CREATE (t:Test) RETURN t",
            "includeStats": True
        }]
    }

r = requests.post('http://your_server_host:7474/db/data/transaction/commit',
                   data=json.dumps(payload))

print(r.text)

The response will include statistics about the number of nodes created etc.

{  
   "stats":{  
      "contains_updates":true,
      "nodes_created":1,
      "nodes_deleted":0,
      "properties_set":1,
      "relationships_created":0,
      "relationship_deleted":0,
      "labels_added":1,
      "labels_removed":0,
      "indexes_added":0,
      "indexes_removed":0,
      "constraints_added":0,
      "constraints_removed":0
   }
}

Upvotes: 1

Sumit
Sumit

Reputation: 1420

In case you need the exact counts of properties either created or updated then you have use "Match" with "Create" or "Match" with "Set" and then count the size of results. Merge may not return which ones are updated and which ones are created.

Upvotes: 0

Related Questions