Moontasir Mahmood
Moontasir Mahmood

Reputation: 105

How can we create undirected edge between two nodes in apache age graph database

How do I connect undirected edge between these 2 nodes as follow :

SELECT * from cypher(
'munmud_graph', 
$$ 
    CREATE (v:Country {"name" : "Bangladesh"}) 
    RETURN v
$$
) as (v agtype); 
SELECT * from cypher(
'munmud_graph', 
$$ 
    CREATE (v:Country {"name" : "India"}) 
    RETURN v
$$
) as (v agtype); 

I want to connect these two node with an undirected edge with label Neighbour

Upvotes: 1

Views: 201

Answers (4)

Vinay Kumar Talreja
Vinay Kumar Talreja

Reputation: 61

Try this query:

SELECT * FROM cypher('munmud_graph', 
$$
    MATCH (a:Country {name: "Bangladesh"}), (b:Country {name: "India"})
    CREATE (a)-[:NEIGHBOR]->(b),
           (b)-[:NEIGHBOR]->(a)
    RETURN a, b
$$
) AS (a agtype, b agtype);

Here I have combined both directions from A to B and B to A.

Upvotes: 0

MOIEZ IBRAR
MOIEZ IBRAR

Reputation: 23

SELECT * from cypher(
'munmud_graph', 
$$ 
    MATCH (a:Country {name: "Bangladesh"}), (b:Country {name: "India"})
    CREATE (a)-[:Neighbour]->(b)
    RETURN a, b
$$
) as (a, b);                                                                         

Hope this will work for you

Upvotes: 1

Matheus Farias
Matheus Farias

Reputation: 715

I believe that, as of today, the only way to do this with Apache AGE is to set the edge with a property that resembles this undirection or set it as bidirectional.

Considering that, both of the following examples doesn't work:

-- EXAMPLE 1 : UNDIRECTED EDGE (DOES NOT WORK)

SELECT * FROM cypher ('demo', $$
MATCH (a:Country), (b:Country)
WHERE a.name = "India" AND b.name = "Bangladesh"
CREATE (a)-[e:BORDERS_WITH]-(b)
RETURN e
$$) as (e agtype);

ERROR:  only directed relationships are allowed in CREATE
LINE 4: CREATE (a)-[e:BORDERS_WITH]-(b)


-- EXAMPLE 2 : BIDIRECTIONAL EDGE (DOES NOT WORK)

SELECT * FROM cypher ('demo', $$
MATCH (a:Country), (b:Country)
WHERE a.name = "India" AND b.name = "Bangladesh"
CREATE (a)<-[e:BORDERS_WITH]->(b)
RETURN e
$$) as (e agtype);

ERROR:  syntax error at or near ">"
LINE 4: CREATE (a)<-[e:BORDERS_WITH]->(b)

But this works:

-- EXAMPLE 3 : SET BIDIRECTIONAL EDGE AS PROPERTY (WORKS)

SELECT * FROM cypher ('demo', $$
MATCH (a:Country), (b:Country)
WHERE a.name = "India" AND b.name = "Bangladesh"
CREATE (a)-[e:BORDERS_WITH{ type:"<->" }]->(b)
RETURN e
$$) as (e agtype);

                                                                        e
--------------------------------------------------------------------------------------------------------------------------------------------------
 {"id": 2533274790395905, "label": "BORDERS_WITH", "end_id": 2251799813685249, "start_id": 2251799813685250, "properties": {"type": "<->"}}::edge
(1 row)

demo=#

Then, if you want to find which edges are of type "<->" you just have to type this query:

SELECT * FROM cypher ('demo', $$
MATCH (a)-[e:BORDERS_WITH]->(b)
WHERE e.type = "<->"
RETURN e
$$) as (e agtype);
                                                                        e
--------------------------------------------------------------------------------------------------------------------------------------------------
 {"id": 2533274790395905, "label": "BORDERS_WITH", "end_id": 2251799813685249, "start_id": 2251799813685250, "properties": {"type": "<->"}}::edge
(1 row)

Upvotes: 1

Taylor Riggan
Taylor Riggan

Reputation: 2759

Creating and storing of undirected edges are is supported, although you can read/query-for edges and ignore their direction.

To create the edge, you would do something like the following:

SELECT * from cypher(
'munmud_graph', 
$$ 
    MATCH
      (x:Country),
      (y:Country)
    WHERE x.name = 'Bangladesh' AND y.name = 'India'
    CREATE (x)-[r:Neighbour]->(y)
    RETURN r
$$
) as (r agtype); 

And then to query for the edge and ignore its direction, you would do:

SELECT * from cypher(
'munmud_graph', 
$$ 
    MATCH (x:Country)-[r]-(y:Country)
    WHERE x.name = 'Bangladesh' AND y.name = 'India'
    RETURN r
$$
) as (r agtype); 

Upvotes: 2

Related Questions