Search code examples
neo4jcypherrelationships

How to design the graph to handle multi directional relationships in neo4j


I am trying to understand the way to handle a graph in neo4j based on multiple relationships.

For Example:

Let us assume there is one graph with three nodes Customer, Store, and Brands and there are the following relationships between those nodes:

Customer--Goesto-->Store  
Store--Sells-->Brand

"Customer" node is "bharath" and "Store" nodes are s1, s2 and "Brand" nodes b1,b2 and b3. s1 sells b1,b2 and s2 sells b1,b3. I want to find out if we can design the graph in such way to query the result to return the path from customer to brand based on a condition. In my case brand the customer wants is b2.

The required graph :

    Let the nodes be (:Customer) = c, (:Store) = s, (:Brand) = b

                      (c{name:"Blah"})
                              |
                          [:Goesto]
                         /         \
                 (s:{sname"s1")   (s{sname:{"s2"})
                     /                \
                 [:Sells]          [:Sells]
               /          \        /
    (b{bname:"b1})  (b{bname:"b2"})     

I am just wondering if we can design the graph and handle it properly in the above way.


Solution

  • You just express your pattern

    And you can add conditions on any element, also the relationship, like timestamps or preferences or correlations.

    And then return any node, relationship or the whole path.

    MATCH path = (c:Customer {name:"Blah"})-[goes:GoesTo]->(s:Store)-[sells:Sells]-(b:Brand {name:"b2})
    WHERE condition
    RETURN path