How to delete node on AgensGraph? - agens-graph

I try to delete node on agens graph and failed.
lee=# match (n:v{id:1}) delete n;
ERROR: vertex 4 in "v" has edge(s)
How to delete node?

Add detach before delete token.
assam=# match (n:v{id:1}) detach delete n;
GRAPH WRITE (DELETE VERTEX 1, DELETE EDGE 1)

Related

How to drop property constraint on AgensGraph?

I want to drop property index on AgensGraph.
agens=# create vlabel v;
CREATE VLABEL
agens=# create elabel e;
CREATE ELABEL
agens=# create property index on v ( id );
CREATE PROPERTY INDEX
agens=# create property index on e ( id );
CREATE PROPERTY INDEX
agens=# drop property index on v ( id );
ERROR: syntax error at or near "on"
LINE 1: drop property index on v ( id );
^
agens=# drop property index on e ( id );
ERROR: syntax error at or near "on"
LINE 1: drop property index on e ( id );
^
But, there is some problem on grammar.
How to drop property constraint on AgensGraph?
Name of property index is created automatically at creation time.
You can use '\dGe' and '\dGe' for find structure of labels.
agens=# \dGv v
List of labels
Graph | Name | Type | Owner
-------+------+--------+-------
graph | v | vertex | agens
(1 row)
Vertex label "graph.v"
--
Property Indexes:
"v_id_idx" btree (id)
Inherits: graph.ag_vertex
agens=# \dGe e
List of labels
Graph | Name | Type | Owner
-------+------+------+-------
graph | e | edge | agens
(1 row)
Edge label "graph.e"
--
Property Indexes:
"e_id_idx1" btree (id)
Inherits: graph.ag_edge
After search index name, drop property index next.
agens=# drop property index v_id_idx;
DROP PROPERTY INDEX
agens=# drop property index e_id_idx1;
DROP PROPERTY INDEX

How to find internal vertices in variable-length-edges on AgensGraph?

I tried to find internal vertices in variable-length-edges on AgensGraph.
But, It returns error message like following.
Is there problem on CYPHER query on mime?
How can I view internal vertices of VLE?
Attaching sample script follow.
create graph vle;
create vlabel o;
create vlabel l;
create elabel e;
create property index on o ( id );
create property index on l ( id );
create property index on e ( id );
create (:o{id:1})
create (:o{id:2})
create (:o{id:3})
create (:o{id:4})
create (:o{id:5})
create (:o{id:6})
create (:o{id:7})
create (:o{id:8})
create (:o{id:9});
match (o:o) create (:v{id:o.id});
match (n:v) where n.id >= 1 and n.id <= 9
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+1)}]->(:v{id:n.id*10+1})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+2)}]->(:v{id:n.id*10+2})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+3)}]->(:v{id:n.id*10+3})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+4)}]->(:v{id:n.id*10+4})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+5)}]->(:v{id:n.id*10+5})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+6)}]->(:v{id:n.id*10+6})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+7)}]->(:v{id:n.id*10+7})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+8)}]->(:v{id:n.id*10+8})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+9)}]->(:v{id:n.id*10+9});
match (n:v) where n.id >= 11 and n.id <= 99
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+1)}]->(:v{id:n.id*10+1})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+2)}]->(:v{id:n.id*10+2})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+3)}]->(:v{id:n.id*10+3})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+4)}]->(:v{id:n.id*10+4})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+5)}]->(:v{id:n.id*10+5})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+6)}]->(:v{id:n.id*10+6})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+7)}]->(:v{id:n.id*10+7})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+8)}]->(:v{id:n.id*10+8})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+9)}]->(:v{id:n.id*10+9});
match (n:v) where n.id >= 111 and n.id <= 999
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+1)}]->(:v{id:n.id*10+1})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+2)}]->(:v{id:n.id*10+2})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+3)}]->(:v{id:n.id*10+3})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+4)}]->(:v{id:n.id*10+4})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+5)}]->(:v{id:n.id*10+5})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+6)}]->(:v{id:n.id*10+6})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+7)}]->(:v{id:n.id*10+7})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+8)}]->(:v{id:n.id*10+8})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+9)}]->(:v{id:n.id*10+9});
match (n:v) where n.id >= 1111 and n.id <= 9999
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+1)}]->(:v{id:n.id*10+1})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+2)}]->(:v{id:n.id*10+2})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+3)}]->(:v{id:n.id*10+3})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+4)}]->(:v{id:n.id*10+4})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+5)}]->(:v{id:n.id*10+5})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+6)}]->(:v{id:n.id*10+6})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+7)}]->(:v{id:n.id*10+7})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+8)}]->(:v{id:n.id*10+8})
create (n)-[:e{id:'v:'+n.id+'->v:'+(n.id*10+9)}]->(:v{id:n.id*10+9});
match p = ( (v1:v{id:1})-[:e*]->(v2:v{id:11111}) ) return nodes(p);
ERROR: graph path and variable length edge cannot be used at the same time
LINE 1: match p = ( (v1:v{id:1})-[:e*]->(v2:v{id:11111}) ) return no...
^
Please, help...
It is impossible to get nodes between VLE path on older version of AgensGraph.
Think upgrading version of AgensGraph.
agens (AgensGraph 2.1.0, based on PostgreSQL 10.4)
Type "help" for help.
agens =# match p = ( (v1:v{id:1})-[:e*]->(v2:v{id:11111}) ) return nodes(p);
nodes
----------------------------------------------------------------------------------------------------
[v[6.1]{"id": 1},v[6.10]{"id": 11},v[6.91]{"id": 111},v[6.820]{"id": 1111},v[6.7381]{"id": 11111}]
(1 row)

How to create node on AgensGraph?

After connected to AgensGraph.
I try to create node on AgensGraph.
But, there is a error on action.
kurt=# create (:v{id:1});
ERROR: graph_path is NULL
HINT: Use SET graph_path
How to set graph_path?
You must create graph, before create vertex(node).
assam=# create graph new_graph;
CREATE GRAPH
After creating graph, set graph_path.
assam=# set graph_path = new_graph;
SET
Finally, You can create a vertex(node).
assam=# create (:v{id:1});
GRAPH WRITE (INSERT VERTEX 1, INSERT EDGE 0)
assam=# match (n:v) return n;
n
-----------------
v[3.1]{"id": 1}
(1 row)

neo4j delete a node in linked list

I have a list of posts that belong to a user. The posts are structured as linked list that belongs to a given user.
I am having deleting the a given node while preserving the data structure.
Here is an illustration of the structure.
In my list attempt I have written the query below, however it does not work since the MATCH does not return anything in the sub-queries.
I thought about using OPTIONAL MATCH in the sub-queries, however that results in exceptions.
MATCH(node:Post)
WHERE ID(node) = 2749
WITH node
MATCH(user:User)-[:OWNS]->(node)-[:PREV]->(nextNode:Post)
CREATE(user)-[:OWNS]->(nextNode)
WITH node
MATCH(prevNode:Post)-[:PREV]->(node)-[:PREV]->(nextNode:Post)
CREATE(prevNode)-[:PREV]->(nextNode)
WITH node
DETACH DELETE node
I assumed that if a pattern in a sub-query fails than the rest of the sub-query will be skipped and the next sub-query will still execute. But it seems that the whole thing does not get executed.
How do we perform something like in the following in sub-queries without halting the execution.
IF pattern matches
THEN
CREATE EDGE
IF pattern matches
THEN
Do something else
Solution
stdob--'s answer is the closest to the right solution, however it fails to delete nodes at the end of the list and throws an exception as it does not check if the nextNode exists in the second FOREACH statement.
It needed tweaking to work:
MATCH(node:Post) WHERE ID(node) = 2813
OPTIONAL MATCH (user:User)-[:OWNS]->(node)
OPTIONAL MATCH (node)-[:PREV]->(nextNode:Post)
OPTIONAL MATCH (prevNode:Post)-[:PREV]->(node)
FOREACH (ith in CASE
WHEN user IS NOT NULL AND nextNode IS NOT NULL
THEN [1]
ELSE []
END | CREATE (user)-[:OWNS]->(nextNode)
)
FOREACH (ith in CASE WHEN
prevNode IS NOT NULL AND nextNode IS NOT NULL
THEN [1] ELSE []
END | CREATE (prevNode)-[:PREV]->(nextNode)
)
DETACH DELETE node
You can use combination of OPTIONAL MATCH, FOREACH and CASE:
MATCH(node:Post) WHERE ID(node) = 2749
OPTIONAL MATCH (user:User)-[:OWNS]->(node)
OPTIONAL MATCH (node)-[:PREV]->(nextNode:Post)
OPTIONAL MATCH (prevNode:Post)-[:PREV]->(node)
FOREACH (ith in CASE WHEN
NOT user IS NULL AND
NOT nextNode IS NULL
THEN [1] ELSE [] END |
CREATE (user)-[:OWNS]->(nextNode)
)
FOREACH (ith in CASE WHEN
NOT prevNode IS NULL AND
NOT nextNode IS NULL
THEN [1] ELSE [] END |
CREATE (prevNode)-[:PREV]->(nextNode)
)
DETACH DELETE node
Update: Also you can use the procedure apoc.do.when from the apoc.library for the conditional query execution.
I recreated your data set with the following query:
CREATE
(u:User {name: 'Alice'})-[:OWNS]->
(p1:Post {number: 1})-[:PREV]->
(p2:Post {number: 2})-[:PREV]->
(p3:Post {number: 3})-[:PREV]->
(p4:Post {number: 4})-[:PREV]->
(p5:Post)
As you mentioned, using OPTIONAL MATCH can result in exceptions,
something along the lines of:
Expected to find a node at prevNode but found nothing Some(NO_VALUE)
However, you can work around this, by first doing the DELETE, then passing on the value using a WITH clause and only running the CREATE if the variable introduced by the OPTIONAL MATCH clause IS NOT NULL.
MATCH (user:User)-[:OWNS]->(node)-[:PREV]->(nextNode:Post)
WHERE node.number = 1
CREATE (user)-[:OWNS]->(nextNode)
WITH node, nextNode
OPTIONAL MATCH (prevNode:Post)-[:PREV]->(node)
DETACH DELETE node
WITH prevNode, nextNode
WHERE prevNode IS NOT NULL
CREATE (prevNode)-[:PREV]->(nextNode)

Postgresql/ database - traceout on graph with stop criterium (topology)

I have a undirected graph and want to make a traceout. This works fine with:
WITH RECURSIVE path AS (
SELECT edge_id, start_node, end_node
FROM simulation.edge_data
WHERE start_node = 1 OR end_node = 1
UNION
SELECT e.edge_id, e.start_node, e.end_node
FROM simulation.edge_data e, path s
WHERE s.start_node = e.start_node OR s.start_node = e.end_node OR s.end_node = e.end_node OR s.end_node = e.start_node)
SELECT * FROM path;
Now I want to stop at certain nodes if the corresponding record of the node has the status = closed.
Not further investigate on not reachable nodes.
I tried to add something in the where clause:
(SELECT status FROM getRecord(e.start_node)) = open
This gives me all the correct nodes but not all edges.
I can provide more code if needed dont want to overdo if the answer is quiet simple!

Resources