1

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

6 Answers6

2

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); 
Taylor Riggan
  • 1,963
  • 6
  • 12
1

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)
Matheus Farias
  • 716
  • 1
  • 10
  • That last one would actually work better if you didn't match on direction. You can match on (a)-[e]-(b), you just cannot create an edge like that. – Taylor Riggan Feb 03 '23 at 15:21
  • But for this example wouldn't it repeat the same edge twice? – Matheus Farias Feb 03 '23 at 18:51
  • If only a single edge is created, then matching on (a)-[e]-(b) only returns a single edge. You'd have to create edges in both directions for a match on (a)-[e]-(b) to return the edges in both directions. – Taylor Riggan Feb 04 '23 at 14:16
  • 1
    Just to clarify this further, if the MATCH pattern were unbound (as in ()-[e]-() ) and if you were querying on some aspect of that edge, then the edge would get returned twice. But because we're only storing one edge from a to b, then we should only see the one result. – Taylor Riggan Feb 09 '23 at 15:31
1
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

0

Try the following cyper query:

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

The MATCH clause in the above query is used to find the "Bangladesh" and "India" nodes based on their name property.

Then, it creates an undirected edge between these two nodes using the CREATE clause and the Neighbour label. In the end it returns the nodes a and b to confirm that the edges are created.

0

To connect the two nodes "Bangladesh" and "India" with an undirected edge labeled "Neighbour," you can follow these steps using Cypher queries:

Firstly create the nodes for Bangladesh and India then use the following query to create the undirected edge between these two nodes with the label "Neighbour":

   MATCH (v1:Country {name: "Bangladesh"}), (v2:Country {name: "India"})
    CREATE (v1)-[:Neighbour]-(v2)
    RETURN v1, v2

MATCH clause is used to find these nodes based on their names. The CREATE clause with the relationship pattern CREATE (v1)-[:Neighbour]-(v2) creates an undirected edge between the two nodes with the "Neighbour" label.

0

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.