Search code examples
memgraphdb

Equivalence between nested and or in a cypher query vs python gqlalchemy query


When querying the same data via memgraphlab with the following query:

MATCH p =(m:ProjLibMainNode)<-[:PROPERTIES_OF]-(n {PropStatus:"Current"})-[:REFERENCES]->(q)
WHERE n.ReviewStatus = "Approved" OR n.ReviewStatus = "Re-Opened"
RETURN n.Name, n.PropStatus, n.ReviewStatus

Results in 590 lines being returned

When trying to setup the equivalent query in python using gqlalchemy on the same data:

QGchecklist = list(
    Match()
    .node(variable="m", labels="ProjLibMainNode")
    .from_(relationship_type="PROPERTIES_OF", directed=True)
    .node(variable="n", labels="ProjLibPropNode")
    .to(relationship_type="REFERENCES", directed=True)
    .node(variable="q")
    .where(item="n.PropStatus", operator=Operator.EQUAL, literal="Current")
    .and_where(item="n.ReviewStatus", operator=Operator.EQUAL, literal="Approved")
    .or_where(item="n.ReviewStatus", operator=Operator.EQUAL, literal="Re-Opened")
    .return_(["n.Name, n.PropStatus", "n.ReviewStatus"])
    .execute()
    )

I get list with a size of 597, ie 598 rows.

The cypher looks like a nested: AND(Current(OR(Approved,Re-Opened))) Whereas the GQAlchemy python query looks more like a AND(Current,Approved)OR(Re-Opened)

How do I correctly nest the AND OR in GQLAlchemy python to match the cypher?


Solution

  • The queries are a bit different since you do not match them based on the same properties; instead, you filter them in the expression.

    To fix this, you can also add a property during node matching. For example, if I have two nodes in my DB:

    CREATE (:Test{id:1, name:"foo"})
    CREATE (:Test{id:2, name:"bar"})
    

    and want just foo nodes to pass on into the query pipeline, you can do it like this:

    nodes = list(
        Match().node(labels="Test", name="foo", variable="n").return_("n").execute()
    )
    

    Notice the name property here is **kwarg: https://memgraph.github.io/gqlalchemy/reference/gqlalchemy/query_builders/declarative_base/#node

    Hence, you can use them in the matching process.