The advanced query syntax accepted by the query argument on datastore_items and profiles: fields, conditions, grouping, and keyword synonyms.
How a query's field and value tokens are written, including quoting rules.
The comparison operators a query phrase can use between a field and a value.
Joining and inverting phrases with AND, OR, and NOT, their interchangeable spellings, and operator precedence.
Query keywords, field IDs, and values all match without regard to case.
Building a query string in Liquid and passing it to the query argument, with safe escaping for user input.
{%- capture var filterQuery -%}description CONTAINS "{{ term }}" OR requirements CONTAINS "{{ term }}"{%- endcapture -%}
{%- datastore_items items = datastore:entity query:filterQuery -%}{%- var query = "movein_ready = true" -%}
{%- if request.query_params.beds is_int -%}
{%- set query = query | append: " and beds = " | append: request.query_params.beds -%}
{%- endif -%}
{%- if request.query_params.baths is_int -%}
{%- set query = query | append: " and baths = " | append: request.query_params.baths -%}
{%- endif -%}
{%- datastore_items collection = datastore:entity query:query -%}Simple range and comparison query
{%- datastore_items items = datastore:"Hotels" query:"number_of_rooms > 100 AND average_room_price <= 100" -%}Filters the Hotels datastore to items with more than 100 rooms and an average room price of 100 or less, combining two numeric comparisons with AND.
Complex nested query with NOT and an optional condition
{%- capture query -%}
is_vegan
OR
(
number_of_ingredients < 6
AND (
NOT ingredients contains chicken
-ingredients contains pork
- ingredients LIKE 'beef'
{%- if other_meat_to_avoid is_valid -%}
AND !(ingredients CONTAINS "{{other_meat_to_avoid | replace: '"', '""' }}")
{%- endif -%}
)
AND NOT (
'contains_dairy'
OR ingredients contains egg
)
)
{%- endcapture -%}
{%- datastore_items items = datastore:'Recipes' query:query -%}Matches vegan recipes, or recipes with fewer than 6 ingredients that avoid chicken, pork, and beef (using NOT and the bare - shorthand for AND NOT), plus an optional other_meat_to_avoid the caller may supply, and excludes anything marked contains_dairy or containing egg.
Query multiple configurable fields dynamically
{%- set query = "" -%}
{%- set fieldvalue = request.query_params['fieldvalue'] | urldecode -%}
{%- if request.query_params['fieldnames'] is_valid and fieldvalue is_valid -%}
{%- var fieldnames = request.query_params['fieldnames'] | urldecode | split: ',' -%}
{%- var queryparts = '' | compact -%}
{%- for field in fieldnames -%}
{%- capture querypart %}{{field}} = "{{fieldvalue | replace: '"', '""'}}"{% endcapture -%}
{%- set queryparts = queryparts | concat: querypart -%}
{%- endfor -%}
{%- set query = queryparts | join: ' OR ' -%}
{%- endif -%}
{%- datastore_items items = datastore:entity query:query -%}Builds an OR query across a caller-supplied, comma-separated list of field names (fieldnames), matching each one against the same escaped value (fieldvalue) from the request's query string.