Skip to content
Robin Edwards edited this page Feb 24, 2014 · 21 revisions

(Used to be called traverse())

class Address(StructuredNode):
   name = StringProperty()
   street = StringProperty()
   town = StringProperty()
   post_code = StringProperty()

class Car(StructuredNode):
   model = StringProperty(unique_index=True)
   

class User(StructuredNode):
   name = StringProperty(unique_index=True)
   age = IntegerProperty()
   friend = Relationship(Address, 'FRIEND', ZeroOrMore)
   address = Relationship(Address, 'ADDRESS', ZeroOrOne)
   car =  Relationship(Address, 'CAR', ZeroOrOne)

Matching by label from a class

User.nodes.filter(age__lt=16)
User.nodes.exclude(age__lt=16)

# chain able:
User.nodes.filter(age__lt=16).exclude(age__lt=4)

Relationship manager methods

jim.friends.all()

# only where rel property since is less than year
jim.friends.match(since__lt=year())

# replaces search() as that is now deprecated
jim.friends.filter(age__gt=5)
jim.friends.exclude(age__lt=18) # negated

Match and filter

match rels with properties then filter on node properties

jim.friends.match(since__gt=yesterday()).filter(age__gt=5)

2 level match

jim.friends.match(since__lt=year()).car.match(**kwargs).filter(model="")

Filtering nodes by their relationships

# users older than 18 who own a car made over 2 years ago but don't have an address
Users.nodes.filter(age__gt=18).has(
    car=Car.filter(manufactured__lt=two_years()),
    address=False)

# friends that own a car with a trailer
me.friends.all().car.all().has(trailer=True)

Check that they are related to a particular node

Users.nodes.filter(age__gt=18).has(car=False, van=Van.filter(model='Ford')

Issues:

  • multi type relationships - solution: disable chaining on multi type relationships.
  • how to return multiple types of node

Under the hood

NodeSet(source)
Traversal(source=, target=)
# Filter
aset = NodeSet(source=User.filter()) = NodeSet(source=User).filter()
aset.exclude(**kwargs)

# Traversal
user.friends # returns Traversal(source=node, target=node_class)
# .all() returns NodeSet(source=traversal)
# .filter() returns NodeSet(source=traversal) (with a filter applied)
user.friends.match(since__gt=today) # sets the parameters of the traversal returns NodeSet