Scott Campbell
Scott Campbell

Reputation: 81

Neo4j - cypher syntax to find relationships between nodes from two separate collections

I have developed the following query to find all nodes that meet a certain query criteria. Specifically, all the disease codes that satisfy certain parameters. I have used UNION to accomplish this in the code:

//Find all diagnosis codes
MATCH p = (a:ObjectConcept{sctid:233604007}) <-[:ISA*]- (b:ObjectConcept),
      q = (c:ObjectConcept{sctid:58800005})<-[:ISA*]-(d:ObjectConcept) 
WHERE NOT (b)-->()--(c) AND NOT (b)-->()-->(d) 
RETURN distinct b 
UNION  
MATCH p = (a:ObjectConcept{sctid:233604007}) <-[:ISA*]- (b:ObjectConcept), 
      t = (e:ObjectConcept{sctid:65119002})<-[:ISA*]-(f:ObjectConcept) 
WHERE NOT (b)-->()-->(e) AND NOT (b)-->()-->(f) 
RETURN distinct b

I would like to care the result from this query. That is the set (distinct b) and find all patients with any of these diseases.

Pseudo-code: Match s = (nodes in distinct b) <-[:HAS_DX]- (z:Patient) 
RETURN  distinct z

However, I do not know the Cypher syntax to carry the set of distinct nodes b into this subsequent query statement. I am using Neo4j v 2.1.7.

Thanks

Upvotes: 1

Views: 290

Answers (2)

Scott Campbell
Scott Campbell

Reputation: 81

The following query was successful. It required matching all aspects of the desired output into each subquery such that the column headers were all the same. Less than 1 sec added to query time.

MATCH p = (a:ObjectConcept{sctid:233604007}) <-[:ISA*]- (b:ObjectConcept),
      q = (c:ObjectConcept{sctid:58800005})<-[:ISA*]-(d:ObjectConcept) 
WHERE NOT (b)-->()--(c) AND NOT (b)-->()-->(d) 
with distinct b
MATCH (t:Patient)-[:HAS_DX]-> (b)
RETURN distinct t.patient_id,b.FSN 
UNION  
MATCH p = (a:ObjectConcept{sctid:233604007}) <-[:ISA*]- (b:ObjectConcept), 
      t = (e:ObjectConcept{sctid:65119002})<-[:ISA*]-(f:ObjectConcept) 
WHERE NOT (b)-->()-->(e) AND NOT (b)-->()-->(f) 
with distinct b
MATCH (t:Patient)-[:HAS_DX]-> (b)
RETURN distinct t.patient_id,b.FSN;

Upvotes: 0

cybersam
cybersam

Reputation: 66989

Does this query work for you?

MATCH p = (a:ObjectConcept{sctid:233604007})<-[:ISA*]-(b:ObjectConcept)<-[:HAS_DX]-(z:Patient),
      q = (c:ObjectConcept)<-[:ISA*]-(d:ObjectConcept) 
WHERE (c.sctid = 58800005 OR c.sctid = 65119002) AND NOT (b)-->()-->(c) AND NOT (b)-->()-->(d) 
RETURN DISTINCT z;

Upvotes: 1

Related Questions