The sample database
The store is called Trailhead Outfitters. The data is generated, but we tried to make it messy in the ways real databases are messy. There's a category tree, a many-to-many table for tags, one table without a primary key and one column that points to another table but has no foreign key. 50,587 rows in total.
| Table | Rows | What it shows |
|---|---|---|
brands | 123 | Foreign key target, embedded in each product |
categories | 107 | Tree through parent_id, up to five levels deep |
tags | 25 | Labels such as waterproof and pfc-free |
products | 8,347 | The main model: name, description, price, status |
product_tags | 14,373 | Pivot table, becomes a tags array on each product |
product_variants | 12,326 | No primary key: sku is chosen as one during import |
reviews | 15,286 | No foreign key on product_id: the relation is added by hand |
Download trailhead-outfitters.sql (4.8 MB) if you want to try the same import in your own account.
Import the dump
The steps themselves are short. The details are in the SQL import docs and shown on screen in Guide 14 and Guide 15.
- Create an API, then upload the
.sqlfile in Data Import. - Review the draft: SearchAPI lists every table with its role, columns, detected types and relations.
- Adjust the settings described in the next section.
- Click Start import. Each table gets a model, endpoints and an index, in dependency order.
- Assign API keys to the new endpoints and deploy the version.
Analyzing the file took about two seconds. The import itself, all seven tables, finished in under a minute.
Before you click Start import
Our first attempt used the defaults almost everywhere, and we ended up importing the database more than once. Some settings can't be changed after the import has created the endpoints, so it's worth going through the draft carefully. Here's what we'd check, in the order it bit us.
Names need to be text, not keyword
This was the first surprise. We searched for ?q=rain and got backpacks, because their
descriptions mention "a rain cover". Not a single rain jacket. The reason: VARCHAR columns come in
as keyword, and a keyword only matches its exact, case-sensitive value. That's what you want for
sku or status. For names it isn't. In the next run we switched
products.name, brands.name, categories.name and
reviews.title to text and marked them Searchable, along with the
description fields.
Rename relation fields
We skipped this at first. After the import, brand_id doesn't hold an id anymore, it holds the
whole brand object, so you end up filtering on brand_id.name. It works, it just reads badly.
We renamed the relation fields to brand, category and product in
Model Field Name.
Choose what each relation embeds
A related record only carries the fields marked Visible In Related Model. Leave them all
unchecked and every product embeds a brand with nothing but an id. We marked brands.name, categories.name, tags.name,
products.name and products.price. Easy to miss: a searchable field
of a related table is only matched by q if it's also visible.
Decide on aggregations early
This one sent us back to the start, with a new import into a fresh API. Aggregations are what produce the
facet counts further down, and they're chosen in the draft. We picked them before marking the related fields visible, so the brand and category
options simply weren't in the list yet. On products we wanted brand.name,
category.name, tags.name and status. Set visibility first, then come
back to aggregations.
Fix what the draft flags
The draft marked a few things for us to deal with. None of them took long.
product_variantshas no primary key, so we selectedskuas the key.reviews.product_idhas no foreign key, so we added the relation toproductsby hand.product_tagswas detected as a pivot table; we choseproductsto receive the nestedtagsfield.categories.parent_idwas detected as a self-reference, which turns the categories into a tree.
What the tables become
Here's one product, fetched by id. The brand, the tags from the pivot table and the whole category path are already in the record, so the storefront doesn't need a second request:
GET https://trailhead.searchapi.net/catalog/v1/products/29
X-API-KEY: your-read-key
{
"id": 29,
"sku": "SKU-00029",
"name": "Cedar Co Nomad Waterproof Men's Jacket",
"description": "Made for changing mountain weather, with YKK zippers and bluesign approved materials.",
"price": 401.99,
"sale_price": 321.99,
"weight_grams": 801,
"status": "active",
"created_at": "2022-09-25",
"brand": { "id": 64, "name": "Cedar Co" },
"category": {
"id": 61,
"name": "Softshell jackets",
"ancestors": [
{ "id": 54, "name": "Clothing" },
{ "id": 55, "name": "Men" },
{ "id": 56, "name": "Jackets" }
],
"depth": 3
},
"tags": [
{ "id": 8, "name": "sale" },
{ "id": 1, "name": "waterproof" }
]
}
| In MySQL | In the API |
|---|---|
products.brand_id → brands.id | brand object with the visible brand fields |
product_tags pivot table | tags array on each product |
categories.parent_id | ancestors and depth, ready for a breadcrumb |
reviews.product_id (no foreign key) | product object on each review |
Search, filter and sort
Every table gets a list endpoint. We called them with a read-only key and trimmed the responses with
includes so they fit on the page. All parameters are described in
Using your API. JSON filter values are shown unencoded to keep
them readable; URL-encode them in real requests. HTTP libraries do this for you, while curl needs
-g or encoded values.
Full-text search
Each word has to show up somewhere: in the product itself, or in its brand, category or tags.
GET /catalog/v1/products?q=waterproof%20jacket&includes=name,price,brand,category,tags
{
"status": "success",
"type": "ok",
"code": 200,
"message": "OK",
"data": {
"pagination": { "start": 0, "limit": 10, "count": 10, "total": 369, "nextSearchAfter": [5.8779955, 1471] },
"results": [
{
"name": "Cedar Co Nomad Waterproof Men's Jacket",
"price": 401.99,
"brand": { "id": 64, "name": "Cedar Co" },
"category": { "id": 61, "name": "Softshell jackets", "ancestors": [ … ], "depth": 3 },
"tags": [ { "id": 8, "name": "sale" }, { "id": 1, "name": "waterproof" } ],
"_score": 5.8779955
},
…
],
"aggregations": { … }
}
}
Filters and price range
Filters narrow the results but don't affect the ranking. Ranges use [from] and [to].
GET /catalog/v1/products?q=rain%20jacket&status=active&price[from]=100&price[to]=200
That leaves 52 active rain jackets between 100 and 200, such as the "Polar Mountain Basecamp Recycled Men's Jacket" at 154.99.
Worth knowing: draft and discontinued products come back too unless you filter them out. Our storefront
always sends status=active.
Products that have two tags
Separate the objects with | and a product has to match all of them. Here, both waterproof and pfc-free.
GET /catalog/v1/products?tags={"name":"waterproof"}|{"name":"pfc-free"}&includes=name,tags
262 products, each with both tags, such as "Timber Works Ridgeline Packable Men's Trousers" (packable, pfc-free, sale, waterproof).
Sort
GET /catalog/v1/products?q=tent&sort=price&includes=name,price
We expected the cheapest tent at the top. Instead we got spare "Tent Pole Set" products at 15.99, since
q=tent matches their names as well. Fair enough, really. 739 matches in total. Use sort=-price for the most expensive first.
A product's reviews and variants
Reviews and variants are filtered by the product they embed, and each one carries the product's name and price.
The jacket has a single review so far:
GET /catalog/v1/reviews?product={"id":29}&sort=-created_at
{
"id": 9765,
"author_name": "Lucas",
"rating": 4,
"title": "Great, minor issues",
"body": "Used it in Patagonia for 11 days. Works well, though the colour faded quickly.",
"verified_purchase": true,
"created_at": "2023-07-01",
"product": { "id": 29, "name": "Cedar Co Nomad Waterproof Men's Jacket", "price": 401.99 }
}
And two variants. Since sku is the primary key we picked during the import, it's also what you'd
use to fetch a single variant: /product_variants/SKU-00029-sand-m.
GET /catalog/v1/product_variants?product={"id":29}
"results": [
{
"sku": "SKU-00029-sand-m",
"color": "Sand",
"size": "M",
"stock": 57,
"product": { "id": 29, "name": "Cedar Co Nomad Waterproof Men's Jacket", "price": 401.99 }
},
{
"sku": "SKU-00029-sand-xxl",
"color": "Sand",
"size": "XXL",
"stock": 34,
"product": { "id": 29, "name": "Cedar Co Nomad Waterproof Men's Jacket", "price": 401.99 }
}
]
Facet counts for the filter sidebar
The aggregation fields come back with every list response, counted over whatever the current search matched.
These are the numbers next to each option in a store's filter sidebar. For ?q=poles:
"aggregations": {
"category_name": {
"buckets": [
{ "key": "Poles|34", "doc_count": 141 },
{ "key": "Poles|12", "doc_count": 126 },
{ "key": "Inflatable pads|18", "doc_count": 48 },
…
]
},
"status": {
"buckets": [
{ "key": "active", "doc_count": 613 },
{ "key": "discontinued", "doc_count": 47 },
{ "key": "draft", "doc_count": 29 }
]
}
}
-
Related fields come back as
value|id, and this is why. Our store has two categories called "Poles". One sits under Hiking and holds trekking poles, the other under Camping › Tents and holds spare tent poles. Search forpolesand you get both, with the same label. The id is the only way to tell them apart, and it's also what you filter on next:category={"id":12}gives you the 126 tent poles. - Options with no matches still come back, with
"doc_count": 0. That keeps the filter list stable between searches; we grey them out in the UI.
Getting rain jackets above backpacks
?q=rain matches rain jackets and rain pants through their category, and backpacks through
descriptions like "with a rain cover". Remember the backpacks from earlier? Even with the names fixed, every
field counts the same by default, so they're still mixed in:
| # | No boost | Score | category.name boosted to 3 | Score |
|---|---|---|---|---|
| 1 | Cedar Labs Tundra Alpine Backpack (Daypacks) | 1 | Arctic Supply Cirrus Recycled Men's Hoodie (Rain jackets) | 3 |
| 2 | Cedar Equipment Cirrus Ultralight Pack (40-60 L packs) | 1 | Timber Works Ridgeline Packable Men's Trousers (Rain pants) | 3 |
| 3 | Arctic Supply Cirrus Recycled Men's Hoodie (Rain jackets) | 1 | North Works Switchback Stretch Men's Hoodie (Rain jackets) | 3 |
| 4 | Timber Works Ridgeline Packable Men's Trousers (Rain pants) | 1 | Trail Works Ember Waterproof Women's Hoodie (Rain jackets) | 3 |
| 5 | North Works Switchback Stretch Men's Hoodie (Rain jackets) | 1 | Trail Labs Horizon Breathable Men's Hoodie (Rain jackets) | 3 |
| 6 | Trail Works Ember Waterproof Women's Hoodie (Rain jackets) | 1 | North Works Summit Thermal Men's Hoodie (Rain jackets) | 3 |
| 7 | Trail Labs Horizon Breathable Men's Hoodie (Rain jackets) | 1 | Glacier Trek Ridgeline Recycled Men's Trousers (Rain pants) | 3 |
| 8 | North Works Summit Thermal Men's Hoodie (Rain jackets) | 1 | Aspen Labs Drift Thermal Men's Hoodie (Rain jackets) | 3 |
| 9 | Ridge Trek Basecamp Ultralight Pack (40-60 L packs) | 1 | Timber Equipment Ridgeline Recycled Men's Jacket (Rain jackets) | 3 |
| 10 | Glacier Trek Ridgeline Recycled Men's Trousers (Rain pants) | 1 | Aspen Mountain Summit Recycled Women's Shell (Rain jackets) | 3 |
We set category.name to 3 under Field Boosts on the products endpoint, and the
whole first page turned into rain jackets and rain pants. The backpacks are still there further down; the
total didn't move from 517. Unlike most endpoint settings, boosts can be changed on a published version and
take effect right away, so you can tune them while watching real results. See Field boosts.
It helps to know how the score is built. A result gets points for each field it matched in, multiplied by that field's boost. Two products that matched in the same fields end up with the same score and are ordered by primary key. So boosts are less about fine-tuning and more about telling the API which fields you care about.
Where it falls short
For a catalog like this one, or a directory, or a mock API for a frontend team that's waiting on the real backend, it worked well. A few things to be aware of before you plan around it:
- The import is a one-time copy. Nothing syncs back from MySQL; afterwards you edit records through the API or in Data UI.
- A table you relate to can have at most 10,000 rows. That's why reviews (15,286 of them) embed their product, not the other way round.
- Relations go one level deep. The product inside a review doesn't bring its brand along.
- It reads MySQL dumps only, in the format mysqldump writes.
Frequently asked questions
- How large can the MySQL dump be?
- A single .sql file of up to 100 MB, in MySQL syntax as produced by mysqldump: CREATE TABLE, ALTER TABLE for foreign keys added separately, and INSERT for the data.
- Does the API stay in sync with my MySQL database?
- No. SQL Import copies the rows once. After that the API is the source: change records through its POST, PUT, PATCH and DELETE endpoints or in Data UI. A new SQL Import always creates new models.
- Can I add aggregations after the import?
- Choose aggregation fields in the import draft, before you start the import. Once the API version is published they are locked; field boosts are the exception and stay editable.
- Why does a search not find a value I can see in the data?
- VARCHAR columns are imported as keyword fields, which match only the exact, case-sensitive value. Change columns you want to search by words, such as names, to text and mark them Searchable.
Other use cases
Try it with your own database
The free Starter plan is enough for a database this size. Upload your dump under Data Import and keep the SQL import docs open next to it.
Create a free account