Graph Queries Filtering
You can use RQL keywords and operators within a graph query node/edge clause, to narrow query results down.
where
can be used within data node and edge clauses, to condition the query.select
can be used within edge clauses, to choose wanted details of filtered results .- Operators like
and
andor
can complementwhere
within data-node and edge clauses.
(You can place these operators between query sections as well. Learn more about it here: Multi-Section Search Patterns)
Sample queries included in this article use only data that is available in the
Northwind sample database,
so you may easily try them out.
Filtering
You can filter data within data-node and edge clauses, using RQL.
Using where
To Filter Results
where
can be used within both data-node and edge clauses.
Here, for example, we retrieve only big orders from the Orders collection:
match
(Orders as orders
where Freight > 5)
Using select
To Choose Filtered Results
select
can be used alongside where
, within an edge clause, to select details of filtered data.
In the following example, we're looking for paths between orders and products with a discount:
match
(Orders as orders)-
[Lines as cheap
where Discount >= 0.25
select Product]->
(Products as products)
-
Query Flow:
- The edge clause uses
where
to search each order's Lines construct.
Only edges of products with a high-enough discount will be used. - Then,
select
is used to pick the actual edge - the ID of a product profile.
- The edge clause uses
You cannot use select
within a data-node clause.
Note: Do not confuse the current usage of select
with the implementation
of the same keyword to project data.
Using where
With or
The following query uses where
along with or
, to find products of two different categories.
match
(Products as products)-
[Category as category]->
(Categories as categories
where Name="Confections"
or Name="Condiments")
with {from Products} as products
with edges (Category) as category
with {from Categories
where Name="Confections"
or Name="Condiments"} as categories
match
(products)-
[category]->
(categories)
Retrieved using OR
Using where
With and
The following query uses where
along with and
, to find products in a chosen price range.
match
(Orders as orders) -
[Lines as lines
select Product]->
(Products as products
where (PricePerUnit < 20
and PricePerUnit > 15))
select orders.Company as company, products.Name as name
- Filtering the products (the destination nodes) also determine which orders (the origin nodes) would be included in the results.
Retrieved using AND: Graphical View
Retrieved using AND: Textual View
Additional Filtering Examples
Let's look at a few more examples to filtering within query clauses.
-
Here, we query the relations between orders, products and suppliers.
We look for products whose discount is low and price is high, because we want to renegotiate such deals.
We limit the results to suppliers whose contact is also the owner, so we'd have who to negotiate with.
match (Orders as orders where (Lines.PricePerUnit > 50 and Lines.Discount < 0.10))- [Lines as negotiateThisProduct select Product]-> (Products as products)- [Supplier as contactIsOwner]-> (Suppliers as salesrep where Contact.Title = "Owner") select id(orders) as Pricy, products.QuantityPerUnit as Negotiate, salesrep.Name as Supplier, salesrep.Contact.Name as Contact
Locate Negotiable Products: Graphical View
Locate Negotiable Products: Textual View
-
Here is an example for the gradual development of a modular query, and filters included in it.
- First, we limit the retrieval of orders to ones ordered from Brazil.
match (Orders as orders where (ShipTo.Country = "Brazil"))- [Lines as lines select Product]-> (Products as products)
Filtering Nodes
- Then we add another data layer by including Suppliers in our query,
and create another filter to include in the results only french suppliers.
match (Orders as orders where (ShipTo.Country = "Brazil"))- [Lines as lines select Product]-> (Products as products)- [Supplier as supplier]-> (Suppliers as suppliers where Address.Country = "France")
- First, we limit the retrieval of orders to ones ordered from Brazil.
Filtering Nodes