0
votes

graph snippet

I have a Shape with a list of Points.

I have the following requirements:

1) retrieve an ordered set of Points;

2) insert/remove a Point and preserve the order of the rest of the Points

I can achieve this by:

A) Point has a sequence integer property that could be used to order;

B) Add a :NEXT relationship between each Point to create a linked list.

I'm new to Neo4j so not sure which approach is preferable to satisfy the requirements?

For the first requirement, I wrote the following queries and found the performance for the traversal to be poor but Im sure its a badly constructed query:

//A) 146 ms
Match (s:Shape {id: "1-700-y11-1.1.I"})-[:POINTS]->(p:Point)
return p
order by p.sequence;

//B) Timeout! Bad query I know, but dont know the right way to go about it!
Match  path=(s:Shape {id: "1-700-y11-1.1.I"})-[:POINTS]->(p1:Point)-[:NEXT*]->(p2:Point)
return collect(p1, p2);
1

1 Answers

2
votes

To get ordered list of points use a slightly modified version of the second query:

Match  path=(s:Shape {id: "1-700-y11-1.1.I"})-[:POINTS]->(P1:Point)
                                             -[:NEXT*]-> (P2:Point)
       WHERE  NOT (:Point)-[:NEXT]->(P1) AND
              NOT (P2)-[:NEXT]->(:Point)
RETURN TAIL( NODES( path) )

And for example query to delete:

WITH "id" as pointToDelete
MATCH (P:Point {id: pointToDelete})
OPTIONAL MATCH (Prev:Point)-[:NEXT]->(P)
OPTIONAL MATCH (P)-[:NEXT]->(Next:Point)
FOREACH (x in CASE WHEN Prev IS NOT NULL THEN [1] ELSE [] END |
  MERGE (Prev)-[:NEXT]->(Next)
)
DETACH DELETE P