Filtering with WHERE
Filter elements based on specific conditions.
:: WHERE (< condition >)
:: WHERE ( _ :: { property } :: COMPARISON ( value ))
The condition in the WHERE step must evaluate to a boolean value. If the condition is not met, the element will be filtered out from the results.
When using the SDKs or curling the endpoint, the query name must match what is defined in the queries.hx
file exactly.
Comparison Operations
The following operations can be used to compare values.
EQ
and NEQ
can be used to compare strings, booleans, and numbers.
GT
, GTE
, LT
, and LTE
can only be used to compare numbers.
String, Boolean, and Number Operations
Operation Description Example ::EQ(value)
Equals ::WHERE(_::{status}::EQ("active"))
::NEQ(value)
Not equals ::WHERE(_::{age}::NEQ(25))
Number Operations
Operation Description Example ::GT(value)
Greater than ::WHERE(_::{age}::GT(25))
::LT(value)
Less than ::WHERE(_::{age}::LT(30))
::GTE(value)
Greater than or equal ::WHERE(_::{rating}::GTE(4.5))
::LTE(value)
Less than or equal ::WHERE(_::{priority}::LTE(2))
Example 1: Basic filtering with WHERE
QUERY GetAdultUsers () =>
adult_users <- N < User > :: WHERE ( _ :: { age } :: GT ( 18 ))
RETURN adult_users
QUERY CreateUser ( name : String , age : U8 , email : String ) =>
user <- AddN < User >({
name : name ,
age : age ,
email : email
})
RETURN user
See all 11 lines
Here’s how to run the query using the SDKs or curl
Python
Rust
Go
TypeScript
Curl
from helix.client import Client
client = Client( local = True , port = 6969 )
users = [
{ "name" : "Alice" , "age" : 25 , "email" : "alice@example.com" },
{ "name" : "Bob" , "age" : 16 , "email" : "bob@example.com" },
{ "name" : "Charlie" , "age" : 30 , "email" : "charlie@example.com" },
{ "name" : "Diana" , "age" : 17 , "email" : "diana@example.com" },
]
for user in users:
client.query( "CreateUser" , user)
result = client.query( "GetAdultUsers" , {})
print (result)
See all 16 lines
Example 2: String and equality filtering
QUERY GetActiveUsers ( status : String ) =>
active_users <- N < User > :: WHERE ( _ :: { status } :: EQ ( status ))
RETURN active_users
QUERY CreateUser ( name : String , age : U8 , email : String , status : String ) =>
user <- AddN < User >({
name : name ,
age : age ,
email : email ,
status : status
})
RETURN user
See all 12 lines
Here’s how to run the query using the SDKs or curl
Python
Rust
Go
TypeScript
Curl
from helix.client import Client
client = Client( local = True , port = 6969 )
users = [
{ "name" : "Alice" , "age" : 25 , "email" : "alice@example.com" , "status" : "active" },
{ "name" : "Bob" , "age" : 30 , "email" : "bob@example.com" , "status" : "inactive" },
{ "name" : "Charlie" , "age" : 28 , "email" : "charlie@example.com" , "status" : "active" },
{ "name" : "Diana" , "age" : 22 , "email" : "diana@example.com" , "status" : "pending" },
]
for user in users:
client.query( "CreateUser" , user)
result = client.query( "GetActiveUsers" , { "status" : "active" })
print (result)
See all 16 lines
Filter by Relationships with EXISTS
Returns true if a traversal has any results. Otherwise, it returns false.
Example 1: Using EXISTS
for relationship filtering
QUERY GetUsersWithFollowers () =>
users <- N < User > :: WHERE ( EXISTS ( _ :: In < Follows >))
RETURN users
QUERY CreateUser ( name : String , age : U8 , email : String ) =>
user <- AddN < User >({
name : name ,
age : age ,
email : email
})
RETURN user
QUERY CreateFollow ( follower_id : ID , following_id : ID ) =>
follower <- N < User >( follower_id )
following <- N < User >( following_id )
AddE < Follows > :: From ( follower ) :: To ( following )
RETURN "Success"
See all 17 lines
Here’s how to run the query using the SDKs or curl
Python
Rust
Go
TypeScript
Curl
from helix.client import Client
client = Client( local = True , port = 6969 )
users = [
{ "name" : "Alice" , "age" : 25 , "email" : "alice@example.com" },
{ "name" : "Bob" , "age" : 30 , "email" : "bob@example.com" },
{ "name" : "Charlie" , "age" : 28 , "email" : "charlie@example.com" },
]
user_ids = []
for user in users:
result = client.query( "CreateUser" , user)
user_ids.append(result[ 0 ][ "user" ][ "id" ])
client.query( "CreateFollow" , { "follower_id" : user_ids[ 1 ], "following_id" : user_ids[ 0 ]})
client.query( "CreateFollow" , { "follower_id" : user_ids[ 2 ], "following_id" : user_ids[ 0 ]})
result = client.query( "GetUsersWithFollowers" , {})
print (result)
See all 20 lines