1
votes

I'm not sure if this can be done in any efficient way, but i'm hoping it can be.

I am getting a set of data with data on it that allows me to find very specific nodes. However this data is not ordered in any way in terms of how the nodes are connected.

What I am trying to do is to be able to find all the nodes in neo4J (up to 7) and then say with these 7 nodes, find the path that connects them all.

These given nodes will be the only nodes connected in the desired path.

basically i'm trying to get a set that looks like

1,2,3,4,5,6,7

and to be able to find 2->7->6->3<-5<-1->4

any help or direction would be greatly appreciated

1

1 Answers

0
votes

the way I would do it is the following:

You need a starting node from where on you will query the next 7 nodes. To be able to find the very first 7 nodes I would introduce a starting root node. Lets call it simply :Root .

MATCH (:Root)-[r:NEXT*1..7]->(x)<-[]-(y) RETURN x, y

or even simpler:

MATCH (:Root)-[r:NEXT*..7]->(x)<-[]-(y) RETURN x, y

:Root of course could be any other node in your set, to get the next seven nodes from there on.

Is this what you want?

Take a further look at the following neo4j cheat sheet, which has some great tips:

http://assets.neo4j.org/download/Neo4j_CheatSheet_v3.pdf

Regards

EDIT

Ok sorry, I misunderstood you.

Maybe this brings you further:

MATCH (n:Node) where n.refId in [1,2,3,4,5,6,7] 
MATCH (n2:Node) where n2.refId in [1,2,3,4,5,6,7]
MATCH p=shortestPath((q)-[:NEXT*]-(q2)) 
return collect(distinct p)

or if those numbers are node IDs than like this:

MATCH (n:Node) where id(n) in [1,2,3,4,5,6,7] 
MATCH (n2:Node) where id(n2) in [1,2,3,4,5,6,7]
MATCH p=shortestPath((q)-[:NEXT*]-(q2)) 
return collect(distinct p)

This actually returns all the paths between the given nodes as a collection. So it doesn't return a single path for all those nodes. I am not aware of a function doing that.

However the neo4j browser displays just a single path between all those nodes desired, because of it's auto complete function. So I think you would have to build your own logic in code, if you want to connect those paths to a single one.

Maybe this is at least a starting point for the problem