Filter Query Results
-
One of the most basic functionalities of querying is the ability to filter out data and return records that match a given condition.
-
Index queries can be executed using -
- query_index_type from the basic
session
API - RQL - Raven Query Language
- query_index_type from the basic
-
The examples in this page demonstrate how filtering is applied by each of these methods.
-
In this page:
whereEquals
- Where equalswhereGreaterThan
- Numeric propertywhereGreaterThan
- Nested propertywhereIn
- Single -vs- Multiple valuescontainsAny
- Any value from specified collectioncontainsAll
- All values from specified collectionwhereStartsWith
- All records with given prefixwhereEndsWith
- All records with given suffix- Where - Identifier Property
whereExists
- Where exists or doesn't exist
whereEquals
- Where equals
/** @var array<Employee> $results */
$results = $session
->query(Employee::class, Employees_ByFirstAndLastName::class) // query 'Employees/ByFirstAndLastName' index
->whereEquals("FirstName", "Robert")
->andAlso()
->whereEquals("LastName", "King") // filtering predicates
->toList(); // materialize query by sending it to server for processing
/** @var array<Employee> $results */
$results = $session
->advanced()
->documentQuery(Employee::class, Employees_ByFirstAndLastName::class) // query 'Employees/ByFirstAndLastName' index
->whereEquals("FirstName", "Robert") // filtering predicates
->andAlso() // by default OR is between each condition
->whereEquals("LastName", "King") // filtering predicates
->toList(); // materialize query by sending it to server for processing
class Employees_ByFirstAndLastName extends AbstractIndexCreationTask
{
public function __construct()
{
parent::__construct();
$this->map = "docs.Employees.Select(employee => new {" .
" FirstName = employee.FirstName," .
" LastName = employee.LastName" .
"})";
}
}
from index 'Employees/ByFirstAndLastName'
where FirstName = 'Robert' and LastName = 'King'
whereGreaterThan
- Numeric property
/** @var array<Product> $results */
$results = $session
->query(Product::class, Products_ByUnitsInStock::class) // query 'Products/ByUnitsInStock' index
->whereGreaterThan("UnitsInStock", 50) // filtering predicates
->toList(); // materialize query by sending it to server for processing
/** @var array<Product> $results */
$results = $session
->advanced()
->documentQuery(Product::class, Products_ByUnitsInStock::class) // query 'Products/ByUnitsInStock' index
->whereGreaterThan("UnitsInStock", 50) // filtering predicates
->toList(); // materialize query by sending it to server for processing
class Products_ByUnitsInStock extends AbstractIndexCreationTask
{
public function __construct()
{
parent::__construct();
$this->map = "docs.Products.Select(product => new {" .
" UnitsInStock = product.UnitsInStock" .
" })";
}
}
from index 'Products/ByUnitsInStock'
where UnitsInStock > 50
whereGreaterThan
- Nested property
// return all orders that were shipped to 'Albuquerque'
/** @var array<Order> $results */
$results = $session
->query(Order::class)
->whereEquals("ShipTo_City", "Albuquerque")
->toList();
// return all orders that were shipped to 'Albuquerque'
$results = $session
->advanced()
->documentQuery(Order::class)
->whereEquals("ShipTo_City", "Albuquerque")
->toList();
from Orders
where ShipTo.City = 'Albuquerque'
/** @var array<Order> $results */
$results = $session
->query(Order::class, Order_ByOrderLinesCount::class) // query 'Order/ByOrderLinesCount' index
->whereGreaterThan("Lines_Count", 50) // filtering predicates
->toList(); // materialize query by sending it to server for processing
/** @var array<Order> $results */
$results = $session
->advanced()
->documentQuery(Order::class, Order_ByOrderLinesCount::class) // query 'Order/ByOrderLinesCount' index
->whereGreaterThan("Lines_Count", 50) // filtering predicates
->toList(); // materialize query by sending it to server for processing
class Order_ByOrderLinesCount extends AbstractIndexCreationTask
{
public function __construct()
{
parent::__construct();
$this->map = "docs.Orders.Select(order => new {" .
" Lines_Count = order.Lines.Count" .
"})";
}
}
from index 'Order/ByOrderLinesCount'
where Lines.Count > 50
whereIn
- Single -vs- Multiple values
When you want to check a single value against multiple values, whereIn
can become handy.
To retrieve all employees where FirstName
is either Robert
or Nancy
, we can issue the following query:
/** @var array<Employee> $results */
$results = $session
->query(Employee::class, Employees_ByFirstAndLastName::class) // query 'Employees/ByFirstAndLastName' index
->whereIn("FirstName", ["Robert", "Nancy"]) // filtering predicates (remember to add `Raven.Client.Linq` namespace to usings)
->toList(); // materialize query by sending it to server for processing
/** @var array<Employee> $results */
$results = $session
->advanced()
->documentQuery(Employee::class, Employees_ByFirstAndLastName::class) // query 'Employees/ByFirstAndLastName' index
->whereIn("FirstName", [ "Robert", "Nancy" ]) // filtering predicates
->toList(); // materialize query by sending it to server for processing
class Employees_ByFirstAndLastName extends AbstractIndexCreationTask
{
public function __construct()
{
parent::__construct();
$this->map = "docs.Employees.Select(employee => new {" .
" FirstName = employee.FirstName," .
" LastName = employee.LastName" .
"})";
}
}
from index 'Employees/ByFirstAndLastName'
where FirstName IN ('Robert', 'Nancy')
containsAny
- Any value from specified collection
To check if enumeration contains any of the values from a specified collection,
use the containsAny
method.
For example, if you want to return all BlogPosts
that contain any of the specified Tags
:
/** @var array<BlogPost> $results */
$results = $session
->query(BlogPost::class, BlogPosts_ByTags::class) // query 'BlogPosts/ByTags' index
->containsAny("Tags", [ "Development", "Research" ]) // filtering predicates
->toList(); // materialize query by sending it to server for processing
/** @var array<BlogPost> $results */
$results = $session
->advanced()
->documentQuery(BlogPost::class, BlogPosts_ByTags::class) // query 'BlogPosts/ByTags' index
->containsAny("Tags", [ "Development", "Research" ]) // filtering predicates
->toList(); // materialize query by sending it to server for processing
class BlogPosts_ByTags extends AbstractIndexCreationTask
{
public function __construct()
{
parent::__construct();
$this->map = "docs.BlogPosts.Select(post => new {" .
" tags = post.tags" .
"})";
}
}
from index 'BlogPosts/ByTags'
where Tags IN ('Development', 'Research')
containsAll
- All values from specified collection
To check if an enumeration contains all of the values from a specified collection,
use the containsAll
method.
For example, if you want to return all the BlogPosts
that contain all of the specified Tags
:
/** @var array<BlogPost> $results */
$results = $session
->query(BlogPost::class, BlogPosts_ByTags::class) // query 'BlogPosts/ByTags' index
->containsAll("Tags", [ "Development", "Research" ]) // filtering predicates
->toList(); // materialize query by sending it to server for processing
/** @var array<BlogPost> $results */
$results = $session
->advanced()
->documentQuery(BlogPost::class, BlogPosts_ByTags::class) // query 'BlogPosts/ByTags' index
->containsAll("Tags", [ "Development", "Research" ]) // filtering predicates
->toList(); // materialize query by sending it to server for processing
class BlogPosts_ByTags extends AbstractIndexCreationTask
{
public function __construct()
{
parent::__construct();
$this->map = "docs.BlogPosts.Select(post => new {" .
" tags = post.tags" .
"})";
}
}
from index 'BlogPosts/ByTags'
where Tags ALL IN ('Development', 'Research')
whereStartsWith
- All records with given prefix
// return all products which name starts with 'ch'
/** @var array<Product> $results */
$results = $session
->query(Product::class)
->whereStartsWith("Name", "ch")
->toList();
// return all products which name starts with 'ch'
/** @var array<Product> $results */
$results = $session
->advanced()
->documentQuery(Product::class)
->whereStartsWith("Name", "ch")
->toList();
from Products
where startsWith(Name, 'ch')
whereEndsWith
- All records with given suffix
// return all products which name ends with 'ra'
/** @var array<Product> $results */
$results = $session
->query(Product::class)
->whereEndsWith("Name", "ra")
->toList();
// return all products which name ends with 'ra'
/** @var array<Product> $results */
$results = $session
->advanced()
->documentQuery(Product::class)
->whereEndsWith("Name", "ra")
->toList();
from Products
where endsWith(Name, 'ra')
Where - Identifier Property
Once a property used in the whereEquals
clause is recognized as an identity property of a given entity type
(according to FindIdentityProperty
convention)
and there aren't any other fields involved in the query, then it is called a "collection query".
Simple collection queries that ask about documents with given IDs or where identifiers start with a given prefix
and don't require any additional handling like ordering, full-text searching, etc, are handled directly by the storage engine.
It means that querying by ID doesn't create an auto-index and has no extra cost. In terms of efficiency, it is the same as
loading documents using session->load
.
/** @var Order $order */
$order = $session
->query(Order::class)
->whereEquals("Id", "orders/1-A")
->firstOrDefault();
/** @var Order $order */
$order = $session
->advanced()
->documentQuery(Order::class)
->whereEquals("Id", "orders/1-A")
->firstOrDefault();
from Orders
where id() = 'orders/1-A'
/** @var array<Order> $orders */
$orders = $session
->query(Order::class)
->whereStartsWith("Id", "orders/1")
->toList();
/** @var array<Order> $orders */
$orders = $session
->advanced()
->documentQuery(Order::class)
->whereStartsWith("Id", "orders/1")
->toList();
from Orders
where startsWith(id(), 'orders/1')
whereExists
- Where exists or doesn't exist
To find all documents in a collection that have a specified field, see How to Filter by Field Presence.
To find all documents in a collection that don't have a specified field, see How to Filter by Non-Existing Field.