What is the best way to get values with same property key?
EDIT: Sorry for changing the question my requirement was to get an employee from either of the departments
I need to fetch all the the employees who work for IT or Sales departments and are being managed by manager with id 123.
I have used
g.V().has('managerId',123).out('manages').as('employee')
.out('worksFor').has('departmentName','IT','Sales')
.select('employee')
where out('worksAt')
gives department.
Can we do this in a has()
step or should we use union()
step like
g.V().has('managerId',123).out('manages').as('employee').out('worksFor')
.union(__.has('departmentName','IT'),__.has('departmentName','Sales')
.select('employee')
You are probably only missing the within
predicate which is also explained in the context of the has
step in the TinkerPop documentation:
g.V().has('managerId',123).out('manages').as('employee').out('worksFor').
has('departmentName',within('IT','Sales')).select('employee')
edit: After reading stephen's answer I noticed that I read over the and in your question:
employees who work for IT and Sales
That makes my answer of course invalid. I still leave it here just in case that you actually meant or as indicated by your later use of the union
step.
Here's a sample graph:
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV("managerId",123).as("manager").
......1> addV("employee","A").as("A").
......2> addV("employee","B").as("B").
......3> addV("department", "IT").as("it").
......4> addV("department", "Sales").as("sales").
......5> addE("manages").from("manager").to("A").
......6> addE("manages").from("manager").to("B").
......7> addE("worksFor").from("A").to("it").
......8> addE("worksFor").from("B").to("it").
......9> addE("worksFor").from("A").to("sales").iterate()
In this case, I make it so that employee A is in both "Sales" and "IT", but employee B is only in "IT". Since you said you wanted employees who work in both departments employee A is who should be returned from the query and B should be filtered out.
Note that the use of within
yields an incorrect answer in that case:
gremlin> g.V().has('managerId',123).
......1> out('manages').
......2> where(out('worksFor').
......3> has('department',within('IT','Sales'))).
......4> valueMap()
==>[employee:[A]]
==>[employee:[B]]
Here is the approach if you want both departments:
gremlin> g.V().has('managerId',123).
......1> out('manages').
......2> where(out('worksFor').
......3> has('department','Sales')).
......4> where(out('worksFor').
......5> has('department','IT')).
......6> valueMap()
==>[employee:[A]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With