索引设计原则 #

设计原则概述 #

良好的索引设计是高效搜索的基础:

  • 简单性:保持数据结构简洁
  • 相关性:确保搜索结果相关
  • 性能:优化搜索响应速度
  • 可维护:便于更新和维护

数据结构设计 #

扁平化优先 #

javascript
// ❌ 避免:深层嵌套
{
  "product": {
    "details": {
      "info": {
        "name": "iPhone"
      }
    }
  }
}

// ✅ 推荐:扁平结构
{
  "name": "iPhone",
  "brand": "Apple",
  "price": 999
}

合理的字段数量 #

javascript
// ✅ 推荐:核心字段
{
  "objectID": "prod-001",
  "name": "iPhone 15 Pro",
  "brand": "Apple",
  "price": 999,
  "category": "Smartphones",
  "image": "https://...",
  "description": "..."
}

// 建议:总字段数不超过50个

预计算值 #

javascript
// ❌ 避免:搜索时计算
{
  "price": 100,
  "discount": 10
}
// 搜索时: filters: 'price - discount < 100'

// ✅ 推荐:预计算
{
  "price": 100,
  "discount": 10,
  "finalPrice": 90,
  "discountPercent": 10
}

字段分类 #

可搜索字段 #

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

分面字段 #

javascript
attributesForFaceting: [
  'brand',
  'category',
  'price_range',
  'inStock'
]

排名字段 #

javascript
customRanking: [
  'desc(rating)',
  'desc(popularity)',
  'asc(price)'
]

展示字段 #

javascript
attributesToRetrieve: [
  'name',
  'price',
  'image',
  'brand'
]

索引策略 #

按环境分离 #

text
products_prod      # 生产环境
products_staging   # 预发布环境
products_dev       # 开发环境

按语言分离 #

text
products_en        # 英文
products_zh        # 中文
products_ja        # 日文

按类型分离 #

text
products           # 商品
articles           # 文章
users              # 用户

数据更新策略 #

增量更新 #

javascript
// 单条更新
await index.partialUpdateObject({
  objectID: 'prod-001',
  price: 899,
  stock: 45
});

// 批量更新
await index.partialUpdateObjects(updates);

批量操作 #

javascript
// 分批处理大量数据
async function batchUpdate(objects, batchSize = 1000) {
  for (let i = 0; i < objects.length; i += batchSize) {
    const batch = objects.slice(i, i + batchSize);
    await index.saveObjects(batch);
  }
}

索引配置模板 #

电商索引 #

javascript
{
  searchableAttributes: [
    'name',
    'brand',
    'category',
    'description'
  ],
  attributesForFaceting: [
    'brand',
    'category',
    'price_range',
    'color',
    'size',
    'inStock'
  ],
  customRanking: [
    'desc(popularity)',
    'desc(rating)',
    'asc(price)'
  ],
  attributesToHighlight: ['name', 'description'],
  attributesToSnippet: ['description:100'],
  hitsPerPage: 20
}

内容索引 #

javascript
{
  searchableAttributes: [
    'title',
    'content',
    'tags',
    'author.name'
  ],
  attributesForFaceting: [
    'category',
    'tags',
    'author.id',
    'status'
  ],
  customRanking: [
    'desc(publishedAt)',
    'desc(viewCount)'
  ],
  attributesToHighlight: ['title', 'content'],
  attributesToSnippet: ['content:200'],
  removeStopWords: true,
  ignorePlurals: true
}

性能优化 #

限制返回字段 #

javascript
// ✅ 只返回需要的字段
attributesToRetrieve: ['name', 'price', 'image']

使用filterOnly #

javascript
// 不需要统计的分面使用filterOnly
attributesForFaceting: [
  'brand',
  'filterOnly(internal_id)'
]

合理设置分页 #

javascript
// ✅ 合理的每页数量
hitsPerPage: 20

// ❌ 避免过大的每页数量
hitsPerPage: 1000  // 影响性能

总结 #

索引设计要点:

要点 说明
扁平化 避免深层嵌套
预计算 提前计算复杂值
分类 搜索、分面、排名、展示
分离 按环境、语言、类型
优化 限制字段、合理分页

接下来,让我们学习 性能优化

最后更新:2026-03-28