cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Query Variable-length pattern with the same relationship property

Now our team use neo4j as a tool to implement MDM solution, we create all system as a node and all data exchanges as relationship and labeled "OUT" in neo4j.

We want to query what kinds of data transfers more than 2 system, so we use the cypher like following

MATCH (E:System)-[r:OUT]->(A:System)-[p:OUT]->(C:System) where r.description=p.description return *

and we got the result

But we can only get data transfer exactly 2 system, how about 3,4 or more?

so we want to try use variable-length pattern to get the result. But how can we restrict all OUT relation with the same property in a set of result like

MATCH test = (E:System)-[r:OUT*2]->(C:System) where ALL r.description is same

thanks!

5 REPLIES 5

Hi,

maybe I can help you with this. I think what you can try is:

MATCH test = (E:System )-[:OUT*]-(C:System) WHERE all(r in relationships(test) WHERE r.description = "someDescription")

This will at least give you all the Systems (independent of number of relationships away) for A certain description.

If you want to have it for all descriptions, maybe you might need a match and an unwind statement beforehand, something like:

MATCH (E:System)-[r:OUT]->(C:System) 
WITH DISTINCT r.description
COLLECT (r.description) AS allDescriptions
UNWIND allDescriptions AS someDescription
MATCH test = (E:System )-[:OUT*]-(C:System) WHERE all(r in relationships(test) WHERE r.description = someDescription)

No guarantee on the above code working since I haven't tested it on your node setup 😉

I hope this helps.
Cheers,
Elena

I would like to add two things

  1. Use distinct keyword in Collect as mentioned below
  2. Make sure there is not cycle present

MATCH (E:System)-[r:OUT]->(C:System)
WITH DISTINCT r.description
COLLECT ( distinct r.description) AS allDescriptions
UNWIND allDescriptions AS someDescription
MATCH test = (E:System )-[:OUT*]-(C:System) WHERE all(r in relationships(test) WHERE r.description = someDescription)

Hi Vivek,
Thanks! but we got the cycle present, and I modified the [:OUT*] into [:OUT2..3] and it works, but I still want to know if I want to use [OUT] to search, is there any method to restrict, like by
n in nodes(test) where count(n)< 3

@as93717913

Sorry I did not understand your question

Hi Vivek,

Cause our system graph have cycle present, so when I use [:OUT*] to query, my neo4j will crash.
And I wonder know, to solve this problem, can I use some condition to restrict every count of nodes in one path can not larger then 2, that mean the path (a)-(b)-(a) is not meet the condition cause node "a" is present 2 times and (a)-(b)-(c)-(d) is matched cause every node in path is presented 1 times only.