Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

neo4j

cypher

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.

like image 811
Orion Avatar asked Dec 31 '12 17:12

Orion


People also ask

How do I return all nodes and relationships in Neo4j?

When you want to return all nodes, relationships and paths found in a query, you can use the * symbol. This returns the two nodes, the relationship and the path used in the query.

Can relationships have properties in Neo4j?

The Neo4j Graph Data Science Library provides multiple operations to work with relationships and their properties stored in a projected graphs. Relationship properties are either added during the graph projection or when using the mutate mode of our graph algorithms.

Why would you use Neo4j match?

The MATCH clause allows you to specify the patterns Neo4j will search for in the database. This is the primary way of getting data into the current set of bindings. It is worth reading up more on the specification of the patterns themselves in Patterns.

When must you use a variable in a simple match clause?

If you want to return the relationship between two nodes or a property of the relationship, a variable is required. A directed or undirected relationship can be used.


1 Answers

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;
like image 145
ulkas Avatar answered Oct 16 '22 13:10

ulkas