You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
:description: This page describes filtering operators.
5
5
6
-
When querying for data, a number of operators are available for different types in the `where` argument of a query or mutation.
6
+
When querying for data, a number of operators are available for the types in the `where` argument of a query or mutation, allowing you to filter query results or specify the set of objects a mutation applies to.
7
7
8
-
== Equality operators
8
+
Operators can either be standalone operators (see xref:#_boolean_operators[]) or they are appended to field names (for example, xref:/queries-aggregations/filtering.adoc#_string_comparison[]).
9
+
10
+
All operators can be combined using the Boolean operators `AND`, `OR`, and `NOT`.
11
+
12
+
== Operators
13
+
14
+
=== Boolean operators
15
+
16
+
As standalone operators, Boolean operators accept an array argument with items of the same format as the `where` argument.
17
+
This way, they can be nested to form complex Boolean expressions.
18
+
19
+
For example, if you want to match all actors either by the name of "Keanu" or not belonging to the "Pantoliano" family, who played in "The Matrix" movie, here is how you query that:
20
+
21
+
[source, graphql, indent=0]
22
+
----
23
+
query {
24
+
actors(where: {
25
+
AND: [
26
+
{
27
+
OR: [
28
+
{ name_CONTAINS: "Keanu" },
29
+
{ NOT: { name_ENDS_WITH: "Pantoliano" } }
30
+
]
31
+
},
32
+
{
33
+
movies_SOME: { title: "The Matrix" }
34
+
}
35
+
]}
36
+
) {
37
+
name
38
+
movies {
39
+
title
40
+
}
41
+
}
42
+
}
43
+
----
44
+
45
+
`name_CONTAINS` and `name_ENDS_WITH` are xref:/queries-aggregations/filtering.adoc#_string_comparison[String comparisons] while `movies_SOME` is a xref:/queries-aggregations/filtering.adoc#_relationship_filtering[relationship filter].
46
+
47
+
=== Equality operators
48
+
49
+
All types can be tested for equality or non-equality.
9
50
10
-
All types can be tested for either equality.
11
-
For non-equality, you *must* use the xref:/queries-aggregations/filtering.adoc#_logical_operators[`NOT`] logical operator.
12
51
For example:
13
52
14
53
.Filtering all users named John
@@ -21,12 +60,24 @@ query {
21
60
}
22
61
----
23
62
63
+
For non-equality, you must use the xref:/queries-aggregations/filtering.adoc#_boolean_operators[`NOT`] logical operator.
64
+
65
+
.Filtering all users which are not named John
66
+
[source, graphql, indent=0]
67
+
----
68
+
query {
69
+
users(where: { NOT: {name: "John" }})
70
+
id
71
+
name
72
+
}
73
+
----
74
+
24
75
[NOTE]
25
76
====
26
77
For the `Boolean` type, equality operators are the only ones available.
27
78
====
28
79
29
-
== Numerical operators
80
+
=== Numerical operators
30
81
31
82
These are the operators available for numeric (`Int`, `Float`, xref::/types/scalar.adoc[`BigInt`]), xref::/types/temporal.adoc[temporal] and xref::/types/spatial.adoc[spatial] types:
32
83
@@ -52,43 +103,72 @@ query {
52
103
Spatial types use numerical filtering differently and they also have additional options.
53
104
See xref:filtering.adoc#_filtering_spatial_types[Filtering spatial types] for more information.
54
105
55
-
== Logical operators
106
+
==== Spatial type filtering
56
107
57
-
All operators can be combined using the logical operators `AND`, `OR`, and `NOT`.
58
-
They can also be standalone operators, which means that they can be used as such and not be appended to field names.
108
+
Both the `Point` and the `CartesianPoint` types use xref::queries-aggregations/filtering.adoc#_numerical_operators[numerical operators] and have an additional `_DISTANCE` filter.
109
+
Here is a list of what each filter does for the two types:
59
110
60
-
These operators accept an array argument with items of the same format as the `where` argument, which means they can also be nested to form complex combinations.
111
+
* `_LT`: checks if a point is less than the distance in the `distance` field away (in meters) from the point specified by the `point` field.
112
+
* `_LTE`: checks if a point is less than or equal to the distance in the `distance` field away (in meters) from the point specified by the `point` field.
113
+
* `_DISTANCE`: checks if a point is the exact distance in the `distance` field away (in meters) from the point specified by the `point` field.
114
+
* `_GT`: checks if a point is greater than the distance in the `distance` field away (in meters) from the point specified by the `point` field.
115
+
* `_GTE`: checks if a point is greater than or equal to the distance in the `distance` field away (in meters) from the point specified by the `point` field.
61
116
62
-
For example, if you want to match all actors by the name of either "Keanu" or not belonging to the "Pantoliano" family, that played in "The Matrix" movie, here is how you can query that:
117
+
For a `Point` type, all filters take the following type as an argument:
63
118
64
119
[source, graphql, indent=0]
65
120
----
66
-
query {
67
-
actors(where: {
68
-
AND: [
69
-
{
70
-
OR: [
71
-
{ name_CONTAINS: "Keanu" },
72
-
{ NOT: { name_ENDS_WITH: "Pantoliano" } }
73
-
]
74
-
},
75
-
{
76
-
movies_SOME: { title: "The Matrix" }
77
-
}
78
-
]}
79
-
) {
121
+
input PointDistance {
122
+
point: Point!
123
+
distance: Float!
124
+
}
125
+
----
126
+
127
+
In practice, you can construct queries like the following, which finds all users within a 5km (5000m) radius of a `Point`:
The following case-sensitive comparison operators are available for `String` and `ID` types:
92
172
93
173
* `_STARTS_WITH`
94
174
* `_ENDS_WITH`
@@ -141,16 +221,17 @@ const features = {
141
221
const neoSchema = new Neo4jGraphQL({ features, typeDefs, driver });
142
222
----
143
223
144
-
== RegEx matching
224
+
=== RegEx matching
145
225
146
-
The filter `_MATCHES` is also available for comparison of `String` and `ID` types.
226
+
The filter `_MATCHES` is available for comparison of `String` and `ID` types.
147
227
It accepts RegEx strings as an argument and returns any matches.
148
228
149
-
150
-
Note that RegEx matching filters are **disabled by default**.
229
+
Note that RegEx matching filters are disabled by default.
151
230
This is because, on an unprotected API, they could potentially be used to execute a https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS[ReDoS attack^] against the backing Neo4j database.
152
231
153
-
If you want to enable them, set the features configuration object for each:
232
+
If you want to enable RegEx matching, update the `features` configuration object.
const neoSchema = new Neo4jGraphQL({ features, typeDefs, driver });
201
281
----
202
282
203
-
== Array comparison
204
-
205
-
The following operator is available on non-array fields, and accepts an array argument:
206
-
207
-
* `_IN`
208
-
209
-
Conversely, the following operator is available on array fields, and accepts a single argument:
210
-
211
-
* `_INCLUDES`
212
-
213
-
These operators are available for all types apart from `Boolean`.
214
-
215
-
== Filtering spatial types
283
+
=== Array comparison
216
284
217
-
Both the `Point` and the `CartesianPoint` types use xref::queries-aggregations/filtering.adoc#_numerical_operators[numerical operators] and have an additional `_DISTANCE` filter.
218
-
Here is a list of what each filter does for the two types:
219
-
220
-
* `_LT`: checks if a point is less than the distance in the `distance` field away (in meters) from the point specified by the `point` field.
221
-
* `_LTE`: checks if a point is less than or equal to the distance in the `distance` field away (in meters) from the point specified by the `point` field.
222
-
* `_DISTANCE`: checks if a point is the exact distance in the `distance` field away (in meters) from the point specified by the `point` field.
223
-
* `_GT`: checks if a point is greater than the distance in the `distance` field away (in meters) from the point specified by the `point` field.
224
-
* `_GTE`: checks if a point is greater than or equal to the distance in the `distance` field away (in meters) from the point specified by the `point` field.
225
-
226
-
For a `Point` type, all filters take the following type as an argument:
The query returns all movies which have "Action" as one of their genres.
331
+
332
+
`_IN` and `_INCLUDES` are available for all types except `Boolean`.
333
+
334
+
== Interface filtering
277
335
278
336
You can use the `typename_IN` filter to filter interfaces.
279
337
Refer to xref:types/interfaces.adoc#type-definitions-interfaced-types-querying[Type definitions -> Type -> Interface] for more details and an example.
280
338
281
339
== Relationship filtering
282
340
283
-
Relationship filtering depends on the type of relationship that you have:
341
+
Relationship filtering depends on the type of relationship:
284
342
285
-
* `n..1`: filtering done on equality or inequality of the related nodes by specifying a filter on `field`.
286
-
* `n..m`: filtering is done on the list of related nodes and is based on the https://neo4j.com/docs/cypher-manual/current/functions/predicate/[list predicates] available in Cypher:
343
+
* `n..1`: the filtering is done on equality or inequality of the related nodes by specifying a filter on `field`.
344
+
* `n..m`: the filtering is done on the list of related nodes and is based on the https://neo4j.com/docs/cypher-manual/current/functions/predicate/[list predicates] available in Cypher:
An `author` represents an `n..1` relationship on `Post`, where a given `Post` is authored by one, and only one, `author`.
312
-
The available filters here will be `author`.
370
+
In the type definitions example, an `author` represents an `n..1` relationship on `Post`, where a given `Post` is authored by one, and only one, `author`.
371
+
The available filter is `author`.
372
+
313
373
For example:
314
374
315
375
.Find all posts by a desired author
@@ -334,8 +394,9 @@ query {
334
394
335
395
=== `n..m` relationships
336
396
337
-
In the previous example, `posts` represents a `n..m` relationship on `User`, where a given `User` can have any number of `posts`.
338
-
Here are some query examples:
397
+
In the type definitions example, `posts` represents an `n..m` relationship on `User`, where a given `User` can have any number of `posts`.
398
+
399
+
For example:
339
400
340
401
.Find all users where all of their posts contain search term: `"neo4j"`
341
402
[source, graphql, indent=0]
@@ -379,8 +440,8 @@ query {
379
440
380
441
== Aggregation filtering
381
442
382
-
This library offers, for each relationship, an aggregation key inside the `where` argument.
383
-
It can be used both on the `node` and `edge` of a relationship.
443
+
The Neo4j GraphQL library offersan aggregation key inside the `where` argument of each relationship.
444
+
You can use it both on the `node` and `edge` of a relationship.
384
445
385
446
Here are some examples on how to apply this kind of filtering:
386
447
@@ -464,7 +525,7 @@ query {
464
525
}
465
526
----
466
527
467
-
=== Operators
528
+
=== With operators
468
529
469
530
Aggregation filtering can also be done with operators.
470
531
They provide autogenerated filters available for each type on the `node` and `edge` of the specified relationship.
0 commit comments