Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
08-19-2020 05:00 PM
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.
Solved! Go to Solution.
08-20-2020 04:39 AM
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
08-20-2020 04:39 AM
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
08-21-2020 02:10 AM
My idea was in using cost attribute as a weight for relationships, but it seems allshortestPaths function doesn't take it into account.
08-21-2020 02:27 AM
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 🙂
08-24-2020 10:36 PM
You'll probably want to look at the Graph Data Science library and its shortest path implementation, that should take into account relationship weights.
All the sessions of the conference are now available online