搜索 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,方便排查。