rayman
rayman

Reputation: 21616

Cant execute conditional query with shortestPath using Cypher

I have the following query:

MATCH (me:label1{userId:{1}})-[rel:nearby_group]-(group:GROUP)
WHERE NOT ((me)-[:relation_1|relation_2*1..3]-()--(group))
DELETE rel

ofcourse by Profiler I am getting lots of hits coz the second part of my query (the one after where not has lots of internal loops)

I wanted to improve it with shortestPath:

PROFILE 
MATCH (me:label1{userId:{1}})-[rel:nearby_group]-(group:GROUP)
WHERE NOT shortestPath((me)-[:relation_1|relation_2*1..3]-()--(group))
DELETE rel

but guess cant do it syntactically:

Neo.ClientError.Statement.InvalidSyntax

Any idea how can I use shortestPath in my query or improve it other way?

screenshot from profiler:

enter image description here

So I have modified the query this way:

PROFILE 
    MATCH (me:label1{userId:{1}})-[rel:nearby_group]-(group:GROUP)
    WHERE NOT shortestPath((me)-[:relation_1|relation_2*1..3]-(group))
    DELETE rel

But iam getting an empty result. I expected to see the following relationship to be deleted and it didnt:

enter image description here

Any idea?

Thanks, ray.

Upvotes: 0

Views: 80

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

Your query should be correct, except the use of 2 colons here :

(me:BT{userId:{1}})-[rel:nearby_group]-(group:GROUP)

should be

PROFILE MATCH (me:BT{userId:{1}})-[rel:nearby_group]-(group:GROUP) where not shortestPath((me)-[:relation_1|relation_2*1..3]-()--(group)) delete rel

UPDATE :

Also, shortestPath doesn't accept multiple relationship patterns defninitions, it is much more like (a)-[:TO*]->(b)

This one works in my browser :

PROFILE MATCH (me:BT{userId:{1}})-[rel:nearby_group]-(group:GROUP) where not shortestPath((me)-[:relation_1|:relation_2*..3]-(group)) delete rel

shortestPath doc

This means: find a single shortest path between two nodes, as long as the path is max 15 relationships long. Inside of the parentheses you define a single link of a path — the starting node, the connecting relationship and the end node. Characteristics describing the relationship like relationship type, max hops and direction are all used when finding the shortest path. If there is a WHERE clause following the match of a shortestPath, relevant predicates will be included in the shortestPath. If the predicate is a NONE() or ALL() on the relationship elements of the path, it will be used during the search to improve performance

http://neo4j.com/docs/developer-manual/current/#query-shortest-path

Upvotes: 0

Related Questions