0
votes

I have a large dense graph with millions of relationships. I want to get all possible paths of length 3 starting from a specific node. However, this results in thousands of possible paths, and it takes a lot of time. So I want to filter the intermediate nodes that I want to expand based on the weight of the node.

The beginning of the request is:

MATCH p=(e1:LabeledExperience { name: 'software developer'})-[n:NextExp *1..3]->(e2:LabeledExperience) 

And then UNWIND the nodes n and apply conditions. The problem with this is that it first matchs all possible paths, unwind them and after that applies the condition. Is there a way to do it at each step ?

1
What sort of conditions are you checking? How complex is the logic?Pablissimo

1 Answers

1
votes

Just take one step (relationship) at a time, with a WHERE clause per step to filter out unwanted nodes.

Something like this:

MATCH (e1:LabeledExperience {name: 'software developer'})-[:NextExp]->(e2:LabeledExperience)
WHERE e2.weight > 10
OPTIONAL MATCH (e2)-[:NextExp]->(e3:LabeledExperience)
WHERE e3.weight > 10
OPTIONAL MATCH (e3)-[:NextExp]->(e4:LabeledExperience)
WHERE e4.weight > 10
RETURN e1, e2, e3, e4

The steps after the first use OPTIONAL MATCH to let the query return a result even if e2 or e3 is a leaf node. Because of this, the returned e3 and/or e4 values can be null.