1

I am facing a problem when I try to use STACK_MATCH relationships to connect Person nodes having the 'backend' title, while avoiding Person nodes ending up being related to themselves.

SELECT * FROM cypher('devbook', $$ 
    MATCH (a: Person), (b: Person) 
    WHERE a.title = 'backend' AND b.title = 'backend' AND id(a) != id(b)
    CREATE (a)-[e:STACK_MATCH { title: 'backend' }]->(b) RETURN e 
$$) as (relationship agtype);

The main problem is that the != operator doesn't work. I've also tried using the <> operator, but it still creates relationships with other nodes that don't have the 'backend' title.

How can I use the != operator in the right way?

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • 1
    I would think twice before `CREATE`ing all those relationships. With `N` such `Person` nodes, you'd end up with `N*(N-1)` extra relationships, and every pair of such nodes will have relationships going in both directions. (Also, if you re-ran that query, it would create yet another set of relationships.) Do you have a use case that really requires those relationships? – cybersam Mar 28 '23 at 20:13
  • What exactly is the 'problem' you are facing? Is it showing an error message, or is the output not what you are expecting? – Ken W. Mar 28 '23 at 21:02

1 Answers1

2

In the given Apache AGE PostgreSQL query, the error is in the line AND id(a) != id(b). The use of the != it is supposed to be <>

Setup:
Insert multiple vertices with different vid and make some of their title not set to 'backend' to check whether it gets connected or not

Data setup (3 vertices with title backend and 3 with another titles)

SELECT * 
FROM cypher('my_graph', $$
    CREATE (a:Person {title: 'backend', vid: 1})
    RETURN a                                                            
$$) as (a agtype);

Query

SELECT *
FROM cypher('devbook', $$
    MATCH (a: Person), (b: Person)
    WHERE a.title = 'backend' AND b.title = 'backend' AND id(a) <> id(b)
    CREATE (a)-[e:STACK_MATCH { title: 'backend' }]->(b) RETURN e
$$) as (relationship agtype);

Output

                                                                    relationship                                                                    
----------------------------------------------------------------------------------------------------------------------------------------------------
 {"id": 1125899906842625, "label": "STACK_MATCH", "end_id": 844424930131970, "start_id": 844424930131969, "properties": {"title": "backend"}}::edge
 {"id": 1125899906842626, "label": "STACK_MATCH", "end_id": 844424930131971, "start_id": 844424930131969, "properties": {"title": "backend"}}::edge
 {"id": 1125899906842627, "label": "STACK_MATCH", "end_id": 844424930131969, "start_id": 844424930131970, "properties": {"title": "backend"}}::edge
 {"id": 1125899906842628, "label": "STACK_MATCH", "end_id": 844424930131971, "start_id": 844424930131970, "properties": {"title": "backend"}}::edge
 {"id": 1125899906842629, "label": "STACK_MATCH", "end_id": 844424930131969, "start_id": 844424930131971, "properties": {"title": "backend"}}::edge
 {"id": 1125899906842630, "label": "STACK_MATCH", "end_id": 844424930131970, "start_id": 844424930131971, "properties": {"title": "backend"}}::edge
(6 rows)

Result is only connection between backend nodes

(Demo through AGE-viewer)

Demo through AGE-viewer

Nodes output

FROM cypher('my_graph', $$
    MATCH (n) RETURN n                             
$$) as (a agtype);                                                      
                                                  a                                                  
-----------------------------------------------------------------------------------------------------
 {"id": 844424930131969, "label": "Person", "properties": {"vid": 3, "title": "backend"}}::vertex
 {"id": 844424930131970, "label": "Person", "properties": {"vid": 2, "title": "backend"}}::vertex
 {"id": 844424930131971, "label": "Person", "properties": {"vid": 1, "title": "backend"}}::vertex
 {"id": 844424930131972, "label": "Person", "properties": {"vid": 4, "title": "notbackend"}}::vertex
 {"id": 844424930131973, "label": "Person", "properties": {"vid": 5, "title": "notbackend"}}::vertex
 {"id": 844424930131974, "label": "Person", "properties": {"vid": 6, "title": "notbackend"}}::vertex
(6 rows)

Note that != is replaced with <> you can find that on the documentation of AGE. And vid is vertex id would be a user identified property

References: