How to filter data
You can use the methods where()
, where_not()
, or_where()
,
or_where_not()
, and_where()
, and_where_not()
, xor_where()
and
xor_where_not()
to construct queries that will filter data.
where(self, item: str, operator: Operator, **kwargs)
- Filter data so thatoperator
evaluates the comparison ofitem
andliteral
orexpression
to true.where_not(self, item: str, operator: Operator, **kwargs)
- Filter data so thatoperator
evaluates the comparison ofitem
andliteral
orexpression
to false.or_where(self, item: str, operator: Operator, **kwargs)
- Append an additional filter withOR
.or_not_where(self, item: str, operator: Operator, **kwargs)
- Append an additional filter withOR NOT
.and_where(self, item: str, operator: Operator, **kwargs)
- Append an additional filter withAND
.and_not_where(self, item: str, operator: Operator, **kwargs)
- Append an additional filter withAND NOT
.xor_where(self, item: str, operator: Operator, **kwargs)
- Append an additional filter withXOR
.xor_not_where(self, item: str, operator: Operator, **kwargs)
- Append an additional filter withXOR NOT
.
In this guide, you'll learn how to:
Filter data by property comparison
You can filter data by comparing properties of graph objects. Below you can see
how to compare name
properties of two nodes.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
query = Match()
.node(labels="Person", variable="p1")
.to(relationship_type="FRIENDS_WITH")
.node(labels="Person", variable="p2")
.where(item="p1.name", operator=Operator.EQUAL, expression="p2.name")
.return_()
.execute()
MATCH (p1:Person)-[:FRIENDS_WITH]->(p2:Person) WHERE p1.name = p2.name RETURN *;
Here the expression
keyword argument is used because the property shouldn't be
quoted in the Cypher query.
Standard boolean operators like NOT
, AND
, OR
and XOR
are used in the
Cypher query language. To have NOT
within WHERE
clause, you need to use
where_not()
method.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
query = Match()
.node(labels="Person", variable="p1")
.to(relationship_type="FRIENDS_WITH")
.node(labels="Person", variable="p2")
.where_not(item="p1.name", operator=Operator.EQUAL, expression="p2.name")
.return_()
.execute()
MATCH (p1:Person)-[:FRIENDS_WITH]->(p2:Person) WHERE NOT p1.name = p2.name RETURN *;
In a similar way, you can use AND
and AND NOT
clauses, which correspond to
the methods and_where()
and and_not_where()
. Using the query below you can
find all persons with the same first_name
and last_name
, but different
address
.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
query = Match()
.node(labels="Person", variable="p1")
.to(relationship_type="FRIENDS_WITH")
.node(labels="Person", variable="p2")
.where(item="p1.first_name", operator=Operator.EQUAL, expression="p2.first_name")
.and_where(item="p1.last_name", operator=Operator.EQUAL, expression="p2.last_name")
.and_not_where(item="p1.address", operator=Operator.EQUAL, expression="p2.address")
.return_()
.execute()
MATCH (p1:Person)-[:FRIENDS_WITH]->(p2:Person) WHERE p1.name = p2.name AND p1.last_name = p2.last_name AND NOT p1.address = p2.address RETURN *;
The same goes for the OR
, OR NOT
, XOR
and XOR NOT
clauses, which
correspond to the methods or_where()
, or_not_where()
, xor_where()
and
xor_not_where()
.
Filter data by property value
You can filter data by comparing the property of a graph object to some value (a
literal). Below you can see how to compare age
property of a node to the
integer.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
query = Match()
.node(labels="Person", variable="p")
.where(item="p.age", operator=">", literal=18)
.return_()
.execute()
MATCH (p:Person) WHERE p.age > 18 RETURN *;
It's important that the third keyword argument is literal
, since then, it will
be of the correct type.
Just like in property comparison, you can use different boolean operators to further filter the data.
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
query = Match()
.node(labels="Person", variable="p")
.where(item="p.age", operator=Operator.GREATER_THAN, literal=18)
.or_where(item="p.name", operator=Operator.EQUAL, literal="John")
.return_()
.execute()
MATCH (p:Person) WHERE p.age > 18 OR p.name = "John" RETURN *;
The literal
keyword is used again since you want John
to be quoted in the
Cypher query.
Filter data by label
Nodes can be filtered by their label using the WHERE
clause instead of
specifying it directly in the MATCH
clause. You have to use expression
as
the third keyword argument again since you don't want the quotes surrounding the
label in the Cypher clause.
Below you can see an example of how to filter data by label:
- GQLAlchemy
- Cypher
from gqlalchemy import Match
from gqlalchemy.query_builders.memgraph_query_builder import Operator
query = Match()
.node(variable="p")
.where(item="p", operator=Operator.LABEL_FILTER, expression="Person")
.return_()
.execute()
MATCH (p) WHERE p:Person RETURN *;
Just like in property comparison, you can use different boolean operators to further filter the data.