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.

How can i find the graphs having a certain length not longer?

gigauser
Graph Buddy

i tried to find graphs only having 2 hops with the query, "match p=(a)-[*2]-(b)" return p"
but it returned also the graphs having more than 2 hops(i.e. more than 3 nodes) as well as the paths having 2 hops

3 REPLIES 3

If you're talking about the graphical display using the browser, there's a feature that's on by default which shows all relationships between the nodes returned.

In the browser settings (gear icon in the lower left), scroll to the bottom and uncheck the "Connect result nodes" checkbox.

Also, keep in mind that what that pattern is looking for is any a and b node that are two hops from each other, so if your entire graph consists of 4 consecutive nodes (let's call them node 1, node 2, node 3, and node 4) that looks something like this: (1)-[:REL]->(2)-[:REL]->(3)-[:REL]->(4), you may get paths like:

(1)--(2)--(3)
(3)--(2)--(1)
(2)--(3)--(4)
(4)--(3)--(2)

And while these would look as expected in the table or text result view, when showing all of these paths at once in the graph display view, you'll see the single path of all 4 nodes, because it correctly shows all of the paths returned at the same time, they just happen to intersect with each other. The graph result view is basically a union of all graph elements returned from the query across all rows.

This is correct behavior. Checking the table result view should confirm correct results.

If you want instead to get back results where a and b have no other connections to any other node beside the one in the path, then you'll have to restrict that in the query. Something like:

match p=(a)-[*2]-(b)
where size((a)--()) = 1 and size((b)--()) = 1
return p

What a great answer and help! Thanks for your kind explanation. I am sorry it is not so pretty when i present it in front of a customer. I questioned it as simple one but i need more general solution to get the graph having n hops only.

gigauser
Graph Buddy

now i seemed to find a prettier answer with the hint of andrew above.
match p = (a)-[ * ]-(b) where size(nodes(p)) = 3 return p
it can be generalized for any size of path.
you must check it in text view