搜尋 API 接入與呼叫範例
通智搜尋 API 的接入流程與可執行範例:憑證、首個請求、欄位、過濾、排序、分頁、聚合和錯誤處理。
這篇指南把「取得憑證 → 發出第一請求 → 組合篩選、排序、分頁和聚合 → 上線前自查」整理成一條可執行路徑。範例數據來自 Aurora-Living-Product-Search(應用 ID 11942244)。
開始之前
你需要三項資訊:控制台裡的 應用 ID、一組 API Key,以及索引裡真實的欄位名。應用 ID 用來核對請求打到了哪個應用;API Key 只在後端使用;欄位名不要直接沿用範例,先用欄位介面查詢目前索引實際有哪些欄位。

在控制台開啟「通智搜尋 → 應用 → 應用詳情」,找到「接入指引」。API Key 預設隱藏,點眼睛圖示可查看;旁邊有「API 接入」「頁面元件接入」「介面除錯」三個入口。本文用的是 API 接入和介面除錯。
認證:兩種方式,不要混用
搜尋介面只有一個地址:POST https://api.tengence.com/v1/search,請求體是 JSON,認證放在 Authorization 頭裡。憑證分兩種:後端直接用 API Key,前端用後端簽發的 AccessToken。API Key 不要下發到瀏覽器。
後端直連:API Key
在服務端請求裡直接帶 Authorization: Bearer <API Key>。API Key 以 sk- 開頭,閘道器據此識別這是應用金鑰,並自動注入應用身份,客戶端不需要再傳應用 ID。

export TENGENCE_API_KEY="sk-你的APIKey"
curl -X POST "https://api.tengence.com/v1/search" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TENGENCE_API_KEY" \
-d '{
"query": "table",
"page": 1,
"size": 3
}'
前端接入:後端簽發 AccessToken
如果搜尋請求必須從瀏覽器發出,不要讓前端持有 API Key。由業務後端用 API Key 調簽發介面,取得一對短期憑證,再把 accessToken 交給前端;refreshToken 留在後端,用來續期或回收。

簽發請求:
curl -X POST "https://api.tengence.com/v1/app/auth/token/issue" \
-H "Content-Type: application/json" \
-d '{
"apiKey": "sk-你的APIKey",
"deviceId": "web-设备或会话标识",
"deviceType": "web"
}'
真實響應(令牌已截斷):
{
"code": "SUCCESS",
"message": "Operation successful",
"data": {
"accessToken": "eyJ...",
"refreshToken": "eyJ...",
"accessExpAt": 1796364665493,
"refreshExpAt": 1796968565493,
"appId": "11942244",
"tokenType": "Bearer"
}
}
accessToken 有效期約 15 分鐘,refreshToken 約 7 天;具體以響應裡的 accessExpAt、refreshExpAt 為準。前端拿 accessToken 調搜尋介面,用法和 API Key 一樣,只是把 Bearer 後面的值換掉。過期前由後端調 POST /v1/app/auth/token/refresh 換新的一對令牌;使用者退出或憑證洩露時調 POST /v1/app/auth/token/revoke 撤銷 refreshToken。
第一個搜尋請求
請求只要一個 query 就能跑。page 預設 1,size 預設 10;這裡顯式寫 3,方便看返回結構。
curl -X POST "https://api.tengence.com/v1/search" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TENGENCE_API_KEY" \
-d '{"query":"table","page":1,"size":3}'
真實響應(只保留兩條商品,took 單位為毫秒):
{
"code": "SUCCESS",
"message": "Operation successful",
"data": {
"items": [
{
"item_id": "AL-TBL-026",
"id": "AL-TBL-026",
"title": "nook side table",
"description": "compact side table with a rounded top and a lower shelf for books. fits beside armchairs and beds.",
"category_name": "side tables",
"price": "249",
"rating": "4.8"
},
{
"item_id": "AL-TBL-422",
"id": "AL-TBL-422",
"title": "fjord oak dining table",
"description": "solid oak dining table for six, finished with a matte water-based lacquer. trestle base leaves clear space at each end.",
"category_name": "dining tables",
"price": "1299",
"rating": "4.8"
}
],
"total": 4,
"aggs": [],
"took": 196
}
}
響應包只有 code、message、data,沒有 success 欄位。判斷成功看 code == "SUCCESS",不要看 HTTP 200 就認為業務成功。data.total 是命中總數,不是目前頁條數;data.took 是服務端耗時。
先確認欄位名
範例裡的 title、price、category_name 來自演示索引,不等於你的索引也有這些欄位。接入前先查欄位清單:
curl "https://api.tengence.com/v1/search/index/fields" \
-H "Authorization: Bearer $TENGENCE_API_KEY"
{
"code": "SUCCESS",
"message": "Operation successful",
"data": [
{"indexFieldName": "category_name", "semanticType": "CATEGORY_NAME", "processType": "ANALYZED_TEXT"},
{"indexFieldName": "description", "semanticType": "DESCRIPTION", "processType": "ANALYZED_TEXT"},
{"indexFieldName": "item_id", "semanticType": "ID", "processType": "KEYWORD"},
{"indexFieldName": "price", "semanticType": "PRICE", "processType": "NUMERIC"},
{"indexFieldName": "rating", "semanticType": "RATING", "processType": "NUMERIC"},
{"indexFieldName": "title", "semanticType": "TITLE", "processType": "ANALYZED_TEXT"}
]
}
processType 決定這個欄位能怎麼用:KEYWORD 適合精確匹配和 terms 聚合;ANALYZED_TEXT 會被分詞,精確短語過濾和 terms 聚合的行為不同;NUMERIC 適合區間過濾和 range 聚合。欄位名、類型對不上,後面的篩選和排序都會失效或返回空結果。
常用呼叫範例
限定搜尋欄位
預設會在所有可搜尋欄位裡找。只想搜標題時傳 searchFields:
-d '{"query":"table","searchFields":["title"],"size":1}'
同一查詢 table,限定 title 命中 3 條,限定 description 命中 4 條。欄位名不存在時不報錯,只返回 0 條,所以上線前一定要用欄位介面核對。
裁剪返回欄位
includeFields 只保留業務需要的欄位,減少響應體積:
-d '{"query":"table","size":1,"includeFields":["title"]}'
{
"title": "nook side table",
"id": "AL-TBL-026"
}
即使只請求 title,id 也會保留,用來做詳情跳轉或去重。不要假設 includeFields 能去掉 id。
高亮關鍵詞
傳 highlightFields 後,命中的詞會直接包在原欄位裡,用 <em> 標記,不存在單獨的 highlight 物件:
-d '{"query":"table","size":1,"highlightFields":["title","description"]}'
{
"title": "nook side <em>table</em>",
"description": "compact side <em>table</em> with a rounded top and a lower shelf for books. fits beside armchairs and beds."
}
渲染前請做 HTML 轉義或只允許 <em> 標籤,不要把整段描述直接塞進 innerHTML。
精確過濾與數值區間
按商品 ID 精確過濾,item_id 是 KEYWORD 欄位,eq 穩定返回 1 條:
-d '{"query":"table","filters":[{"field":"item_id","operator":"eq","value":"AL-TBL-318"}]}'
{
"item_id": "AL-TBL-318",
"title": "halo marble coffee table",
"price": "649"
}
批次 ID 用 in,多個條件之間是 AND:
-d '{"query":"table","filters":[{"field":"item_id","operator":"in","values":["AL-TBL-026","AL-TBL-318","AL-LGT-603"]}]}'
按價格區間篩選,兩個條件同時給:
-d '{"query":"table","filters":[{"field":"price","operator":"gte","value":600},{"field":"price","operator":"lte","value":1000}]}'
真實命中 1 條:AL-TBL-318,價格 "649"。price 在響應裡是字串,但過濾時傳數字或字串都能工作;做數值比較前先轉成數字。
category_name 是分詞欄位。eq "coffee tables" 這種多詞短語返回 0 條,eq "coffee" 才命中 1 條。需要完整類目名精確匹配時,優先用 item_id 這類 KEYWORD 欄位,或在客戶端按 category_name 原值再篩一次。
排序
sortFields 的 order 必須是大寫 ASC 或 DESC。小寫 asc 會返回 400 BAD_REQUEST,不是被忽略。
-d '{"query":"a","size":3,"sortFields":[{"field":"price","order":"ASC"}],"includeFields":["price"]}'
{"code":"SUCCESS","data":{"items":[{"price":"79","id":"AL-DEC-805"},{"price":"119","id":"AL-DEC-067"},{"price":"129","id":"AL-STO-014"}]}}
把 ASC 換成 DESC,同一查詢返回 1899、1599、1499。排序欄位必須是欄位清單裡的真實欄位;未知排序欄位可能直接返回空結果。
分頁
-d '{"query":"table","page":2,"size":3}'
第一頁 3 條,第二頁只剩 1 條:AL-LGT-603。total 仍為 4。size 最大 100;傳大於 100 的值會回落到預設 10 條,不會報錯。page 從 1 開始,超出總頁數時不要依賴固定回退行為,客戶端按 total 和 size 自己算頁數。
區間聚合
按價格分檔統計,聚合結果在 data.aggs 裡,是一個數組:
-d '{
"query":"table",
"size":1,
"aggregations":[
{"name":"by_price","type":"range","field":"price","ranges":[
{"key":"under_500","to":500},
{"key":"500_1500","from":500,"to":1500}
]}
]
}'
{
"name": "by_price",
"field": "price",
"type": "range",
"items": [
{"value":"under_500","label":"under_500","count":2,"from":0.0,"to":500.0},
{"value":"500_1500","label":"500_1500","count":2,"from":500.0,"to":1500.0}
]
}
terms 聚合只適合 KEYWORD 欄位。對 category_name 這種 ANALYZED_TEXT 欄位做 terms,會把 total 變成 0、aggs 變空;需要類目分佈時,先確認欄位類型,或改用數值區間聚合。
分檔只傳 from、不傳 to 時,目前該檔返回 count: 0;需要開放上界時,明確傳一個足夠大的 to。
綜合查詢:過濾、排序、高亮與聚合
把 filters、sortFields、highlightFields、searchFields、includeFields 和 aggregations 放進同一個請求。以下以商品搜尋為例:價格 400–1400 元、評分不低於 4.7,先按價格降序再按評分降序,高亮標題和描述,同時返回價格與評分分檔。
curl -X POST https://api.tengence.com/v1/search \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TENGENCE_API_KEY" \
-d '{
"query": "table",
"page": 1,
"size": 3,
"filters": [
{
"field": "price",
"operator": "gte",
"value": 400
},
{
"field": "price",
"operator": "lte",
"value": 1400
},
{
"field": "rating",
"operator": "gte",
"value": 4.7
}
],
"sortFields": [
{
"field": "price",
"order": "DESC"
},
{
"field": "rating",
"order": "DESC"
}
],
"highlightFields": [
"title",
"description"
],
"searchFields": [
"title",
"description"
],
"includeFields": [
"item_id",
"title",
"price",
"rating",
"category_name",
"description"
],
"aggregations": [
{
"name": "price_bands",
"field": "price",
"type": "range",
"ranges": [
{
"key": "0_500",
"from": 0,
"to": 500
},
{
"key": "500_1000",
"from": 500,
"to": 1000
},
{
"key": "1000_1500",
"from": 1000,
"to": 1500
}
]
},
{
"name": "rating_bands",
"field": "rating",
"type": "range",
"ranges": [
{
"key": "4.6_4.8",
"from": 4.6,
"to": 4.8
},
{
"key": "4.8_5.0",
"from": 4.8,
"to": 5.0
}
]
}
]
}'
響應只展示第一條商品,其餘兩條省略。
{
"code": "SUCCESS",
"message": "Operation successful",
"data": {
"items": [
{
"category_name": "dining tables",
"item_id": "AL-TBL-422",
"price": "1299",
"rating": "4.8",
"description": "solid oak dining <em>table</em> for six, finished with a matte water-based lacquer. trestle base leaves clear space at each end.",
"id": "AL-TBL-422",
"title": "fjord oak dining <em>table</em>"
}
],
"total": 3,
"aggs": [
{
"name": "price_bands",
"field": "price",
"type": "range",
"items": [
{
"value": "0_500",
"label": "0_500",
"count": 1,
"from": 0.0,
"to": 500.0
},
{
"value": "500_1000",
"label": "500_1000",
"count": 1,
"from": 500.0,
"to": 1000.0
},
{
"value": "1000_1500",
"label": "1000_1500",
"count": 1,
"from": 1000.0,
"to": 1500.0
}
]
},
{
"name": "rating_bands",
"field": "rating",
"type": "range",
"items": [
{
"value": "4.6_4.8",
"label": "4.6_4.8",
"count": 1,
"from": 4.6,
"to": 4.8
},
{
"value": "4.8_5.0",
"label": "4.8_5.0",
"count": 2,
"from": 4.8,
"to": 5.0
}
]
}
],
"took": 340
}
}
過濾條件之間是 AND;排序按陣列順序生效;聚合基於過濾後的結果計算。範例中 3 條商品正好落在 3 個價格檔和 2 個評分檔。
如果某個欄位只放在 highlightFields 裡、卻被 includeFields 裁掉,響應裡不會出現這個欄位,高亮內容也看不到;高亮欄位要同時保留在返回欄位中。
要限定指定商品集合,把 item_id 的 in 條件加進 filters;item_id 是 KEYWORD,精確匹配可用。
{"field":"item_id","operator":"in","values":["AL-TBL-026","AL-TBL-318","AL-TBL-422"]}
錯誤與邊界
認證錯誤是少數會明確返回 HTTP 4xx 的情況:
{"code":"MISSING_CREDENTIAL","message":"Please provide a valid credential","data":null}
{"code":"API_KEY_INVALID","message":"Invalid API key, please check and retry","data":null}
兩者都是 HTTP 401。缺少 Authorization 頭是 MISSING_CREDENTIAL;Key 寫錯或失效是 API_KEY_INVALID。
搜尋介面對部分參數錯誤很寬鬆:空 query、沒有 query、未知 searchFields 欄位、未知過濾欄位、非法過濾運算子,都可能返回 HTTP 200 + code: SUCCESS,結果是 0 條或過濾條件被忽略。小寫排序 order 則會返回 400 BAD_REQUEST。因此客戶端必須在發請求前自己校驗:query 非空、欄位名來自欄位介面、order 用大寫、過濾條件至少能命中一條。
排障時保留響應頭裡的 x-tengence-request-id;如果客戶端自己傳了同名頭,響應會回傳同一個值,方便和服務端日誌對齊。
上線前檢查
- 用欄位介面確認欄位名和
processType,不要直接沿用範例。 - API Key 只放在後端環境變數,不進前端包、不進 Git。
- 前端走 AccessToken,後端儲存
refreshToken並處理續期和撤銷。 - 統一按
code判斷業務結果,HTTP 200 不等於成功。 - 對
includeFields保留id的行為做兜底。 - 高亮結果渲染前做 HTML 轉義。
- 分頁按
total和size計算,不依賴越界回退。 - 記錄
x-tengence-request-id,方便排查。