Stuck up with same output in shortestPath and AllShortestPath

cypher
knowledge-base

(Sucheta) #1

Hi,

Please help me segregate the output and get the quest to finding the shortestPath categories right.

My shortestPath Query and output is -

MATCH (ms:cabinet ),(cs:activityname ) , p = shortestPath((ms)-[*]-(cs))  WITH p  RETURN p

My AllShortestPath Query and output is -

WITH ["abc","william"] AS users
MATCH path=allShortestPaths((n1:cabinet)-[*]-(n2:activityname)) 
RETURN path

I even tried this query but it gives me error -

MATCH (n:cabinet)
  WHERE n.name IN {cabinet}
WITH collect(n) AS nodes
UNWIND nodes AS n
UNWIND nodes AS m
WITH *
  WHERE id(n) < id(m)
MATCH path = allShortestPaths((n)-[relation*..10]-(m))
  WHERE ALL(x IN relation
    WHERE x.type = 'Parameter')
WITH
  collect({ path: path, pathLength: length(path) }) AS data,
  max(length(path)) AS maxLength
WITH [row IN data WHERE row.pathLength = maxLength] AS rows
UNWIND rows AS row
RETURN row.path AS path

Error -

Neo.ClientError.Statement.ParameterMissing: Expected parameter(s): cabinet


(Andrew Bowman) #2

Hi Sucheta,

The last query you have looks mostly right, with a few exceptions:

  1. In your allShortestPaths() match, your predicate for all relationships having the type Parameter isn't quite right. What you have here looks for a type property = 'Parameter', but the type of a relationship isn't a property. For this comparison, you should use WHERE type(x) = 'Parameter'. That said, since you already know what the type should be, and it likely isn't dynamic, it's better to put this into the pattern itself that way you don't even need a WHERE clause: MATCH path = allShortestPaths((n)-[relation:Parameter*..10]-(m))

  2. The error shows that you didn't provide a cabinet parameter (which is used in the WHERE clause of your first MATCH). If you're executing this via the browser, you need to set a parameter cabinet which should be a list of string names. Use :help param from the browser to get syntax help for how to set a parameter before you execute your query.


(Sucheta) #3

Hi Andrew,

I ran the query as -

MATCH path = allShortestPaths((n)-[relation*..10]-(m))
  WHERE ALL(x IN relation
  WHERE type(x) = 'Parameter')  RETURN path

o/p -

Neo.DatabaseError.Statement.ExecutionFailed
Neo.DatabaseError.Statement.ExecutionFailed: The shortest path algorithm does not work when the start and end nodes are the same. This can happen if you
perform a shortestPath search after a cartesian product that might have the same start and end nodes for some
of the rows passed to shortestPath. If you would rather not experience this exception, and can accept the
possibility of missing results for those rows, disable this in the Neo4j configuration by setting
`cypher.forbid_shortestpath_common_nodes` to false. If you cannot accept missing results, and really want the
shortestPath between two common nodes, then re-write the query using a standard Cypher variable length pattern
expression followed by ordering by path length and limiting to one result.

[2]

I also tried this cypher -

MATCH path = allShortestPaths((n)-[relation:Parameter]-(m)) RETURN path

o/p -

Neo.DatabaseError.Statement.ExecutionFailed
Neo.DatabaseError.Statement.ExecutionFailed: The shortest path algorithm does not work when the start and end nodes are the same. This can happen if you
perform a shortestPath search after a cartesian product that might have the same start and end nodes for some
of the rows passed to shortestPath. If you would rather not experience this exception, and can accept the
possibility of missing results for those rows, disable this in the Neo4j configuration by setting
`cypher.forbid_shortestpath_common_nodes` to false. If you cannot accept missing results, and really want the
shortestPath between two common nodes, then re-write the query using a standard Cypher variable length pattern
expression followed by ordering by path length and limiting to one result.

accordingly, i tried to find the variable -

cypher.forbid_shortestpath_common_nodes

in the Neo4j-Home folder, and inside conf/neo4j.conf and also inside other folders. But i could not find the boolean variable.

I am not sure if that is the correct folder for the Neo4j desktop version because i downloaded neo4j couple of times when i got error max entries exceeded and i am currently using the image of neo4j to start & run neo4j . So does my folder neo4j-community-3.4.9 proper and correct folder for neo4j-desktop-offline-1.1.10-x86_64 (1).AppImage ? Does this neo4j initialized from Image file have a folder repository? Please help.


(Andrew Bowman) #4

You can get around that error by adding a predicate to ensure the start and end nodes are never the same:

MATCH path = allShortestPaths((n)-[relation*..10]-(m))
  WHERE n <> m AND ALL(x IN relation WHERE type(x) = 'Parameter')  
RETURN path

(Sucheta) #5

Hi Andrew,

I ran the query you suggested -

MATCH path = allShortestPaths((n)-[relation*..10]-(m))
WHERE n.company="cateina" AND m.name="cabinet" AND ALL(x IN relation WHERE type(x) = 'Parameter')
RETURN path

And the output i got is -


#### Neo.DatabaseError.Statement.ExecutionFailed

Neo.DatabaseError.Statement.ExecutionFailed: The shortest path algorithm does not work when the start 
and end nodes are the same. This can happen if you perform a shortestPath search after a cartesian 
product that might have the same start and end nodes for some of the rows passed to shortestPath. If you 
would rather not experience this exception, and can accept the possibility of missing results for those rows, 
disable this in the Neo4j configuration by setting `cypher.forbid_shortestpath_common_nodes` to false. If you
 cannot accept missing results, and really want the shortestPath between two common nodes, then re-write 
the query using a standard Cypher variable length pattern expression followed by ordering by path length 
and limiting to one result.

And the name is correct that i entered. i.e. - cabinet

query -

MATCH (n)-[r:Parameter]->(m) WHERE m.name = "cabinet" RETURN n,m,r

Please help


(Andrew Bowman) #6

Please re-read my last comment. You need to have a predicate which says that the start and end nodes are not the same:

WHERE n <> m

The property checking isn't enough here, the planner is looking for something like this.