Sorting and limiting control the order and size of a semantic query’s result set. Add sort orders to arrange result rows by a field, an alias, or an aggregated measure, and add a limit to cap the number of rows returned. Both are set per query in the options block of structuredSemanticQuery.
Sorting is expressed as a list of sort orders (sort_orders), applied in the order you list them: the first sort order is the primary key, each subsequent one breaks ties. Two sort-order shapes are available — a simple_sort_order that orders by a single field or alias, and an aggregative_sort_order that orders a grouped dimension by an aggregated measure. You can mix both in one request; a common pattern is an aggregative sort followed by a simple tie-breaker.
Limiting caps the number of result rows and is applied after grouping. Because the limit applies to grouped output, it constrains the number of groups returned, not the number of underlying source records.
Metadata in the model
Sorting has an optional model-defined counterpart. A field’s default sort direction is set at authoring time through the SortOrder attribute (values Ascending, Descending, None) on a Semantic Data Object Field, a Semantic Calculated Dimension, or a Semantic Calculated Measurement. Set apply_model_sorting: true in the query to apply those model-defined sort directions to the result. Limiting has no model metadata — it is set per query.
Use simple_sort_order to order results by a single field or alias. Reference the sort key either by sort_by_field (a full field expression) or by sort_by_field_alias (an alias already assigned in the query’s fields). List multiple entries in sort_orders to sort by more than one key; they apply in order.
Use aggregative_sort_order to order a grouped dimension by an aggregated measure — for example, sort each account group by its SUM(Annual Revenue). Identify the grouped column with grouped_field_alias (an alias) or grouped_expression (a field expression), and specify the measure to sort by with sort_by_field, sort_by_field_alias, or sort_by_row_count: true to sort by the group’s row count. Aggregative and simple sort orders combine in one sort_orders list.
Set sorting_order on a sort order to ASC (ascending) or DESC (descending). When omitted, results sort ascending. This applies to both simple_sort_order and aggregative_sort_order. You can sort by an alias assigned to a calculated field in the query.
Set null_values_position on a sort order to control where rows with null sort-key values land: NULLS_FIRST places them before non-null rows, NULLS_LAST places them after. This applies to both sort-order shapes. The example above under Simple sort order sets "null_values_position": "NULLS_FIRST", and the example below sets NULLS_LAST.
When you sort by a field without overriding the direction, results order by the field’s data type: dimension columns sort alphanumerically, table fields sort by their underlying type (numeric fields sort numerically), and date and datetime fields sort chronologically. The following example sorts a date field descending.
Set apply_model_sorting: true to apply the model-defined SortOrder of each queried field to the result. In the following example the queried fields carry a model sortOrder (Descending on the dimension, Ascending on the calculated measurement), and the query opts in to those directions.
Use limit_options.limit to cap the number of result rows. The limit is applied after grouping, so it constrains the number of groups returned rather than the number of underlying source records. For a result grouped by dimension, a limit of 30 returns at most 30 groups even when the source has more.
Performance: A limit caps the number of result rows after grouping — it constrains the groups returned, not the rows scanned. Use filters to reduce the scan.
Performance: Result size is capped at 5,000 rows for interactive queries and 50,000 rows for non-interactive queries; a query that would exceed the cap fails. Narrow the result with filters or a limit.
Detailed rows
Set detailed_rows: true to return the individual detail rows behind an aggregated result alongside the aggregates, rather than only the grouped/aggregated output.
Performance: When you request detailed (non-aggregated) rows, a limit is pushed into the detail-rows step, reducing the rows scanned and returned.
Row counts
Set row_counts: true to include row-count information in the query result. Combine it with sort_by_row_count (see Aggregative sort order) to sort groups by how many rows each contains.
A sort order references exactly one key. Provide one of sort_by_field / sort_by_field_alias (and, for aggregative sorts, one of grouped_field_alias / grouped_expression); the alias must match a field defined in the query.
The limit value must be non-negative.
limit is applied after grouping. It caps the number of grouped result rows, not the number of underlying source records, so it directly affects how a grouped visualization renders.
Related
Grouping — Grouping drives the aggregation that aggregative sorts and limits operate on.
Aggregation & Totals — Aggregation methods used in sort_by_field and row counts/totals.