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.

Is there any workaround for "allShortestPaths(...) requires a pattern containing a single relationship"

I have the following structure

(r:Industry)->[:REFERS]-(data:IndustryData)-[:OCCURRED]->(m:Month)
(r:Region)<-[:REFERS]-(data:RegionData)-[:OCCURRED]<-(m:Month)
(s:Size)<-[:REFERS]-(data:SizeData)-[:OCCURRED]<-(m:Month)

So, all the entities share Month nodes.

Using Industry as an input I want to find all the paths to Region and Size.

I can do it for 1 relationship, e.g. Industry to Size:

MATCH (i:Industry)
WHERE i.value in ["7379", "73", "Information Technology"]
MATCH (s:Size)
WHERE s.value in ["Large2", "Large"]
MATCH path=allshortestPaths((i)-[*]->(s))
RETURN path

However, when I execute the next query

MATCH (i:Industry)
WHERE i.value in ["7379", "73", "Information Technology"]
MATCH (s:Size)
WHERE s.value in ["Large2", "Large"]
MATCH (r:Region)
WHERE r.value in ["USA", "North America"]
match path=allshortestPaths((r)<-[*]-(i)-[*]->(s))
return path
LIMIT 50

I receive the following error allShortestPaths(...) requires a pattern containing a single relationship

What alternatives do I have? Changing the structure also is an option for me.

1 ACCEPTED SOLUTION

Bennu
Graph Fellow

Hi @miron4dev,

I'm not sure about your strategy with shortestPath. You may be getting the answers that you want without the right strategy to do so. In any case, try this query:

MATCH (i:Industry)
WHERE i.value in ["7379", "73", "Information Technology"]
with collect(i) as inter
MATCH (s:Size)
WHERE s.value in ["Large2", "Large"]
with inter, s
MATCH (r:Region)
WHERE r.value in ["USA", "North America"]
with inter, s, r
match path=allshortestPaths((r)-[*]-(s))
with r, s, inter, path, nodes(path) as ns
where any(n in ns WHERE n in inter)
return path
LIMIT 50

H

View solution in original post

4 REPLIES 4

Bennu
Graph Fellow

Hi @miron4dev,

I'm not sure about your strategy with shortestPath. You may be getting the answers that you want without the right strategy to do so. In any case, try this query:

MATCH (i:Industry)
WHERE i.value in ["7379", "73", "Information Technology"]
with collect(i) as inter
MATCH (s:Size)
WHERE s.value in ["Large2", "Large"]
with inter, s
MATCH (r:Region)
WHERE r.value in ["USA", "North America"]
with inter, s, r
match path=allshortestPaths((r)-[*]-(s))
with r, s, inter, path, nodes(path) as ns
where any(n in ns WHERE n in inter)
return path
LIMIT 50

H

My idea was in using cost attribute as a weight for relationships, but it seems allshortestPaths function doesn't take it into account.

Hi @miron4dev,

allshortestPaths/shortestPath just takes into consideration the number of hops. Was the previous query useful? I'm trying to get more insight on your intentions 🙂

You'll probably want to look at the Graph Data Science library and its shortest path implementation, that should take into account relationship weights.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online