cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Basic intersection of sets using Cypher

Hi Group !

I'm a very basic and newbie user of neo4J and Cypher and I'm trying to build a query.

This is about the visibility of a Component in an application given a set of eligibility conditions (items) for the Employee that is connected to the application.

the data model:

What I'm trying ?

  • Get a first set of names of eligibility items for an Employee
  • Get a second set of name of eligibility items for a Block
  • Intersect both sets (using APOC function), if something is common it means that i can display/render the Block for that Employee

my (very bad) attempts

match (ee:Employee {name:"Carlos"})-[:EE_Eligible_For]->(o:EligibilityItem)  with ee, collect(o.name) as l1
match (:Component {name:"HEALTH_ACCORDION"})-[:Is_Eligible_For]->(e:EligibilityItem)  
with collect(e.name) as l2, collect (o.name) as l2 return apoc.coll.intersection (l1, l2)

// fetch the eligibility items for the component
match (c:component {key:"HEALTH_ACCORDION"})->[:Is_Eligible_For]->(e:EligibilityItem)
with c,collect(e) as ComponentEList
// fetch the eligibility items for the logged user
match (ee:Employee {name:"Carlos")->[:EE_Eligible_For]->(e2:EligibilityItem)
with ee, collect(e2) as EmployeeEList
match (sal:SalaryClass) where ee.salary > sal.min and ee.salary < sal.max
with sal.name + EmployeeEList 
// intersect the eligibility list
return apoc.coll.intersect(ComponentEList,EmployeeEList)

Greatly appreciate if you can point me the right direction to achieve my goal.

Running Neo4J 4.0.4

best regards

Rui

1 ACCEPTED SOLUTION

I've tried and tried and tried again

but i did it !

in short, I was misusing the "WITH" instruction

My solution:

// get eligibility items for employee 
match (ee:Employee {name:"Carlos"})-[:EE_Eligible_For]->(e1:EligibilityItem)
with  collect(e1.name) as l1
// get eligibility items for component
match (:Component {name:"HEALTH_ACCORDION"})-[:Is_Eligible_For]->(e2:EligibilityItem)  
with collect(e2.name) as l2,l1 
// check if there is common  eligibility items 
return apoc.coll.intersection (l1, l2) CommonEligibilityItem, size(apoc.coll.intersection (l1, l2)) as NCommonEligibilityItem

View solution in original post

1 REPLY 1

I've tried and tried and tried again

but i did it !

in short, I was misusing the "WITH" instruction

My solution:

// get eligibility items for employee 
match (ee:Employee {name:"Carlos"})-[:EE_Eligible_For]->(e1:EligibilityItem)
with  collect(e1.name) as l1
// get eligibility items for component
match (:Component {name:"HEALTH_ACCORDION"})-[:Is_Eligible_For]->(e2:EligibilityItem)  
with collect(e2.name) as l2,l1 
// check if there is common  eligibility items 
return apoc.coll.intersection (l1, l2) CommonEligibilityItem, size(apoc.coll.intersection (l1, l2)) as NCommonEligibilityItem