Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

neo4j - Cypher query: Finding all paths between two nodes filtered by relationship properties

I have the following graph as a Neo4j graph database:

                           activates
                            (80 °F)
          (A)------------------------------------->(D)
           | \__                                _/->^
           |    \__  activates               __/    |
           |       \__(50 °F)             __/       |
           |          \__              __/          |             
           |             \__        __/             | 
activates  |                \__  __/                |
 (50 °F)   |                   /                   | activates
           |                 __/\__                 | (50 °F)
           |    activates __/      \__              |
           |    (60 °F)__/            \__           |
           |        __/                  \__        |
           |     __/                        \__     |
           |  __/                              \_   |
           v /                                   ->|
          (B)------------------------------------->(C)
                           activates                          
                            (50 °F)

Each relationship has a property denoting the required temperature for the 'activates' action.

I need to retrieve all the available paths between (A) and (D) WHERE the temperature is 50 °F along the path.

The output should include:

A -[:activates{temperature:'50'}]-> B -[:activates{temperature:'50'}]-> C -[:activates{temperature:'50'}]-> D

A -[:activates{temperature:'50'}]-> C -[:activates{temperature:'50'}]-> D

but not

A -[:activates{temperature:'80'}]-> D

A -[:activates{temperature:'50'}]-> B -[:activates{temperature:'60'}]-> D

How do I write the required Cypher query?

Thanks in advance.

Edit 1: I added another diagonal relationship (B -[:activates{temperature:'80'}]-> D) for more clarity.

Edit 2: I need to retrieve all the available paths between (A) and (D) WHERE the temperature is the same along the path, i.e: A -> B -> C -> D, A -> C -> D, A -> D.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
START a=node({A}), d=node({D})
MATCH p=a-[r:ACTIVATES*..]-d
WHERE has(r.temperature) and r.temperature='50'
RETURN p;

substitute the values in the curved brackets (A,D) with their node IDs

update: using function all

START a=node(1), d=node(4) 
MATCH p=a-[r:ACTIVATES*..]-d 
WITH head(relationships(p))as r1,p //since the pointer r is a collection of rels we must declare a single relationship pointer
WHERE all(r2 in relationships(p) 
          where r2.temperature=r1.temperature) 
return p;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...