索引概念 #

什么是索引? #

索引(Index)是Algolia中存储和搜索数据的核心容器。可以将其理解为一个数据库表,用于存储结构化的JSON记录。

索引的作用 #

text
┌─────────────────────────────────────────────────┐
│                  Application                     │
├─────────────────────────────────────────────────┤
│                                                  │
│  ┌─────────────┐  ┌─────────────┐               │
│  │   Index     │  │   Index     │               │
│  │  products   │  │   users     │               │
│  │             │  │             │               │
│  │  Record 1   │  │  Record 1   │               │
│  │  Record 2   │  │  Record 2   │               │
│  │  Record 3   │  │  Record 3   │               │
│  │    ...      │  │    ...      │               │
│  └─────────────┘  └─────────────┘               │
│                                                  │
└─────────────────────────────────────────────────┘

索引结构 #

记录结构 #

每条记录是一个JSON对象:

json
{
  "objectID": "unique-id-123",
  "name": "iPhone 15 Pro",
  "brand": "Apple",
  "price": 999,
  "category": "Smartphones",
  "description": "Latest iPhone with A17 Pro chip",
  "image": "https://example.com/iphone.jpg",
  "stock": 50,
  "rating": 4.8,
  "tags": ["phone", "apple", "5g"]
}

objectID的重要性 #

objectID 是记录的唯一标识符:

特性 说明
必需性 每条记录必须有objectID
唯一性 同一索引内不能重复
类型 字符串或数字
用途 更新、删除记录的依据
javascript
// 自动生成objectID
const { objectIDs } = await index.saveObjects([
  { name: "Product 1" }  // Algolia会自动生成objectID
]);

// 手动指定objectID
await index.saveObjects([
  { objectID: "prod-001", name: "Product 1" }
]);

创建索引 #

通过Dashboard创建 #

  1. 登录Algolia Dashboard
  2. 点击左侧"Indices"
  3. 点击"Create Index"
  4. 输入索引名称

通过API创建 #

javascript
const algoliasearch = require('algoliasearch');
const client = algoliasearch('APP_ID', 'ADMIN_KEY');

// 创建索引(添加第一条数据时自动创建)
const index = client.initIndex('products');

await index.saveObjects([
  { objectID: '1', name: 'First Product' }
]);

索引命名规范 #

text
推荐命名格式:
├── products           # 生产环境
├── products_dev       # 开发环境
├── products_staging   # 预发布环境
├── products_backup    # 备份索引
└── products_v2        # 版本化索引

命名规则:
- 只能包含字母、数字、下划线
- 不能以数字开头
- 长度限制:1-100字符
- 区分大小写

索引设置 #

基本设置 #

javascript
await index.setSettings({
  // 可搜索属性
  searchableAttributes: ['name', 'description', 'brand'],
  
  // 分面属性
  attributesForFaceting: ['brand', 'category', 'price'],
  
  // 自定义排名
  customRanking: ['desc(rating)', 'desc(popularity)'],
  
  // 每页结果数
  hitsPerPage: 20,
  
  // 高亮属性
  attributesToHighlight: ['name', 'description'],
  
  // 代码片段属性
  attributesToSnippet: ['description:50'],
  
  // 不返回的属性
  unretrievableAttributes: ['internal_id']
});

searchableAttributes详解 #

定义哪些字段可以被搜索:

javascript
// 优先级从高到低
searchableAttributes: [
  'name',           // 最高优先级
  'description',    // 中等优先级
  'brand',          // 较低优先级
  'category'
]

// 使用 unordered 前缀取消优先级
searchableAttributes: [
  'name',
  'unordered(description)',  // 不考虑词的位置
  'unordered(brand)'
]

attributesForFaceting详解 #

定义可用于筛选和分面的字段:

javascript
attributesForFaceting: [
  'brand',                    // 可筛选可分面
  'category',                 // 可筛选可分面
  'filterOnly(price)',        // 仅可筛选
  'searchable(tags)'          // 可搜索的标签
]

排名设置 #

javascript
// 默认排名公式
ranking: [
  'typo',       // 拼写错误数量
  'geo',        // 地理距离
  'words',      // 匹配词数量
  'filters',    // 过滤器匹配
  'proximity',  // 词的接近度
  'attribute',  // 属性优先级
  'exact',      // 精确匹配度
  'custom'      // 自定义排名
]

// 自定义排名规则
customRanking: [
  'desc(rating)',        // 评分降序
  'desc(popularity)',    // 人气降序
  'asc(price)'           // 价格升序
]

索引操作 #

添加数据 #

javascript
// 单条添加
await index.saveObject({
  objectID: '1',
  name: 'Product 1'
});

// 批量添加
await index.saveObjects([
  { objectID: '1', name: 'Product 1' },
  { objectID: '2', name: 'Product 2' },
  { objectID: '3', name: 'Product 3' }
]);

// 部分更新
await index.partialUpdateObject({
  objectID: '1',
  price: 899  // 只更新price字段
});

删除数据 #

javascript
// 删除单条
await index.deleteObject('1');

// 批量删除
await index.deleteObjects(['1', '2', '3']);

// 按条件删除
await index.deleteBy({
  filters: 'stock:0'  // 删除库存为0的记录
});

// 清空索引
await index.clearObjects();

替换数据 #

javascript
// replaceAllObjects 会先创建临时索引,然后替换
await index.replaceAllObjects([
  { objectID: '1', name: 'New Product 1' },
  { objectID: '2', name: 'New Product 2' }
], {
  safe: true  // 确保操作安全
});

批量操作 #

批量请求 #

javascript
const { algoliasearch } = require('algoliasearch');

const client = algoliasearch('APP_ID', 'ADMIN_KEY');
const index = client.initIndex('products');

// 批量操作
await index.batch({
  requests: [
    { action: 'addObject', body: { name: 'Product 1' } },
    { action: 'updateObject', body: { objectID: '2', name: 'Updated' } },
    { action: 'deleteObject', objectID: '3' },
    { action: 'partialUpdateObject', body: { objectID: '4', price: 100 } }
  ]
});

操作类型 #

Action 说明
addObject 添加记录
updateObject 更新记录(完全替换)
partialUpdateObject 部分更新
deleteObject 删除记录
clear 清空索引

索引复制 #

复制索引 #

javascript
// 复制索引(包括设置和数据)
await client.copyIndex('source_index', 'target_index');

// 只复制设置
await client.copySettings('source_index', 'target_index');

// 只复制规则
await client.copyRules('source_index', 'target_index');

// 只复制同义词
await client.copySynonyms('source_index', 'target_index');

使用场景 #

text
生产环境索引管理:
├── products_prod (主索引)
├── products_staging (预发布)
│   └── 复制自 products_prod
└── products_backup (备份)
    └── 复制自 products_prod

索引监控 #

获取索引统计 #

javascript
// 获取索引统计信息
const stats = await index.getStats();

console.log(stats);
// {
//   numberOfRecords: 10000,
//   dataSize: 5242880,
//   lastBuildTimeS: 2.5,
//   isPending: false
// }

监控指标 #

指标 说明
numberOfRecords 记录数量
dataSize 数据大小(字节)
lastBuildTimeS 最后构建时间
isPending 是否有待处理任务

任务管理 #

获取任务状态 #

javascript
// 操作返回taskID
const { taskID } = await index.saveObject({ ... });

// 等待任务完成
await index.waitTask(taskID);

// 获取任务状态
const status = await index.getTask(taskID);
console.log(status.status); // 'published' 或 'notPublished'

任务状态 #

状态 说明
notPublished 待处理
published 已完成
failed 失败

索引生命周期 #

索引状态 #

text
创建 → 配置 → 导入数据 → 搜索 → 更新 → 删除
  │       │        │        │       │       │
  └───────┴────────┴────────┴───────┴───────┘
                    可循环

最佳实践 #

javascript
// 1. 先配置索引设置
await index.setSettings({
  searchableAttributes: ['name', 'description'],
  customRanking: ['desc(rating)']
});

// 2. 再导入数据
await index.saveObjects(products);

// 3. 等待索引构建完成
await index.waitTask(taskID);

// 4. 开始搜索
const results = await index.search('query');

索引限制 #

限制项 免费版 付费版
记录数 10,000 根据套餐
索引数 无限制 无限制
记录大小 10KB 20KB
属性数 1000 1000

总结 #

索引是Algolia的核心概念:

要点 说明
结构 JSON记录集合
标识 objectID唯一标识
配置 searchableAttributes、customRanking等
操作 增删改查、批量操作
管理 复制、监控、任务管理

接下来,让我们学习 记录与字段

最后更新:2026-03-28