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.

All paths with 2 relations by nodes

Hi,

This is my graph :
2X_f_f461a04bf13aebe537a6e00476e52657cb8f8a69.png

I want to do a request that return all the loops in the graph (all the paths), where all the node have two relations, one that leave and one that arrive (one :NEED and one :OFFEREDBY).

How can I restrict (write) the request ? I didn't manage to do it with [:RELTYPE'*1] or [:RELTYPE'*1..1]

In other words, this is the answer desired :

thank you!

7 REPLIES 7

Hello,

Can you elaborate on this? What constitutes a valid result vs an invalid result? Some examples showing what you want, and what you don't want may help.

Hi,
I can put more pictures,

But I dont want all the graph in the answer, I want all the different closed loops, all the different paths.

I hope to be more clear

In a previous edit I saw you had a query already that generated the patterns, it looks like you want to refine that query?

Will something like this work for you?

(start)-[:NEED|OFFEREDBY*..16]->(start)

With some acceptable upper bound of course. This traverses all outgoing :NEED and :OFFEREDBY relationships, and the results back will be those that loop back to the start.

Unfortunately, this give me all the graph again :

2X_8_870b65976e217837f91972d2da8b810737030c51.png

I really would like two distinct paths that show the loops that the graph have (only with 2 relations by nodes, one arriving, one leaving).

A query that give me that :

Oh, so you want the graph to show the same node per pattern where it's matched?

That can't currently be done by the Neo4j visualizer. Nodes are distinct, it can't display the same node multiple times in different contexts. The graphical results are a union of the query results. You would need to execute separate queries (maybe using SKIP and LIMIT) to visualize each graph separately.

Oh, I see..
It gonna be less easy than expected..

Thank you so much for your help !

It's an awesome question!

Unfortunately Neo4j has a built-in behaviour by MATCH clause to avoid passing 2 times on the same relationship. But you can use this behaviour for your self, knowing that neo4j will always short the result the same way.

As Andrew said ( I didn't know ), the visualiser can only show a node one time, so from the Neo4j desktop API you have no choice to do two or more queries.

I considered your A,B... and R1,R2... as labels to simplify my example.

Query:

MATCH paths = (:A)-[*]->(:A)
RETURN paths SKIP x LIMIT 1

Increment x for each loop (request ) you want to see in your graph begin by 0

From a home made visualiser:

MATCH paths = (:A)-[*]->(:A)
RETURN paths

And use the json or csv result showing all your paths "loops".
You can export the json or csv from the neo4j desktop app dedicated button.