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:
where_equals
- Where equalswhere_greater_than
- Numeric propertywhere_greater_than
- Nested propertywhere_in
- Single -vs- Multiple valuescontains_any
- Any value from specified collectioncontains_all
- All values from specified collectionwhere_starts_with
- All records with given prefixwhere_ends_with
- All records with given suffix- Where - Identifier Property
where_exists
- Where exists or doesn't exist
where_equals
- Where equals
results = list( # Materialize query by sending it to server for processing
session.query_index_type(
Employees_ByFirstAndLastName, Employee
) # query 'Employees/ByFirstAndLastName' index
.where_equals("FirstName", "Robert") # filtering predicates
.and_also()
.where_equals("LastName", "King")
)
class Employees_ByFirstAndLastName(AbstractIndexCreationTask):
def __init__(self):
super().__init__()
self.map = "from e in docs.Employees select new {FirstName = e.FirstName, LastName = e.LastName}"
from index 'Employees/ByFirstAndLastName'
where FirstName = 'Robert' and LastName = 'King'
where_greater_than
- Numeric property
results = list( # Materialize query by sending it to server for processing
session.query_index_type(
Products_ByUnitsInStock, Product # query 'Products/ByUnitsInStock' index
).where_greater_than(
"UnitsInStock", 50
) # filtering predicates
)
class Products_ByUnitsInStock(AbstractIndexCreationTask):
def __init__(self):
super().__init__()
self.map = "from p in docs.Products select new {p.UnitsInStock}"
from index 'Products/ByUnitsInStock'
where UnitsInStock > 50
where_greater_than
- Nested property
# return all orders that were shipped to 'Albuquerque'
results = list(session.query(object_type=Order).where_equals("ShipTo.City", "Albuquerque"))
from Orders
where ShipTo.City = 'Albuquerque'
results = list( # Materialize query by sending it to server for processing
session.query_index_type(
Order_ByOrderLinesCount, Order # query 'Orders/ByOrderLinesCount' index
).where_greater_than(
"Lines.Count", 50
) # filtering predicates
)
class Order_ByOrderLinesCount(AbstractIndexCreationTask):
def __init__(self):
super().__init__()
self.map = "from o in docs.Orders select new {Lines_Count = order.Lines.Count}"
from index 'Order/ByOrderLinesCount'
where Lines.Count > 50
where_in
- Single -vs- Multiple values
When you want to check a single value against multiple values, where_in
can become handy.
To retrieve all employees where FirstName
is either Robert
or Nancy
, we can issue the following query:
results = list( # Materialize query by sending it to server for processing
session.query_index_type(
Employees_ByFirstAndLastName, Employee # query 'Employees/ByFirstAndLastName' index
).where_in(
"FirstName", ["Robert", "Nancy"]
) # filtering predicates
)
class Employees_ByFirstAndLastName(AbstractIndexCreationTask):
def __init__(self):
super().__init__()
self.map = "from e in docs.Employees select new {FirstName = e.FirstName, LastName = e.LastName}"
from index 'Employees/ByFirstAndLastName'
where FirstName IN ('Robert', 'Nancy')
contains_any
- Any value from specified collection
To check if enumeration contains any of the values from a specified collection,
use the contains_any
method.
For example, if you want to return all BlogPosts
that contain any of the specified Tags
:
results = list( # Materialize query by sending it to server for processing
session.query_index_type(BlogPosts_ByTags, BlogPost).contains_any( # query 'BlogPosts/ByTags' index
"Tags", ["Development", "Research"]
) # filtering predicates
)
class BlogPosts_ByTags(AbstractIndexCreationTask):
def __init__(self):
super().__init__()
self.map = "from post in posts select new {post.Tags}"
from index 'BlogPosts/ByTags'
where Tags IN ('Development', 'Research')
contains_all
- All values from specified collection
To check if an enumeration contains all of the values from a specified collection,
use the contains_all
method.
For example, if you want to return all the BlogPosts
that contain all of the specified Tags
:
results = list( # Materialize query by sending it to server for processing
session.query_index_type(BlogPosts_ByTags, BlogPost).contains_all( # query 'BlogPosts/ByTags' index
"Tags", ["Development", "Research"]
) # filtering predicates
)
class BlogPosts_ByTags(AbstractIndexCreationTask):
def __init__(self):
super().__init__()
self.map = "from post in posts select new {post.Tags}"
from index 'BlogPosts/ByTags'
where Tags ALL IN ('Development', 'Research')
where_starts_with
- All records with given prefix
# return all products which name starts with 'ch'
results = list(session.query(object_type=Product).where_starts_with("Name", "ch"))
from Products
where startsWith(Name, 'ch')
where_ends_with
- All records with given suffix
results = list(session.query(object_type=Product).where_ends_with("Name", "ra"))
from Products
where endsWith(Name, 'ra')
Where - Identifier Property
Once a property used in the where_equals
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
.
order = session.query(object_type=Order).where_equals("Id", "orders/1-A").first()
from Orders
where id() = 'orders/1-A'
orders = list(session.query(object_type=Order).where_starts_with("Id", "orders/1"))
from Orders
where startsWith(id(), 'orders/1')
where_exists
- 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.