0

I know hot to get the sub-graph by using Cypher query. But since I use py2neo.ogm model. I just want to know how to get sub-graph by using ogm. for example:

class Company(GraphObject):
    __primarykey__ = "firm_name"

    firm_name = Property()

    shareHolder = RelatedFrom("Company", "hold_by")

I already created the relationship between companies. I want to get the sub-graph of a company. I checked the document of py2neo, seems there is no example... Anyone can help? Best regards

digx1
  • 1,100
  • 5
  • 9
Howardyan
  • 667
  • 1
  • 6
  • 15

1 Answers1

3

The source code (partly copied py2neo v3 ogm doc) produces the following movie titles list (not including the minus sign), when run with community edition of Neo4J with the movies sample (:play movies)

  • Something's Gotta Give
  • Johnny Mnemonic
  • The Replacements
  • The Matrix Reloaded
  • The Matrix Revolutions
  • The Matrix
  • The Devil's Advocate
  • A Few Good Men
  • Apollo 13
  • Frost/Nixon
  • A Few Good Men
  • Stand By Me
  • A Few Good Men
  • Top Gun
  • Jerry Maguire

    import py2neo
    import py2neo.ogm
    
    from py2neo import Graph, Node, Relationship
    from py2neo.ogm import GraphObject, Property, RelatedFrom, RelatedTo, RelatedObjects
    
    class Movie(GraphObject):
        __primarykey__ = "title"
    
        title = Property()
        tag_line = Property("tagline")
        released = Property()
    
        actors = RelatedFrom("Person", "ACTED_IN")
        directors = RelatedFrom("Person", "DIRECTED")
        producers = RelatedFrom("Person", "PRODUCED")
    
    
    class Person(GraphObject):
        __primarykey__ = "name"
    
        name = Property()
        born = Property()
    
        acted_in = RelatedTo(Movie)
        directed = RelatedTo(Movie)
        produced = RelatedTo(Movie)
    
    def authenticateAndConnect():
        # Authenticate the user using py2neo.authentication
        py2neo.authenticate('localhost:7474', '<username>', '<password>')
    
        # Connect to Graph and get the instance of graph
        return Graph('http://localhost:7474/default.graphdb/data/')     
    
    def foo():
        graph = authenticateAndConnect()
        for person in list(Person.select(graph).where("_.name =~ 'K.*'")):
            for movie in person.acted_in:
                print(movie.title)
    
    if __name__ == '__main__':
        foo()   
    
Portable
  • 323
  • 1
  • 9
  • Hi Protable, thank you for your reply. But I found neo4jrestclient lib is much easier to use. I have give up py2neo... – Howardyan Mar 13 '17 at 01:26
  • by using neo4jrestclient. the thing is easy: from neo4jrestclient.client import Q import neo4jrestclient.client as client query_string = 'start a=node(%s) match (a)-[r:hold_by*]-(b) return a, r, b' % id results = gdb.query(query_string, returns=(client.Node, [client.Relationship], client.Node), data_contents=True) print results.graph – Howardyan Mar 13 '17 at 09:59
  • I believe it is as easy using py2neo. But you asked for an OGM example, which uses classes. – Portable Mar 16 '17 at 04:09
  • 1
    I found this answer useful. I hope that @Howardyan will accept this answer it answers the original question. – ChrisGuest May 10 '17 at 08:00