> ## Documentation Index
> Fetch the complete documentation index at: https://docs.glood.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Filter Aggregations

> Live Elasticsearch facet counts for a given query + applied filters. Auth: `Authorization: Bearer <gl_sf_...>` + `x-shop`.

<ResponseExample>
  ```json 200 theme={null}
  {
    "aggregations": {
      "color": {
        "type": "terms",
        "data_type": "option",
        "buckets": [
          { "value": "red", "label": "Red", "count": 42, "enabled": true, "is_others": false },
          { "value": "blue", "label": "Blue", "count": 30, "enabled": true, "is_others": false }
        ],
        "has_more": false,
        "other_count": 0
      },
      "price": {
        "type": "range",
        "data_type": "price",
        "buckets": [ { "value": "0-50", "label": "$0 – $50", "count": 100 } ],
        "stats": { "min": 5, "max": 499, "avg": 60, "sum": 14400, "count": 240 }
      }
    },
    "facets": [
      { "id": 1, "key": "color", "title": "Color", "data_type": "option", "attribute_type": "option" },
      { "id": 2, "key": "price", "title": "Price", "data_type": "number", "attribute_type": "price" }
    ],
    "tree": { "id": 5, "target_page": "search", "target_id": 10, "is_default": false },
    "metadata": { "total_products": 240, "query_time": 30, "region": "toronto" }
  }
  ```

  ```json 400 theme={null}
  {
    "error": [
      {
        "message": "\"applied_filters[0].key\" is required",
        "path": ["applied_filters", 0, "key"],
        "type": "any.required",
        "context": { "label": "applied_filters[0].key", "key": "key" }
      }
    ],
    "ok": false
  }
  ```
</ResponseExample>


## OpenAPI

````yaml POST /api/storefront/v1/headless/filter-aggregations
openapi: 3.0.1
info:
  title: Glood.AI API Reference
  description: Glood.AI API Reference
  license:
    name: MIT
  version: 1.0.0
servers:
  - url: https://storefront.glood.ai
security:
  - bearerAuth: []
paths:
  /api/storefront/v1/headless/filter-aggregations:
    servers:
      - url: https://search.glood.ai
    post:
      description: >-
        Live Elasticsearch facet counts for a given query + applied filters.
        Auth: `Authorization: Bearer <gl_sf_...>` + `x-shop`.
      parameters:
        - name: x-shop
          in: header
          description: >-
            Shopify store URL. Resolves the shop for the request (headless does
            not accept `?shop=`).
          required: true
          schema:
            type: string
            example: shop.myshopify.com
      requestBody:
        description: Request body
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/HeadlessFilterAggregationsRequest'
      responses:
        '200':
          description: Filter aggregations
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HeadlessFilterAggregationsResponse'
        '400':
          description: Validation or shop error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    HeadlessFilterAggregationsRequest:
      type: object
      description: Live Elasticsearch facet counts for a query + applied filters.
      properties:
        collection:
          oneOf:
            - type: string
            - type: integer
          description: Collection id to scope aggregation. String or integer.
        tree:
          type: integer
          description: Explicit filter tree id.
        query:
          type: string
          description: Search term to aggregate against. May be empty.
          default: ''
        applied_filters:
          type: array
          description: Currently applied filter selections.
          items:
            type: object
            description: An applied filter.
            required:
              - key
              - value
            properties:
              key:
                type: string
                description: Filter key.
              operator:
                type: string
                description: Combine operator (e.g. `or`, `and`).
              value:
                oneOf:
                  - type: array
                    items:
                      type: string
                  - type: string
                  - type: number
                  - type: boolean
                description: Selected value(s).
        facets:
          type: array
          description: Which facets to aggregate.
          items:
            type: object
            description: A facet to aggregate.
            required:
              - key
            properties:
              key:
                type: string
                description: Facet key.
        locale:
          type: string
          description: BCP-47 locale.
          default: en-US
        market:
          type: integer
          description: Shopify market id.
        options:
          type: object
          description: Aggregation options.
          properties:
            include_zero_counts:
              type: boolean
              description: Include buckets with a zero count.
              default: false
            include_disabled:
              type: boolean
              description: Include disabled filters.
              default: false
            sort_by:
              type: string
              description: Bucket sort order.
              enum:
                - count
                - alpha
                - position
              default: count
            limit:
              type: integer
              description: Max buckets per facet.
              minimum: 1
              maximum: 100
              default: 20
        visitor_id:
          type: string
          description: >-
            Anonymous device/browser UID (the `gl_s_uid` cookie). The primary
            personalization key; stays stable when a customer logs in.
        client_id:
          type: string
          description: Per-session / per-device identifier.
        customer_id:
          type: string
          description: >-
            Shopify customer id. Present only when the shopper is logged in;
            enables customer-level personalization.
    HeadlessFilterAggregationsResponse:
      type: object
      description: >-
        Live facet counts (buckets + stats) for the current query and applied
        filters.
      properties:
        aggregations:
          type: object
          description: Live counts keyed by filter key.
          additionalProperties:
            $ref: '#/components/schemas/HeadlessFilterAggregation'
        facets:
          type: array
          description: Facet descriptors for the aggregated filters.
          items:
            type: object
            description: A facet descriptor.
            properties:
              id:
                type: integer
                description: Filter definition id.
              key:
                type: string
                description: Filter key.
              title:
                type: string
                description: Filter title.
              data_type:
                type: string
                description: Filter data type.
              attribute_type:
                type: string
                description: Underlying product attribute type.
        tree:
          nullable: true
          allOf:
            - type: object
              description: Resolved tree summary.
              properties:
                id:
                  type: integer
                  description: Tree id.
                target_page:
                  type: string
                  description: Page the tree targets.
                  enum:
                    - search
                    - collection
                target_id:
                  type: integer
                  nullable: true
                  description: Target object id.
                is_default:
                  type: boolean
                  description: Whether this is the default tree.
          description: Resolved tree summary, or null.
        metadata:
          type: object
          description: Response metadata.
          properties:
            total_products:
              type: integer
              description: Total products matching the query + filters.
            query_time:
              type: integer
              description: Server processing time in ms.
            region:
              type: string
              description: Data-plane region.
              example: toronto
    Error:
      required:
        - error
        - message
      type: object
      properties:
        error:
          type: integer
          format: int32
        message:
          type: string
    HeadlessFilterAggregation:
      type: object
      description: Live Elasticsearch facet counts for one filter key.
      properties:
        type:
          type: string
          description: Aggregation type.
          enum:
            - terms
            - range
        data_type:
          type: string
          description: >-
            Underlying data type of the aggregated attribute (e.g. `option`,
            `price`, `tag`).
        buckets:
          type: array
          description: Aggregated value buckets with live counts.
          items:
            type: object
            description: A single aggregation bucket.
            properties:
              value:
                type: string
                description: Bucket value (or range key like `0-50`).
              label:
                type: string
                description: Human-readable bucket label.
              count:
                type: integer
                description: Number of matching products in the bucket.
              enabled:
                type: boolean
                description: >-
                  Whether this value is currently selectable given the other
                  applied filters.
              is_others:
                type: boolean
                description: >-
                  True for the synthetic bucket that groups values beyond the
                  display limit.
        has_more:
          type: boolean
          description: Whether more buckets exist beyond the returned set.
        other_count:
          type: integer
          description: Count rolled into the 'others' bucket when `has_more` is true.
        stats:
          type: object
          description: Numeric statistics. Present only for range/price aggregations.
          properties:
            min:
              type: number
              description: Minimum value across matches.
            max:
              type: number
              description: Maximum value across matches.
            avg:
              type: number
              description: Average value across matches.
            sum:
              type: number
              description: Sum of values across matches.
            count:
              type: integer
              description: Number of matched documents.
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer

````