Search code examples
postgresqlcypherapache-ageopencypher

Why doesn't the != operator work in my query in AGE?


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?


Solution

  • 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: