Database Reference
In-Depth Information
Retrieving Relationships
Once a relationship has been created between one or more nodes, then the relationship can be retrieved based on a
node (Listing 9-7).
Listing 9-7. Retrieving Relationships
from py2neo import neo4j, ogm, node, rel
# set connection information (defaults to: http://localhost:7474/db/data/)
graph_db = neo4j.GraphDatabaseService()
greg = graph_db.node(1)
brad = graph_db.node(10)
# find relationship via outgoing relationship
rels = greg.match_outgoing(rel_type="FOLLOWS", end_node= brad)
# find relationship ignoring the direction of the relationship
rels = greg.match(rel_type="FOLLOWS", other_node= brad)
Deleting a Relationship
Once a relationship's graph id has been set and saved into the database, it becomes eligible to be removed when
necessary. To remove a relationship, set it as a relationship object instance and then call the delete method for the
relationship (Listing 9-8).
Listing 9-8. Deleting a Relationship
from py2neo import neo4j, ogm, node, rel
# set connection information (defaults to: http://localhost:7474/db/data/)
graph_db = neo4j.GraphDatabaseService()
greg = graph_db.node(1)
brad = graph_db.node(10)
# find single relationship
rel = graph_db.match_one(start_node=greg, rel_type="FOLLOWS", end_node= brad)
# you could also set bidirectional to True if reversed relatioships should be matched
rel = graph_db.match_one(start_node=greg, rel_type="FOLLOWS", end_node= brad, bidirectional=True)
# delete relationship
rel.delete()
Using Labels
Labels function as specific meta-descriptions that can be applied to nodes. Labels were introduced in Neo4j 2.0 in
order to help in querying and can also function as a way to quickly create a subgraph.
 
Search WWH ::




Custom Search