Queries
How to Query Content with Filters, Sorting and Pagination
Last updated
How to Query Content with Filters, Sorting and Pagination
Last updated
This documentation is based on the FoodCrunch use case. Please open the link below alongside this page to understand the examples.
Squidex has a query engine that allows different query languages. So far, the following query languages are available:
OData Queries - this is the first system implemented using an existing solution that was easy to adapt, these are easier to write in URLs.
JSON Queries are newer and are mainly used for the UI, because they are faster and easier to determine. It is best to use JSON- queries for your client.
Both query languages support the same features:
Filters with complex comparison operators, conjunctions and negations.
Full text search.
Sorting by one or multiple fields.
Skipping items and restricting the size of the result set for pagination.
OData is an open protocol which allows the creation and consumption of queryable and inoperable APIs in a simple, standardized way. It was designed and developed by Microsoft and provides ready-to use-solutions. We have decided to use the Query syntax because we wanted to leverage an existing system and parser and we find it easy to adapt to our needs.
The queries are provided over the URL and have a special syntax. OData query options start with a dollar character, e.g. $filter
.
Here's an example:
Even though we use OData, we do not support the full capabilities and there are no plans to do so in future. Some features like select, expand or formatting can be handled better with GraphQL.
The full OData convention can be read at:
JSON queries are passed in as URL encoded JSON objects with the q
query parameter. They are much harder to read for humans, but easier and faster to parse. This was introduced when a new query editor was implemented for the Management UI.
Here's an example:
As you can see it is horrible to read, therefore we will just show normal JSON examples from now on.
Let's demonstrate the API concepts based on our FoodCrunch App use case. The app has two schemas, we will consider the startups
schema which contains a database of startups in the food space.
The schema has the following fields:
slug
String
No
A single slug for Google friendly URLs.
name
String
No
The name of the startup.
description
String
Yes
The description of the startup.
stage
String
No
Current startup stage
funding
Number
No
The total funding in Millions (USD).
founded
Number
No
Year when the startup was founded.
founders
Array
No
The founders as list of name and position.
tags
Tags
No
A list of tags for search.
location
Geolocation
No
The geolocation of the headquarter.
metadata
JSON
No
Unstructured metadata.
givenUp
Boolean
No
Indicates whether the startup has given up.
Then, your content will have the following structure in the API:
Please note, that there is one object for each field because each field has a partitioning. This defines how the field is structured. The most simple partitioning is the invariant partition, which only allows a single key iv
.
If the field is localizable
, use the languages codes from the languages that you defined in your App settings as keys.
Read more about localization here:
To identify the field of our content item, use the full path to this field, separated by hashes.
For example:
id
createdBy
data/name/iv
data/description/en
data/description/de
data/founded/iv
When you use JSON queries, you can also use the dot-notation to create a syntax that is closer to Javascript and other programming languages. It is best to use this notation.
For example:
data.name.iv
In OData dash characters (-) are not allowed. Therefore, you should replace them with underscore in your queries.
For example, if there was a field called acquired-by
we would use:
data/acquired_by/iv
in OData
data.acquired-by.iv
in JSON
The examples here used the startups
schema of the FoodCrunch use case.
The top
/ take
query option requests the number of items in the queried collection to be included in the result. The default value is 20 and the maximum allowed value is 200.
Because of an error the parameter is called top in OData and take in JSON.
The skip
query option requests the number of items in the queried collection to be skipped and not included in the result. Use this together with top
/ take
to read all your data page by page.
Example of skip
combined with top
/ take
:
You can get random items using the random
option:
The random operator picks elements from the result set (not from the entire database).
For example, this query returns 5 random items from the first 200 elements with the default order:
The search
query option allows clients to request entities matching a free-text search expression. We add the data of all fields for all keys to a single field in the database and use this combined field to implement the full text search.
You can either use search or filter but not both.
The filter
system query option allows clients to filter a collection of resources that are addressed by a request URL.
For example, find all the startups in the Seed stage.
For example, find all the startups with a funding of more than 100 million USD.
For example, find all the startups with a funding of less than 10 million USD.
If you have fields that have an array of values, for example, references that are represented as an array of content IDs, you can still use the equal operator. The API will return a content item if at least one item in the array is equal to the passed in value.
An example of filtering by tags:
You can either use search or filter but not both.
Example demonstrating an array (components, array fields, references, assets, strings) cannot be empty:
Example demonstrating a date must match value:
Example demonstrating a date must match one of many values:
Example demonstrating an ID must match value:
Example demonstrating a name must match string value:
Example demonstrating a boolean must match value:
Example demonstrating a number must match a value:
Examples of string property demonstrating startswith
, endswith
or contains
:
contains, startsWith and endsWith are always case insensitive.
Examples of string property matching a regex pattern:
Examples of using operators with false / negation In OData these operators can also be compared with false. In JSON queries you must use a not operation to negate your filter expression.
In OData single quotes ('
) in text values must be replaced with double single quotes.
Example of Geolocation within radius:
Examples of various conditions
Examples of combining conditions:
Examples of negations
The orderby
or sorting
query option allows clients to request resources in a particular order.
For example, find the top 20 most funded startups:
You can also sort by multiple fields.
By default, the content API only returns published content. You can also use the X-Unpublished
header to return draft content.
The API tracks the version of each content element and provides this information in the ETag
content header if you create an update (POST, PUT, PATCH) or if you request a single resource. If you request multiple resources, the version is provided as a field to each entry.
You can use this header for two use cases:
When you create an update, you get the new version. This information can be used to find out if your change has already been written to the read store when you receive the same resource following your update.
When you create an update, you can use the If-Match
header to pass the expected version to the API. If the version does not match the version in the database, this means that another user or client has changed the same resource. In which case, the 412 (Precondition Failed)
status code is returned. You should provide this information to the user and ask if the user wants to reload the data or if the resource should be overwritten (but don't use the If-Match
header for the second request).
Read more about the If-Match
header at: