返回

通智搜索 API 文档:认证方式、搜索接口与调用示例

通智云团队 ·
API集成 搜索系统 通智搜索
通智搜索 API 文档:认证方式、搜索接口与调用示例

通智搜索是一款高性能智能搜索服务,提供全文检索、智能聚合、灵活过滤等功能,帮助您快速构建专业搜索体验。

关键要点

目录


快速开始

基础信息

项目 说明
接口地址 https://api.tengence.com/v1/search
请求方式 POST
内容类型 application/json
字符编码 UTF-8

快速示例

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "page": 1,
    "size": 10
  }'

认证方式

所有接口需要在请求头中包含 API Key:

Authorization: Bearer {YOUR_API_KEY}

获取 API Key:登录 Tengence 控制台 (https://console.tengence.com),在「应用详情」页中获取API Key。


搜索接口

POST https://api.tengence.com/v1/search

请求方式:POST

执行搜索请求并返回匹配结果。

请求头

参数名 类型 必填 说明
Authorization string 是 Bearer {YOUR_API_KEY}
Content-Type string 是 application/json

请求参数

参数名 类型 必填 默认值 说明
query string 是 – 搜索关键词
page number 否 1 页码,从1开始
size number 否 20 每页数量,最大100
sortFields array 否 – 排序字段列表
highlightFields array 否 – 高亮字段列表
aggregations array 否 – 聚合配置列表
searchFields array 否 – 搜索字段限制
includeFields array 否 – 返回字段限制
filters array 否 – 过滤条件列表

filters 参数结构

filters 参数用于精确过滤搜索结果,支持多种操作符。

FilterParam 结构

字段名 类型 必填 说明
field string 是 字段名,使用下划线命名法,如 create_time
operator string 是 操作符,见下方支持的操作符列表
value string/number 否 单值,用于 eq/ne/gt/gte/lt/lte 操作符
values array 否 多值数组,用于 in/not_in 操作符

支持的操作符

操作符 含义 适用类型 示例值
eq 等于 字符串/数字/日期 "published"
ne 不等于 字符串/数字/日期 "draft"
gt 大于 数字/日期 100
gte 大于等于 数字/日期 100
lt 小于 数字/日期 1000
lte 小于等于 数字/日期 1000
in 包含于 字符串/数字 ["a", "b"]
not_in 不包含于 字符串/数字 ["x", "y"]
exists 字段存在 – null
not_exists 字段不存在 – null

说明

sortFields 参数结构

字段名 类型 必填 说明
field string 是 排序字段
order string 否 ASC/DESC,默认DESC

aggregations 参数结构

字段名 类型 必填 说明
name string 是 聚合名称(返回结果的key)
field string 是 聚合字段
type string 是 聚合类型:terms/range/date_histogram
size number 否 返回数量(仅terms,默认10)
sortBy string 否 排序字段:count/value(仅terms)
sortOrder string 否 排序顺序:ASC/DESC(仅terms)
ranges array 否 范围定义(仅range)
interval string 否 时间间隔(仅date_histogram)

interval 参数说明

值 说明 适用场景
1m 按分钟统计 实时数据监控
1h 按小时统计 日趋势分析
1d 按天统计 日报表
1w 按周统计 周报表
1M 按月统计 月报表
1y 按年统计 年度报表

响应结构

字段名 类型 说明
code string 状态码
message string 响应消息
data object 响应数据
data.items array 搜索结果列表
data.total number 匹配文档总数
data.aggs array 聚合结果列表
data.took number 查询耗时(毫秒)
success boolean 是否成功

响应示例

{
  "code": "SUCCESS",
  "message": "Operation successful",
  "data": {
    "items": [
      {
        "id": "123",
        "title": "AI Agent Platform",
        "price": 4999
      }
    ],
    "total": 1520,
    "aggs": [],
    "took": 45
  },
  "success": true
}

调用示例

1. 基础搜索

场景:最简单的关键词搜索

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai"
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai'
  })
});
const data = await response.json();
# Python
import requests

response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai'
    }
)
data = response.json()
// Java
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.tengence.com/v1/search"))
    .header("Content-Type", "application/json")
    .header("Authorization", "Bearer {YOUR_API_KEY}")
    .POST(HttpRequest.BodyPublishers.ofString(
        "{\"query\": \"ai\"}"
    ))
    .build();

HttpResponse<String> response = client.send(request,
    HttpResponse.BodyHandlers.ofString());
// PHP
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.tengence.com/v1/search");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer {YOUR_API_KEY}'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'query' => 'ai'
]));
$response = curl_exec($ch);
curl_close($ch);
// Go
package main

import (
    "bytes"
    "encoding/json"
    "net/http"
)

func main() {
    data := map[string]string{"query": "ai"}
    jsonData, _ := json.Marshal(data)

    req, _ := http.NewRequest("POST",
        "https://api.tengence.com/v1/search",
        bytes.NewBuffer(jsonData))
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Authorization", "Bearer {YOUR_API_KEY}")

    client := &http.Client{}
    client.Do(req)
}

2. 分页搜索

场景:获取第2页结果,每页20条

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "page": 2,
    "size": 20
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    page: 2,
    size: 20
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'page': 2,
        'size': 20
    }
)

3. 排序 – 价格降序

场景:按价格从高到低排序

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "sortFields": [{"field": "price", "order": "DESC"}]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    sortFields: [{ field: 'price', order: 'DESC' }]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'sortFields': [{'field': 'price', 'order': 'DESC'}]
    }
)

4. 多字段排序

场景:先按评分降序,再按评论数降序

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "sortFields": [
      {"field": "rating", "order": "DESC"},
      {"field": "review_count", "order": "DESC"}
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    sortFields: [
      { field: 'rating', order: 'DESC' },
      { field: 'review_count', order: 'DESC' }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'sortFields': [
            {'field': 'rating', 'order': 'DESC'},
            {'field': 'review_count', 'order': 'DESC'}
        ]
    }
)

5. 聚合 – 分类统计

场景:统计各分类下的内容数量

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "aggregations": [{
      "name": "by_category",
      "field": "category",
      "type": "terms",
      "size": 20,
      "sortBy": "count",
      "sortOrder": "DESC"
    }]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    aggregations: [{
      name: 'by_category',
      field: 'category',
      type: 'terms',
      size: 20,
      sortBy: 'count',
      sortOrder: 'DESC'
    }]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'aggregations': [{
            'name': 'by_category',
            'field': 'category',
            'type': 'terms',
            'size': 20,
            'sortBy': 'count',
            'sortOrder': 'DESC'
        }]
    }
)

6. 聚合 – 价格区间统计

场景:统计不同价格区间的商品分布

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "aggregations": [{
      "name": "by_price_range",
      "field": "price",
      "type": "range",
      "ranges": [
        {"key": "low", "from": 0, "to": 100},
        {"key": "medium", "from": 100, "to": 500},
        {"key": "high", "from": 500, "to": 1000},
        {"key": "luxury", "from": 1000}
      ]
    }]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    aggregations: [{
      name: 'by_price_range',
      field: 'price',
      type: 'range',
      ranges: [
        { key: 'low', from: 0, to: 100 },
        { key: 'medium', from: 100, to: 500 },
        { key: 'high', from: 500, to: 1000 },
        { key: 'luxury', from: 1000 }
      ]
    }]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'aggregations': [{
            'name': 'by_price_range',
            'field': 'price',
            'type': 'range',
            'ranges': [
                {'key': 'low', 'from': 0, 'to': 100},
                {'key': 'medium', 'from': 100, 'to': 500},
                {'key': 'high', 'from': 500, 'to': 1000},
                {'key': 'luxury', 'from': 1000}
            ]
        }]
    }
)

7. 聚合 – 日期趋势(按天)

场景:查看最近30天的每日数量趋势

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "aggregations": [{
      "name": "daily_trend",
      "field": "create_time",
      "type": "date_histogram",
      "interval": "1d"
    }],
    "size": 0
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    aggregations: [{
      name: 'daily_trend',
      field: 'create_time',
      type: 'date_histogram',
      interval: '1d'
    }],
    size: 0
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'aggregations': [{
            'name': 'daily_trend',
            'field': 'create_time',
            'type': 'date_histogram',
            'interval': '1d'
        }],
        'size': 0
    }
)

8. 聚合 – 月份趋势

场景:按月统计(12个月趋势)

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "aggregations": [{
      "name": "monthly_trend",
      "field": "create_time",
      "type": "date_histogram",
      "interval": "1M"
    }]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    aggregations: [{
      name: 'monthly_trend',
      field: 'create_time',
      type: 'date_histogram',
      interval: '1M'
    }]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'aggregations': [{
            'name': 'monthly_trend',
            'field': 'create_time',
            'type': 'date_histogram',
            'interval': '1M'
        }]
    }
)

9. 多聚合组合 – 分类+品牌+价格

场景:搜索时,同时展示分类、品牌、价格区间筛选

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "page": 1,
    "size": 20,
    "aggregations": [
      {
        "name": "by_category",
        "field": "category",
        "type": "terms",
        "size": 10
      },
      {
        "name": "by_brand",
        "field": "brand",
        "type": "terms",
        "size": 10
      },
      {
        "name": "by_price_range",
        "field": "price",
        "type": "range",
        "ranges": [
          {"key": "under_1000", "to": 1000},
          {"key": "1000_3000", "from": 1000, "to": 3000},
          {"key": "3000_5000", "from": 3000, "to": 5000},
          {"key": "above_5000", "from": 5000}
        ]
      }
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    page: 1,
    size: 20,
    aggregations: [
      {
        name: 'by_category',
        field: 'category',
        type: 'terms',
        size: 10
      },
      {
        name: 'by_brand',
        field: 'brand',
        type: 'terms',
        size: 10
      },
      {
        name: 'by_price_range',
        field: 'price',
        type: 'range',
        ranges: [
          { key: 'under_1000', to: 1000 },
          { key: '1000_3000', from: 1000, to: 3000 },
          { key: '3000_5000', from: 3000, to: 5000 },
          { key: 'above_5000', from: 5000 }
        ]
      }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'page': 1,
        'size': 20,
        'aggregations': [
            {
                'name': 'by_category',
                'field': 'category',
                'type': 'terms',
                'size': 10
            },
            {
                'name': 'by_brand',
                'field': 'brand',
                'type': 'terms',
                'size': 10
            },
            {
                'name': 'by_price_range',
                'field': 'price',
                'type': 'range',
                'ranges': [
                    {'key': 'under_1000', 'to': 1000},
                    {'key': '1000_3000', 'from': 1000, 'to': 3000},
                    {'key': '3000_5000', 'from': 3000, 'to': 5000},
                    {'key': 'above_5000', 'from': 5000}
                ]
            }
        ]
    }
)

10. 过滤 – 精确匹配

场景:只显示已发布且属于特定分类的内容

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "filters": [
      {"field": "status", "operator": "eq", "value": "published"},
      {"field": "category", "operator": "eq", "value": "技术"}
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    filters: [
      { field: 'status', operator: 'eq', value: 'published' },
      { field: 'category', operator: 'eq', value: '技术' }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'filters': [
            {'field': 'status', 'operator': 'eq', 'value': 'published'},
            {'field': 'category', 'operator': 'eq', 'value': '技术'}
        ]
    }
)

11. 过滤 – 价格范围

场景:搜索价格在 1000-5000 之间的商品

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "filters": [
      {"field": "price", "operator": "gte", "value": 1000},
      {"field": "price", "operator": "lte", "value": 5000}
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    filters: [
      { field: 'price', operator: 'gte', value: 1000 },
      { field: 'price', operator: 'lte', value: 5000 }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'filters': [
            {'field': 'price', 'operator': 'gte', 'value': 1000},
            {'field': 'price', 'operator': 'lte', 'value': 5000}
        ]
    }
)

12. 过滤 – 日期范围

场景:只搜索 2025 年发布的内容

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "filters": [
      {"field": "create_time", "operator": "gte", "value": "2025-01-01"},
      {"field": "create_time", "operator": "lte", "value": "2025-12-31"}
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    filters: [
      { field: 'create_time', operator: 'gte', value: '2025-01-01' },
      { field: 'create_time', operator: 'lte', value: '2025-12-31' }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'filters': [
            {'field': 'create_time', 'operator': 'gte', 'value': '2025-01-01'},
            {'field': 'create_time', 'operator': 'lte', 'value': '2025-12-31'}
        ]
    }
)

13. 过滤 – 多值匹配 (IN)

场景:搜索多个分类下的内容

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "filters": [
      {"field": "category", "operator": "in", "values": ["机器学习", "深度学习", "自然语言处理"]}
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    filters: [
      { field: 'category', operator: 'in', values: ['机器学习', '深度学习', '自然语言处理'] }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'filters': [
            {'field': 'category', 'operator': 'in', 'values': ['机器学习', '深度学习', '自然语言处理']}
        ]
    }
)

14. 过滤 – 排除值 (NOT IN)

场景:排除特定标签的内容

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "filters": [
      {"field": "tags", "operator": "not_in", "values": ["广告", "推广"]}
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    filters: [
      { field: 'tags', operator: 'not_in', values: ['广告', '推广'] }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'filters': [
            {'field': 'tags', 'operator': 'not_in', 'values': ['广告', '推广']}
        ]
    }
)

15. 过滤 – 大于/小于

场景:搜索评分大于 4.5 的内容

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "filters": [
      {"field": "rating", "operator": "gt", "value": 4.5}
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    filters: [
      { field: 'rating', operator: 'gt', value: 4.5 }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'filters': [
            {'field': 'rating', 'operator': 'gt', 'value': 4.5}
        ]
    }
)

16. 综合过滤 – 电商商品筛选

场景:筛选商品,价格 2000-5000,品牌为指定值,状态为在售

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "page": 1,
    "size": 20,
    "filters": [
      {"field": "price", "operator": "gte", "value": 2000},
      {"field": "price", "operator": "lte", "value": 5000},
      {"field": "brand", "operator": "in", "values": ["Apple", "华为", "HUAWEI"]},
      {"field": "status", "operator": "eq", "value": "on_sale"}
    ],
    "sortFields": [{"field": "sales_count", "order": "DESC"}]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    page: 1,
    size: 20,
    filters: [
      { field: 'price', operator: 'gte', value: 2000 },
      { field: 'price', operator: 'lte', value: 5000 },
      { field: 'brand', operator: 'in', values: ['Apple', '华为', 'HUAWEI'] },
      { field: 'status', operator: 'eq', value: 'on_sale' }
    ],
    sortFields: [{ field: 'sales_count', order: 'DESC' }]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'page': 1,
        'size': 20,
        'filters': [
            {'field': 'price', 'operator': 'gte', 'value': 2000},
            {'field': 'price', 'operator': 'lte', 'value': 5000},
            {'field': 'brand', 'operator': 'in', 'values': ['Apple', '华为', 'HUAWEI']},
            {'field': 'status', 'operator': 'eq', 'value': 'on_sale'}
        ],
        'sortFields': [{'field': 'sales_count', 'order': 'DESC'}]
    }
)

17. 高亮 – 指定高亮字段

场景:在标题和内容中高亮显示搜索词

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "highlightFields": ["title", "content"]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    highlightFields: ['title', 'content']
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'highlightFields': ['title', 'content']
    }
)

18. 字段选择 – 限制搜索字段

场景:只在标题中搜索

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "searchFields": ["title"]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    searchFields: ['title']
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'searchFields': ['title']
    }
)

19. 字段选择 – 限制返回字段

场景:只返回 ID 和标题

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "includeFields": ["id", "title", "create_time"]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    includeFields: ['id', 'title', 'create_time']
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'includeFields': ['id', 'title', 'create_time']
    }
)

20. 电商场景 – 完整功能

场景:商品搜索,包含排序、高亮、聚合等完整功能

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "page": 1,
    "size": 20,
    "sortFields": [{"field": "sales_count", "order": "DESC"}],
    "highlightFields": ["title", "description"],
    "searchFields": ["title", "keywords", "brand"],
    "includeFields": ["id", "title", "price", "brand", "sales_count"],
    "aggregations": [
      {
        "name": "by_brand",
        "field": "brand",
        "type": "terms",
        "size": 10,
        "sortBy": "count",
        "sortOrder": "DESC"
      },
      {
        "name": "by_price",
        "field": "price",
        "type": "range",
        "ranges": [
          {"key": "budget", "to": 2000},
          {"key": "mid_range", "from": 2000, "to": 5000},
          {"key": "premium", "from": 5000}
        ]
      }
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    page: 1,
    size: 20,
    sortFields: [{ field: 'sales_count', order: 'DESC' }],
    highlightFields: ['title', 'description'],
    searchFields: ['title', 'keywords', 'brand'],
    includeFields: ['id', 'title', 'price', 'brand', 'sales_count'],
    aggregations: [
      {
        name: 'by_brand',
        field: 'brand',
        type: 'terms',
        size: 10,
        sortBy: 'count',
        sortOrder: 'DESC'
      },
      {
        name: 'by_price',
        field: 'price',
        type: 'range',
        ranges: [
          { key: 'budget', to: 2000 },
          { key: 'mid_range', from: 2000, to: 5000 },
          { key: 'premium', from: 5000 }
        ]
      }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'page': 1,
        'size': 20,
        'sortFields': [{'field': 'sales_count', 'order': 'DESC'}],
        'highlightFields': ['title', 'description'],
        'searchFields': ['title', 'keywords', 'brand'],
        'includeFields': ['id', 'title', 'price', 'brand', 'sales_count'],
        'aggregations': [
            {
                'name': 'by_brand',
                'field': 'brand',
                'type': 'terms',
                'size': 10,
                'sortBy': 'count',
                'sortOrder': 'DESC'
            },
            {
                'name': 'by_price',
                'field': 'price',
                'type': 'range',
                'ranges': [
                    {'key': 'budget', 'to': 2000},
                    {'key': 'mid_range', 'from': 2000, 'to': 5000},
                    {'key': 'premium', 'from': 5000}
                ]
            }
        ]
    }
)

21. 内容搜索 – 完整功能

场景:内容搜索,按时间排序,支持日期趋势和来源聚合

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "page": 1,
    "size": 10,
    "sortFields": [{"field": "publish_time", "order": "DESC"}],
    "highlightFields": ["title", "summary"],
    "aggregations": [
      {
        "name": "by_date",
        "field": "publish_time",
        "type": "date_histogram",
        "interval": "1w"
      },
      {
        "name": "by_source",
        "field": "source",
        "type": "terms",
        "size": 20
      }
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    page: 1,
    size: 10,
    sortFields: [{ field: 'publish_time', order: 'DESC' }],
    highlightFields: ['title', 'summary'],
    aggregations: [
      {
        name: 'by_date',
        field: 'publish_time',
        type: 'date_histogram',
        interval: '1w'
      },
      {
        name: 'by_source',
        field: 'source',
        type: 'terms',
        size: 20
      }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'page': 1,
        'size': 10,
        'sortFields': [{'field': 'publish_time', 'order': 'DESC'}],
        'highlightFields': ['title', 'summary'],
        'aggregations': [
            {
                'name': 'by_date',
                'field': 'publish_time',
                'type': 'date_histogram',
                'interval': '1w'
            },
            {
                'name': 'by_source',
                'field': 'source',
                'type': 'terms',
                'size': 20
            }
        ]
    }
)

22. 日志分析 – 完整功能

场景:日志分析搜索,按错误类型和服务名聚合

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ERROR",
    "page": 1,
    "size": 50,
    "sortFields": [{"field": "timestamp", "order": "DESC"}],
    "aggregations": [
      {
        "name": "errors_by_hour",
        "field": "timestamp",
        "type": "date_histogram",
        "interval": "1h"
      },
      {
        "name": "errors_by_code",
        "field": "error_code",
        "type": "terms",
        "size": 20
      },
      {
        "name": "errors_by_service",
        "field": "service_name",
        "type": "terms",
        "size": 10
      }
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ERROR',
    page: 1,
    size: 50,
    sortFields: [{ field: 'timestamp', order: 'DESC' }],
    aggregations: [
      {
        name: 'errors_by_hour',
        field: 'timestamp',
        type: 'date_histogram',
        interval: '1h'
      },
      {
        name: 'errors_by_code',
        field: 'error_code',
        type: 'terms',
        size: 20
      },
      {
        name: 'errors_by_service',
        field: 'service_name',
        type: 'terms',
        size: 10
      }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ERROR',
        'page': 1,
        'size': 50,
        'sortFields': [{'field': 'timestamp', 'order': 'DESC'}],
        'aggregations': [
            {
                'name': 'errors_by_hour',
                'field': 'timestamp',
                'type': 'date_histogram',
                'interval': '1h'
            },
            {
                'name': 'errors_by_code',
                'field': 'error_code',
                'type': 'terms',
                'size': 20
            },
            {
                'name': 'errors_by_service',
                'field': 'service_name',
                'type': 'terms',
                'size': 10
            }
        ]
    }
)

23. 实时监控 – 分钟级统计

场景:按分钟统计最近一小时的实时数据

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "aggregations": [{
      "name": "realtime_data",
      "field": "created_at",
      "type": "date_histogram",
      "interval": "1m"
    }],
    "sortFields": [{"field": "created_at", "order": "DESC"}]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    aggregations: [{
      name: 'realtime_data',
      field: 'created_at',
      type: 'date_histogram',
      interval: '1m'
    }],
    sortFields: [{ field: 'created_at', order: 'DESC' }]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'aggregations': [{
            'name': 'realtime_data',
            'field': 'created_at',
            'type': 'date_histogram',
            'interval': '1m'
        }],
        'sortFields': [{'field': 'created_at', 'order': 'DESC'}]
    }
)

24. 综合业务场景

场景:搜索,结合过滤、排序、高亮、聚合等全部功能

curl -X POST "https://api.tengence.com/v1/search" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {YOUR_API_KEY}" \
  -d '{
    "query": "ai",
    "page": 1,
    "size": 20,
    "filters": [
      {"field": "price", "operator": "gte", "value": 2000},
      {"field": "price", "operator": "lte", "value": 5000},
      {"field": "brand", "operator": "in", "values": ["Apple", "华为"]},
      {"field": "status", "operator": "eq", "value": "on_sale"}
    ],
    "sortFields": [
      {"field": "sales_count", "order": "DESC"},
      {"field": "rating", "order": "DESC"}
    ],
    "highlightFields": ["title", "description"],
    "searchFields": ["title", "keywords", "brand"],
    "includeFields": ["id", "title", "price", "brand", "sales_count", "rating"],
    "aggregations": [
      {
        "name": "by_brand",
        "field": "brand",
        "type": "terms",
        "size": 10
      },
      {
        "name": "by_price_range",
        "field": "price",
        "type": "range",
        "ranges": [
          {"key": "budget", "to": 3000},
          {"key": "mid_range", "from": 3000, "to": 4000},
          {"key": "premium", "from": 4000}
        ]
      }
    ]
  }'
// JavaScript
const response = await fetch('https://api.tengence.com/v1/search', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer {YOUR_API_KEY}'
  },
  body: JSON.stringify({
    query: 'ai',
    page: 1,
    size: 20,
    filters: [
      { field: 'price', operator: 'gte', value: 2000 },
      { field: 'price', operator: 'lte', value: 5000 },
      { field: 'brand', operator: 'in', values: ['Apple', '华为'] },
      { field: 'status', operator: 'eq', value: 'on_sale' }
    ],
    sortFields: [
      { field: 'sales_count', order: 'DESC' },
      { field: 'rating', order: 'DESC' }
    ],
    highlightFields: ['title', 'description'],
    searchFields: ['title', 'keywords', 'brand'],
    includeFields: ['id', 'title', 'price', 'brand', 'sales_count', 'rating'],
    aggregations: [
      {
        name: 'by_brand',
        field: 'brand',
        type: 'terms',
        size: 10
      },
      {
        name: 'by_price_range',
        field: 'price',
        type: 'range',
        ranges: [
          { key: 'budget', to: 3000 },
          { key: 'mid_range', from: 3000, to: 4000 },
          { key: 'premium', from: 4000 }
        ]
      }
    ]
  })
});
# Python
response = requests.post(
    'https://api.tengence.com/v1/search',
    headers={
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {YOUR_API_KEY}'
    },
    json={
        'query': 'ai',
        'page': 1,
        'size': 20,
        'filters': [
            {'field': 'price', 'operator': 'gte', 'value': 2000},
            {'field': 'price', 'operator': 'lte', 'value': 5000},
            {'field': 'brand', 'operator': 'in', 'values': ['Apple', '华为']},
            {'field': 'status', 'operator': 'eq', 'value': 'on_sale'}
        ],
        'sortFields': [
            {'field': 'sales_count', 'order': 'DESC'},
            {'field': 'rating', 'order': 'DESC'}
        ],
        'highlightFields': ['title', 'description'],
        'searchFields': ['title', 'keywords', 'brand'],
        'includeFields': ['id', 'title', 'price', 'brand', 'sales_count', 'rating'],
        'aggregations': [
            {
                'name': 'by_brand',
                'field': 'brand',
                'type': 'terms',
                'size': 10
            },
            {
                'name': 'by_price_range',
                'field': 'price',
                'type': 'range',
                'ranges': [
                    {'key': 'budget', 'to': 3000},
                    {'key': 'mid_range', 'from': 3000, 'to': 4000},
                    {'key': 'premium', 'from': 4000}
                ]
            }
        ]
    }
)

常见问题

Q: 如何获取 API Key?

登录 Tengence 控制台 (https://console.tengence.com),在「应用详情」页中获取 API Key。


Q: 日期字段支持哪些格式?

日期字段支持多种格式:


Q: 分页参数如何设置?

使用 page 和 size 参数:


Q: 如何同时使用多个过滤条件?

在 filters 数组中添加多个过滤对象,多个条件之间是 AND 关系。


Q: 聚合结果如何排序?

对于 terms 聚合,可以使用 sortBy 和 sortOrder 参数:


Q: 搜索不到结果怎么办?

请检查:

  1. 搜索关键词是否正确
  2. 索引中是否有相关数据
  3. 过滤条件是否过于严格
  4. 字段名是否正确(使用下划线命名法)

Q: 如何限制搜索的字段范围?

使用 searchFields 参数指定要搜索的字段列表。


Q: 如何只返回需要的字段?

使用 includeFields 参数指定要返回的字段列表。


相关资源


下一篇: 通智云数据中心使用说明:API 推送、MySQL 与行为数据集接入 →
咨询
二维码

企业微信