I wrote a quick script that runs a traversal query through Neo4j's traversal framework REST API:
from py2neo import neo4j
import requests
import json
dbURI = 'http://localhost:7474/db/data/'
def graphTraversal (nodeId):
returnType = 'path'
payload = {
'order': 'breadth_first',
'uniqueness': 'node_global',
'relationships': [{
'direction': 'all',
'type': 'associatedMusicalArtist'
}],
'max_depth': 2
}
payload = json.dumps(payload)
headers = {
'Accept': 'application/json; charset=UTF-8',
'Content-Type': 'application/json'
}
uri = dbURI + '/node/' + nodeId + '/traverse/' + returnType
print '\n> Traversal of ' + uri + ' ...'
res = requests.post(uri, data=payload, headers=headers)
print res.text
return res.json();
def getNodeByUri (db, key, value):
return db.get_indexed_node('nodeIdx', key, value)
def getNodeId (node):
return str(node._id)
def main ():
db = neo4j.GraphDatabaseService(dbURI)
node = getNodeByUri(db, '__URI__', 'http://dbpedia.org/resource/Jimi_Hendrix')
nodeId = getNodeId(node)
rels = graphTraversal(nodeId)
if __name__ == '__main__':
main()
The code runs fine, but I have a question:
I know that the Java driver permits this but for Python, the documentation isn't clear about how to do it.
Thanks in advance!
EDIT: Here's a link to the documentation of Neo4j's traversal framework REST API I'm following.
There is no direct way to do this, the traversal endpoints are all single-node.
You can do it by using the Batch API, sending lots of traversal requests in a single HTTP request. Have a look here:
http://docs.neo4j.org/chunked/stable/rest-api-batch-ops.html
However
For the Traversal you are running, you could easily express it in Cypher, and then you'd be able to give it multiple start points, like so:
START n=node(1,2,3,4,5) MATCH (n)-[:associatedMusicalArtist*0..2]-(other) RETURN other
Which you can then send to the the transactional endpoint:
http://docs.neo4j.org/chunked/stable/rest-api-transactional.html