3

My problem is very close to this topic: Cypher query: Finding all paths between two nodes filtered by relationship properties but what I'm trying to do is to find path which has increasing value of relationship property along the path. So in that previos topics example solution paths (from A to D) would be:

A->D and A->B->D

I used solution from previous topic

START a=node(1), d=node(4) 
MATCH p=a-[r:ACTIVATES*..]->d 
WITH head(relationships(p)) as r1,p
WHERE all(r2 in relationships(p) 
      where r2.temperature > r1.temperature) 
return p;

and it works for this example. Problem is when there is path with more then 2 relationships, for example:

    activates:50      activates:70      activates:60
(A)-------------->(B)-------------->(C)-------------->(D)

this path unfortunately matches too.

Is there way how to write this query in cypher or I'll have to use gremlin instead?

Thanks for any suggestion.

Update: What I need is some construction like (in pseudo programing language):

WITH head(relationships(p)) as r1,p
FOREACH(r2 in tail(relationships(p)):
    r1.temperature < r2.temperature, r1 = r2)

but in cypher if it's posible.

Community
  • 1
  • 1
cernej
  • 31
  • 1
  • 3

1 Answers1

2

This one worked in my example

START a=node(1), d=node(4) 
MATCH p=a-[r:ACTIVATES*..]-d 
WITH head(relationships(p))as r1,last(relationships(p))as r2,p
WHERE all(r3 in relationships(p) 
      where r2.temperature > r1.temperature AND NOT r3.temperature < r1.temperature) 
return p;

Update: Is there any way to do that with node properties?

  • 1
    Hi. Thank you for the answer. But this won't work for me. This actualy matches **(A)--50-->(B)--70-->(C)--60-->(D)** but I need to match only **(A)--50-->(B)--60-->(C)--70-->(D)** – cernej Apr 11 '13 at 20:36
  • Theoretically it is possible to add special nodes with this properties. But in my case it will lead to extreme large and complicated graph (now my graph has 100k+ of nodes and I counted that this change would lead to hundred 100M+ of nodes) – cernej Apr 12 '13 at 09:26
  • If node properties can be used for incremental property path finding te you can add to your existing nodes the property value of the relationship wit something like this `foreach(r in relationships(p): FOREACH(x in nodes(p):SET x.value=r.value))`. – SotirisTsartsaris Apr 13 '13 at 10:32