Where
Describes the where parameter, part of the GraphQL API used for the Optimizely querying service, when retrieving content in Optimizely solutions.
The where
parameter specifies the filter condition for the result returned by the query. It is used to filter and search on values in fields to reduce or increase the results retrieved by Optimizely Content Graph.
where: [{filter_condition}]
The filter_condition is defined like this:
predicate: {
filter_operators: expression
}
Definitions
- {filter_condition} – Defines the conditions to be met for the result to be returned. There is no limit to the number of predicates that can be included in a filter_condition.
- predicate – A field name.
- filter_operators – Matching functions (called "operators") that operate specifically on string, number, date, and Boolean values, and operator options like
boost
andsynonyms
that work together with the operators. - expression – A string, numeric, enumerated, or Boolean value.
Note
Optimizely supports using _modified extend argument to query contents (Whenever contents have been synced to our service, that contents would be marked with _modified extend field equal now())
filter_operators | Description | String type | SearchableString type | Number type | Date type | Bool type |
---|---|---|---|---|---|---|
eq | Used to test equality between two literal expressions, can be case-insensitive. | Yes | Yes | Yes | Yes | No |
noteq | Used to test the condition of two literal expressions not being equal to each other, can be case-insensitive. | Yes | Yes | Yes | Yes | No |
like | Determines whether a specific character string matches a specified pattern. | Yes | Yes | No | No | No |
contains | Filter fields that contains character-based data for precise or less precise matches to single words and phrases. | No | Yes | No | No | No |
gt | Used to test the condition of one expression being greater than another. | No | No | Yes | Yes | No |
gte | Used to test the condition of one expression being greater than or equal to another. | No | No | Yes | Yes | No |
lt | Used to test the condition of one expression being less than another. | No | No | Yes | Yes | No |
lte | Used to test the condition of one expression being less than or equal to another. | No | No | Yes | Yes | No |
exist | Determines whether a specific expression exists or not. | Yes | Yes | Yes | No | Yes |
startsWith | Determines whether a specific expression starts with a specific pattern. | Yes | Yes | No | No | No |
endsWith | Determines whether a specific expression ends with a specific pattern. | Yes | No | No | No | No |
in | Determines whether a specific literal expression is in an array. | Yes | Yes | Yes | No | No |
notIn | Determines whether a specific literal expression is not in an array. | Yes | Yes | Yes | No | No |
boost | Boost a {filter_condition} by a number (cannot be a negative number), so certain results can be ranked higher when sorting by RELEVANCE . | Yes | Yes | Yes | Yes | Yes |
synonyms | Enables the expansion of the expression with synonyms stored in the system. | Yes | Yes | No | No | No |
Examples
The following examples show how to use some common filter conditions in where clause.
Where with eq operators
{
BiographyPage(where: {
Name: {
eq: "Alan Turing"
}
}, locale: [en]) {
Name
Language {
Name
DisplayName
}
}
}
Query with dates
Querying for date values is flexible. We can query for exact date values, which are indexed in the ISO 8601 standard. You can query for an exact date value.
{
BiographyPage(where: { Born: { eq: "1983-11-14T00:00:00Z" } }) {
items {
Name
Born
}
total
}
}
You can write this date query in a different format, but then you need to specify the timezone UTC, or else the date value is converted to a date value from local system, resulting in no matches.
{
BiographyPage(where: { Born: { eq: "11/14/1983 00:00:00 AM UTC" } }) {
items {
Name
Born
}
total
}
}
The power of date queries is that you can query for results between time ranges, with an optional time. This query returns results where the value for Born
is equal or greater than "1947-07-30"
.
{
BiographyPage(where: { Born: { gte: "1947-07-30" } }) {
items {
Name
Born
}
total
}
}
Where with multiple filter conditions
{
BiographyPage(where: {
Name: {
eq: "Alan Turing"
}
Language: {
DisplayName: {
eq: "English"
}
}
}, locale: [en]) {
Name
Language {
Name
DisplayName
}
}
}
Note
The AND operator is the default logical operator that applies to the group of multiple filtered conditions, so the above query can be understood like this:
if (Name == "Alan Turing" AND Language.DisplayName == "English")
See Logical connectors for information about logical operators AND, OR, and NOT.
Where with _modified extend date argument.
The _modified field contains the date time when content was created/updated/deleted in the service. The value in _modify will not be the same as the Created/Saved value on the content.
The _modified field can be used to accomplish delta import of content from the service, for example "give me content that has been modified after (gt) 2023-09-13T04:36:14Z. You can use filtering by _modify field with cursor to accomplish good delta import of content from the service.
{
BiographyPage(where: {
_modified: {
gte: "2021-09-13T04:36:14Z"
}
}, locale: [en]) {
Name
Language {
Name
DisplayName
}
}
}
Updated 2 months ago