2
votes

I have a setup like the attached image. Orange nodes denotes the cases and the Blue nodes denotes the Performers of various activities within that case.

Case specifics nodes

I would like to query each case in turn. Within each case, I need to add relationship

[:RELATED {value: 1}] 

from nodei to nodek , for all k that lies in between(nodes whose ID fall between i and j) those pair of Performer nodes (nodei,nodej) such that :

Name(nodei) == Name( nodej )

and

| ID(nodei) - ID(nodej) | >= 2



[Example and Expected Output]:

  1. In Case1, since Performer nodes with ID:1 and ID:4 satisfy the criteria, so relationships are added between:

           Node(ID:1) to Node(ID:2)
    
           Node(ID:1) to Node(ID:3)
    

  1. In Case2, Performer nodes with ID:2 and ID:4 satisfy the criteria, so relationships are added between:

           Node(ID:2) to Node(ID:3)
    

  1. In Case3, there are two sets of nodes satisfying the criteria,

    a. For Node(ID:1) and Node(ID:4) add relationships from

           Node(ID:1) to Node(ID:2)
    
           Node(ID:1) to Node(ID:3)
    

    b. For Node(ID:3) and Node(ID:5), add relationship from

           Node(ID:3) to Node(ID:4)
    

Hint needed in formulating CYPHER queries for the above case.

Thanks in advance.

1
In case 1, why should there be a rel between Node(ID:1) to Node(ID:2)? Not the same name and (2 - 1) == 1.wassgren

1 Answers

3
votes

As mentionned in the question comment, your calculation should be reversed :

node j - node i >= 2

One point more, for case 3, Following your explanations there should be a third relationship between node 2 and 3

Here is a query I made, you can test it in this neo4j console : http://console.neo4j.org/r/gpfesu

MATCH (n:Case) 
MATCH path=(pe)<--(n)-->(pe2)
WHERE pe.name = pe2.name
AND pe2.id - pe.id >= 2
WITH path,n, (pe2.id - pe.id) as length
WITH range(head(nodes(path)).id+1, last(nodes(path)).id-1) as ids,
     n, 
     head(nodes(path)).id as starter,
     length
UNWIND ids as x
MATCH (perf:Performer {id:starter})<--(n)-->(perf2:Performer {id:x})
MERGE (perf)-[:RELATES_TO {value:1, length:length}]->(perf2)