7
votes

I created a application the fills a Neo4J database with emails records. I created 2 types of nodes, person & mail and I created 4 types of relationships, sent, cc, to & reply_of.

Now I want to find relationships between 2 nodes that aren't directly connected. For example between P1 & P3 (see picture).

How can I achieve this through cypher? Is it doable in neo4j?

Relationship between persons & mails

Updated question:

First let me clear something about the picture...

The Reply_OF relationship is a relationship between mail nodes. It creates a relationship between the original mail and any reply of forward mail. The Sent, To, Cc & Bcc relationships create relations between a person node and a mail node. There is no direct relation between person nodes.

Submitting this question and reading the answers made me realise I wanted to know something else...what I really would like to know is how can I show all person nodes that have seen a reply or forward mail from the original mail that where not on the To, Cc or Bcc lists in the original mail.

2

2 Answers

3
votes

Answer to the updated question:

I assume your email nodes carry a label of Email and have a property mailId.

MATCH (mailToTrack:Email {mailId: 'mymailid'})-[:Reply_Of*1..100]->()-[:TO|:CC|:BCC]->(person)
RETURN distinct person
4
votes

In Neo4j terms you want to find paths between P1 and P3. A relationship connects just two neighbors.

I assume P nodes carry a Person label, and have a name property, in which case you can use:

MATCH p=(p1:Person {name:'P1'})-[:SENT|:TO|:CC|:BCC*1..20]->(p3:Person {name:'P3'})
RETURN p

In a lot of cases you're interested in the shortest path between them:

MATCH p=shortestPath((p1:Person {name:'P1'})-[:SENT|:TO|:CC|:BCC*1..20]->(p3:Person {name:'P3'})
RETURN p