0

I am using Neo4j randomWalk algorithm on a base 2 types of nodes (N1 and N2) and one type of relationship (R1). The random walk algorithm returns a list of NodeId. The issue here is that I'd like to get the relationships R1 between the nodes of each path.

I've tried the following query :

    MATCH (n:N1) WHERE ID(n) = 38
    CALL algo.randomWalk.stream(ID(n), 4, 4)
    YIELD nodeIds
    UNWIND nodeIds as nodeId
    OPTIONAL MATCH (l:N1)-[r:R1]-(q:N2)
    WHERE (ID(l) = nodeId) AND (ID(q) in nodeIds)
    RETURN l,q, nodeIds, nodeId,ID(q), ID(l)

However, this is not a good solution cause it is showing every relationships between node l and node q that are present in nodeIds. Hence I don't get the path. Do you know how could I solve this problem?

Thanks for your time, Syndorik

Syndo rik
  • 755
  • 1
  • 7
  • 15
  • You say you want r but then you don't return it in your example. Also, why don't you just yield "path"? (https://neo4j.com/docs/graph-algorithms/current/algorithms/random-walk/) – Tezra May 17 '19 at 18:53
  • Well in this example I didn't return r because I just wanted to check wether the Ids I get are the Ids of the path. Moreover I don t yield the path variable, because the one created is a virtual one, without the property of the relationship. I gave an answer to my problem just below, you can check it out to see if it's good. – Syndo rik May 18 '19 at 19:10

1 Answers1

0

After some research, I found a way to get the nth element and nth+1 element in the same UNWIND loop.

This is what I did :

MATCH (n:Clips) WHERE ID(n) = 39
CALL algo.randomWalk.stream(ID(n),4,1)
YIELD nodeIds
UNWIND RANGE(0,size(nodeIds)-1) as idx
MATCH (l)-[r:R1]-(q)
WHERE ID(l) = nodeIds[idx] and ID(q) = nodeIds[idx+1]
RETURN ID(l), ID(q),nodeIds

With this, I successfully retrieved the path with relations and nodes. Do not hesitate if you have some questions on how it works. The image bellow is what I get.

The Result I'm getting

Syndo rik
  • 755
  • 1
  • 7
  • 15
  • If you yield the path instead, than can't you just `UNWIND RELATIONSHIPS(path) as rs RETURN startNode(rs) as l endNode(rs) AS q` to get the pairs? – Tezra May 20 '19 at 12:22
  • Well when I do that, Relationships(path) return an empty list since randomWalk actually creates only a virtual path. – Syndo rik May 21 '19 at 16:34