Find nodes that have all common intermediaries

I am creating a system in which we map orders

to staff

. Conceptually, order

this is a request for a person who does some work, and staff

is a person who can do that work. order

may have one or more requirements

(ie, Restrictions on who can do the job), or staff

may have another requirements

(ie, Qualified to do the job).

I am trying to create a cypher query that will give me everything staff

that has everything requirement

listed in the data order

. On the other hand, I am trying to find all the staff

nodes that are associated with every requirement

node that is associated with a given order

node. My question is , how do I create a cypher query to simulate this business logic?

As an example, consider the following sample data:

Look at orderId: 1 node. it relates requires

to two nodes designated RN and ER IV. For words, order # 1 requires all applicants to have an RN qualification and an ER IV qualification. It just so happens that employee Evan (staffId: 1) has both of these qualifications, so he should be able to apply for this job. Employee Tim has ONE of these requirements, but not both, so he shouldn't be able to apply for this job. Also, orderId: 2 only has one requirement that Evan and Tim have, so they must both can apply for this job.

So basically, if I started with order # 1, I would only want to return Evan. If I started with order # 2, I would like to return Evan and Tim *.

The next request is halfway there. This will give me all the unique paths from a given order to one employee at a time. However, it does not verify that EVERY request path is satisfied (which means it will currently only work for orders that have only one requirement):

start o=node(2) 
match o-[:requires]->req<-[:hasRequirement]-s 
return o, req, s;

      

So what are my options? Is there some way I can check for an unknown number of matching relationships? Or will I need to model my data differently?

* Edit: I was wrong when setting up my details. Tim had to be associated with RN to qualify for order # 2.

+3


source to share


2 answers


I think this cypher statement solves your problem:



start o=node(2) 
match o-[:requires]->req<-[:hasRequirement]-p 
with o, p, count(req) as c 
where length(o-[:requires]-()) = c 
return p, c

      

+6


source


I figured it out

start o=node(2) 
match o-[orderReqRel:requires]->r 
with count(orderReqRel) as orderReqs, o 
match p-[personReqRel:hasRequirement]->r<-[:requires]-o 
with count(personReqRel) as personReqs,p,orderReqs 
where personReqs=orderReqs
return p;

      



Seems to work for Order 1 where I only go back to Evan. Order 2 doesn't seem to have a requirement that Tim has (your explanation points to this, but for some reason, not seeing it in the console you shared)

+3


source







All Articles