Search

How to perform a search query and how it works

Searches are a type of query a bit different from the other ones (breakdowns, date breakdowns, and stats). A search is not an aggregation and doesn't involve any computation based on operators, but an actual search. When performing a search query on the Metrics API endpoint you get in the response the list of the requested fields (sorted, filtered, and paginated) of the actual records that match the query.

Request

To perform a search query send a POST request to the /{{resource_name}}/search endpoint specifying the query keys and filter parameters:

{
  "search": {
    "limit": ...,
    "sort": "...",
    "sort_by": "...",
    "fields": [ ... ],
    "cursor": "..."
  },
  "filter": { ... },
  "meta": { ... }
}

Query keys

Requesting all the field's attributes

In some cases, you may need to get in the search results all the attributes of some fields for each record. If so, you can use the .* syntax in the request. Just add {{field_name}}.* to the field array for each field you want to fully detail (see example).

Response

The response of a search query returns an array of objects containing a number of records equal to (or less than) the one specified in the limit key, sorted by the date field specified in the sort_by key. For each record all the fields requested in the fields array are returned:

{
  "data": [
    { 
      "...": "...", // requested fields
      "...": "...",
      "...": { ... }
      "...": ...
    },
    { 
      "...": "...", // requested fields
      "...": "...",
      "...": { ... }
      "...": ...
    },
    { 
      "...": "...", // requested fields
      "...": "...",
      "...": { ... }
      "...": ...
    },
    { ... }
  ],
  "meta": {
    "pagination": {
      "record_count": ...,
      "cursor": "..."
    },
    ...
  }
}

Pagination

Unlike the other types of queries (breakdowns, date breakdowns, and stats) when performing a search query on the Metrics API endpoint you get paginated results. You can find the info you need to navigate through the pages in the pagination object of the response's meta:

The value of the cursor can be added to the request payload to get that specific page's records in the response (see example).

Examples

The following examples will be focused on the query part of the request. So no specific filter or meta options will be defined (i.e. all the results will be filtered by the default filter and the response won't include the request payload). See the use cases section for more complex combinations of queries and filters.

Requesting specific fields

The following request performs a search query to get information about the orders total amount, currency, alongside the associated customer email and shipping address information, sorted by the time when they were placed:

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.co/metrics/orders/search' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
    "search": {
      "limit": 5,
      "sort": "desc",
      "sort_by": "order.placed_at",
      "fields": [
        "order.id",
        "order.number",
        "order.placed_at",
        "order.total_amount_with_taxes",
        "order.currency_code",
        "customer.email",
        "shipping_address.state_code",
        "shipping_address.zip_code"
      ]
    }
  }'

Requesting all the attributes for some fields

The following request performs a search query to get all the available fields for the last two orders placed and the associated customers:

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.co/metrics/orders/search' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
      "search": {
        "limit": 2,
        "sort": "desc",
        "sort_by": "order.placed_at",
        "fields": [
          "order.*",
          "customer.*"
        ]
      }
  }'

Requesting a specific page from the paginated results

The following request performs a search query to get information about the number of shipments associated with the orders and their fulfillment status, sorted by the time of the last fulfillment status update. A specific page in the paginated results is requested:

curl -g -X POST \
  'https://{{your_domain}}.commercelayer.co/metrics/orders/search' \
  -H 'Accept: application/vnd.api.v1+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -H 'Authorization: Bearer {{your_access_token}}' \
  -d '{
      "search": {
        "limit": 5,
        "sort": "desc",
        "sort_by": "order.fulfillment_updated_at",
        "fields": [
          "order.id",
          "order.number",
          "order.shipments_count",
          "order.fulfillment_status",
          "order.fulfillment_updated_at"
        ],
        "cursor": "LS0tCi0gJzIwMjItMDYtMjZUMDY6MD=="
      }
  }'

Last updated