0
votes

I have two cypher queries that behave as expected. My graph simply consists of businesses connected by relationships.

# Finding the shortest path that exists between two given business nodes
START a=node:Businesses('id: xxx'), b=node:Businesses('id: yyy')
MATCH a, b, p= shortestPath((a)-[*..15]-(b))
RETURN p

# Find all nodes connected 1-step out from a given business node
START a=node:Businesses('id: xxx')
MATCH (a)-[r:isRelated*]->(d)
RETURN distinct d,r

I now want to combine aspects of these two queries into one. I want to find the shortest path between any two given nodes AND go 1-step out from the nodes in the returned path. I've tried the query below which doesn't work because p is returning a path and my second match statement is expecting a node.

START a=node:Businesses('id: xxx'), b=node:Businesses('id: yyy')
MATCH a, b, p= allShortestPaths((a)-[*..15]-(b))
WITH p
MATCH (p)-[r:isRelated*1]->(d)
RETURN distinct p,d,r

How should I go about writing this type of query?

1

1 Answers

2
votes

How about this:

START a=node:Businesses('id: xxx'), b=node:Businesses('id: yyy')
MATCH shortest=shortestPath((a)-[*..15]-(b)) 
WITH extract(n in nodes(shortest) | id(n)) as ids
MATCH pp=(x)-->(y)
WHERE id(x) in ids
RETURN pp

From the shortest path you collect all node ids along and then filter in the second part. I guess this query is not very efficient, but will do the job.