CosmicMind / Graph

Graph is a semantic database that is used to create data-driven applications.
http://cosmicmind.com
MIT License
873 stars 72 forks source link

Search and Order result #133

Closed marrocmau closed 7 years ago

marrocmau commented 7 years ago

I have this situation and i want search only users (in relationship with Trip) but order by "isBoss" is true

let graph = Graph()
graph.clear()

let search = Search<Relationship>(graph: graph).for(types: "Subscriber")

let users1 = Entity(type: "User")
users1["name"]="John"
users1["date"]="08/05/2017"
users1["image"]="img1"
users1["isBoss"]=true

let users2 = Entity(type: "User")
users2["name"]="Mary"
users2["date"]="08/05/2017"
users2["image"]="img2"
 users2["isBoss"]=false

 let users3 = Entity(type: "User")
users3["name"]="George"
users3["date"]="08/05/2017"
users3["image"]="img2"
 users3["isBoss"]=false

let trip1 = Entity(type: "Trip")
trip1["location"]="USA"
trip1["vehicle"]="CAR"

users1.is(relationship: "Subscriber").of(object: trip1)
users2.is(relationship: "Subscriber").of(object: trip1)

let trip2 = Entity(type: "Trip")
trip2["location"]="CANADA"
trip2["vehicle"]="BOAT"

 users2["isBoss"]=false
 users3["isBoss"]=true        
users2.is(relationship: "Subscriber").of(object: trip2)
users3.is(relationship: "Subscriber").of(object: trip2)

graph.sync()

let result = search.sync().filter { (subscriber) -> Bool in
    return "USA" == subscriber.object?["location"] as? String
}

let users = result.subject(types: "User")

print(users)
daniel-jonathan commented 7 years ago

In this case you might want to use Graph slightly differently. You can use groups in this case, where Boss is a grouping. This way, you can search for all members that are Bosses, and then check if they have a trip.

let graph = Graph()
graph.clear()

let search = Search<Relationship>(graph: graph).for(types: "Subscriber")

let u1 = Entity(type: "User")
u1["name"]="John"
u1["date"]="08/05/2017"
u1["image"]="img1"
u1.add(to: "Boss")

let u2 = Entity(type: "User")
u2["name"]="Mary"
u2["date"]="08/05/2017"
u2["image"]="img2"

let u3 = Entity(type: "User")
u3["name"]="George"
u3["date"]="08/05/2017"
u3["image"]="img2"
u3.add(to: "Boss")

let trip1 = Entity(type: "Trip")
trip1["location"]="USA"
trip1["vehicle"]="CAR"

u1.is(relationship: "Subscriber").of(object: trip1)
u2.is(relationship: "Subscriber").of(object: trip1)

let trip2 = Entity(type: "Trip")
trip2["location"]="CANADA"
trip2["vehicle"]="BOAT"

u2.is(relationship: "Subscriber").of(object: trip2)
u3.is(relationship: "Subscriber").of(object: trip2)

graph.sync()

let result = search.sync().filter { (subscriber) -> Bool in
      return true == subscriber.subject?.member(of: "Boss") && "USA" == subscriber.object?["location"] as? String
}

print(result.subject(types: "User"))

You can also do this:

let graph = Graph()
graph.clear()

let search = Search<Entity>(graph: graph).member(of: "Boss")

let u1 = Entity(type: "User")
u1["name"]="John"
u1["date"]="08/05/2017"
u1["image"]="img1"
u1.add(to: "Boss")

let u2 = Entity(type: "User")
u2["name"]="Mary"
u2["date"]="08/05/2017"
u2["image"]="img2"

let u3 = Entity(type: "User")
u3["name"]="George"
u3["date"]="08/05/2017"
u3["image"]="img2"
u3.add(to: "Boss")

let trip1 = Entity(type: "Trip")
trip1["location"]="USA"
trip1["vehicle"]="CAR"

u1.is(relationship: "Subscriber").of(object: trip1)
u2.is(relationship: "Subscriber").of(object: trip1)

let trip2 = Entity(type: "Trip")
trip2["location"]="CANADA"
trip2["vehicle"]="BOAT"

u2.is(relationship: "Subscriber").of(object: trip2)
u3.is(relationship: "Subscriber").of(object: trip2)

graph.sync()

let users = search.sync().filter { (user) -> Bool in
      let subscribers = user.relationship(types: "Subscriber").filter { (subscriber) -> Bool in
           return "USA" == subscriber.object?["location"] as? String
      }

     return 0 < subscribers.count
}

print(users)

If you need any further help, please reopen the issue :) Thank you!