文档索引 #

一、索引概述 #

1.1 什么是索引 #

索引是将文档添加到Solr的过程,包括:

  • 解析文档
  • 分析字段
  • 构建倒排索引
  • 存储文档

1.2 索引流程 #

text
文档数据
    ↓
解析(JSON/XML/CSV)
    ↓
字段分析(Analyzer)
    ↓
索引写入(IndexWriter)
    ↓
提交(Commit)
    ↓
搜索可见

二、JSON格式索引 #

2.1 单文档索引 #

bash
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "book-001",
    "title": "Solr实战指南",
    "author": "张三",
    "price": 99.0,
    "category": ["技术", "搜索"],
    "publish_date": "2026-03-27T00:00:00Z"
  }'

2.2 批量索引 #

bash
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '[
    {
      "id": "book-001",
      "title": "Solr实战指南",
      "author": "张三",
      "price": 99.0
    },
    {
      "id": "book-002",
      "title": "Elasticsearch权威指南",
      "author": "李四",
      "price": 128.0
    },
    {
      "id": "book-003",
      "title": "Lucene实战",
      "author": "王五",
      "price": 89.0
    }
  ]'

2.3 使用文件索引 #

bash
# 创建JSON文件
cat > books.json << 'EOF'
[
  {"id": "book-001", "title": "Solr实战指南", "price": 99.0},
  {"id": "book-002", "title": "Elasticsearch权威指南", "price": 128.0}
]
EOF

# 使用post工具
bin/post -c mycore books.json

# 使用curl
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
  -H "Content-Type: application/json" \
  --data-binary @books.json

2.4 JSONL格式 #

bash
# JSONL格式(每行一个JSON)
cat > books.jsonl << 'EOF'
{"id": "book-001", "title": "Solr实战指南", "price": 99.0}
{"id": "book-002", "title": "Elasticsearch权威指南", "price": 128.0}
{"id": "book-003", "title": "Lucene实战", "price": 89.0}
EOF

curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
  -H "Content-Type: application/x-ndjson" \
  --data-binary @books.jsonl

三、XML格式索引 #

3.1 单文档索引 #

bash
curl -X POST "http://localhost:8983/solr/mycore/update" \
  -H "Content-Type: application/xml" \
  -d '<add>
    <doc>
      <field name="id">book-001</field>
      <field name="title">Solr实战指南</field>
      <field name="author">张三</field>
      <field name="price">99.0</field>
    </doc>
  </add>'

3.2 批量索引 #

bash
curl -X POST "http://localhost:8983/solr/mycore/update" \
  -H "Content-Type: application/xml" \
  -d '<add>
    <doc>
      <field name="id">book-001</field>
      <field name="title">Solr实战指南</field>
      <field name="price">99.0</field>
    </doc>
    <doc>
      <field name="id">book-002</field>
      <field name="title">Elasticsearch权威指南</field>
      <field name="price">128.0</field>
    </doc>
  </add>'

3.3 XML属性语法 #

xml
<add>
  <doc>
    <field name="id" boost="2.0">book-001</field>
    <field name="title">Solr实战指南</field>
    <field name="category">技术</field>
    <field name="category">搜索</field>
  </doc>
</add>

四、CSV格式索引 #

4.1 基础CSV索引 #

bash
# 创建CSV文件
cat > books.csv << 'EOF'
id,title,author,price
book-001,Solr实战指南,张三,99.0
book-002,Elasticsearch权威指南,李四,128.0
book-003,Lucene实战,王五,89.0
EOF

# 索引CSV
curl -X POST "http://localhost:8983/solr/mycore/update/csv" \
  -H "Content-Type: text/csv" \
  --data-binary @books.csv

4.2 CSV参数 #

bash
curl -X POST "http://localhost:8983/solr/mycore/update/csv" \
  -d "separator=%2C" \
  -d "fieldnames=id,title,author,price" \
  -d "skip=1" \
  -d "skipLines=1" \
  --data-binary @books.csv

4.3 CSV参数说明 #

参数 说明
separator 分隔符(默认逗号)
fieldnames 字段名列表
skip 跳过字段
skipLines 跳过行数
encapsulator 引用符
escape 转义符
trim 是否去除空白

五、使用post工具 #

5.1 基本用法 #

bash
# 索引文件
bin/post -c mycore example/exampledocs/books.json

# 索引目录
bin/post -c mycore /path/to/documents/

# 索引多个文件
bin/post -c mycore file1.json file2.xml file3.csv

5.2 常用参数 #

bash
# 指定格式
bin/post -c mycore -format json data.json

# 指定类型
bin/post -c mycore -type application/json data.txt

# 自动提交
bin/post -c mycore -commit data.json

# 参数传递
bin/post -c mycore -params "commit=true" data.json

六、提交策略 #

6.1 硬提交(Hard Commit) #

bash
# 索引后立即硬提交
curl -X POST "http://localhost:8983/solr/mycore/update?commit=true" \
  -H "Content-Type: application/json" \
  -d '{"id": "book-001", "title": "Solr实战指南"}'

# 单独提交
curl -X POST "http://localhost:8983/solr/mycore/update?commit=true"

硬提交特点:

  • 数据持久化到磁盘
  • 打开新的IndexSearcher
  • 搜索可见
  • 性能开销较大

6.2 软提交(Soft Commit) #

bash
# 软提交
curl -X POST "http://localhost:8983/solr/mycore/update?softCommit=true"

软提交特点:

  • 数据在内存中可见
  • 不持久化到磁盘
  • 近实时搜索(NRT)
  • 性能开销小

6.3 自动提交配置 #

在solrconfig.xml中配置:

xml
<updateHandler class="solr.DirectUpdateHandler2">
  <!-- 硬提交 -->
  <autoCommit>
    <maxTime>15000</maxTime>
    <openSearcher>false</openSearcher>
  </autoCommit>
  
  <!-- 软提交 -->
  <autoSoftCommit>
    <maxTime>1000</maxTime>
  </autoSoftCommit>
</updateHandler>

6.4 提交策略选择 #

场景 推荐策略
实时搜索 软提交 + 定期硬提交
批量导入 手动硬提交
高写入量 自动提交
低延迟要求 软提交

七、索引选项 #

7.1 覆盖现有文档 #

bash
# 默认行为:覆盖相同ID的文档
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '{"id": "book-001", "title": "更新后的标题"}'

7.2 增量更新 #

bash
# 部分字段更新
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "book-001",
    "title": {"set": "新标题"},
    "price": {"inc": 10}
  }'

7.3 文档权重 #

xml
<add>
  <doc boost="2.5">
    <field name="id">book-001</field>
    <field name="title" boost="3.0">Solr实战指南</field>
  </doc>
</add>

八、批量索引优化 #

8.1 批量大小 #

bash
# 推荐批量大小:100-1000文档
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '[...100文档...]'

8.2 并行索引 #

bash
# 使用多个线程并行索引
for i in {1..10}; do
  curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
    -H "Content-Type: application/json" \
    -d @batch_$i.json &
done
wait

8.3 索引时优化配置 #

xml
<indexConfig>
  <!-- 禁用索引时合并 -->
  <useCompoundFile>false</useCompoundFile>
  
  <!-- 增大缓冲区 -->
  <ramBufferSizeMB>256</ramBufferSizeMB>
  
  <!-- 禁用索引时优化 -->
  <mergePolicy class="org.apache.lucene.index.TieredMergePolicy">
    <int name="maxMergeAtOnce">10</int>
  </mergePolicy>
</indexConfig>

九、索引监控 #

9.1 查看索引统计 #

bash
curl "http://localhost:8983/solr/mycore/admin/stats?key=updateHandler"

9.2 查看索引大小 #

bash
# 查看索引目录大小
du -sh server/solr/mycore/data/index/

# 查看文档数量
curl "http://localhost:8983/solr/mycore/select?q=*:*&rows=0"

9.3 查看事务日志 #

bash
ls -la server/solr/mycore/data/tlog/

十、索引错误处理 #

10.1 错误响应 #

json
{
  "responseHeader": {
    "status": 400,
    "QTime": 5
  },
  "error": {
    "msg": "Document is missing mandatory uniqueKey field: id",
    "code": 400
  }
}

10.2 常见错误 #

错误 原因 解决方案
缺少唯一键 未提供id字段 确保文档包含id
字段类型错误 值类型不匹配 检查字段类型
字段不存在 Schema未定义 添加字段定义
内存不足 批量过大 减小批量大小

10.3 错误处理策略 #

bash
# 记录失败文档
curl -X POST "http://localhost:8983/solr/mycore/update/json/docs" \
  -H "Content-Type: application/json" \
  -d @docs.json 2>&1 | tee errors.log

十一、实战示例 #

11.1 电商商品索引 #

bash
curl -X POST "http://localhost:8983/solr/products/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "product-001",
    "name": "iPhone 15 Pro",
    "brand": "Apple",
    "category": ["手机", "电子产品"],
    "price": 8999.00,
    "stock": 100,
    "description": "最新款iPhone",
    "tags": ["5G", "A17芯片", "钛金属"],
    "rating": 4.8,
    "sales": 10000,
    "created_at": "2026-03-27T00:00:00Z",
    "updated_at": "2026-03-27T10:00:00Z"
  }'

11.2 文章索引 #

bash
curl -X POST "http://localhost:8983/solr/articles/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "article-001",
    "title": "Solr性能优化指南",
    "author": "张三",
    "content": "本文介绍Solr性能优化技巧...",
    "tags": ["Solr", "性能优化", "搜索引擎"],
    "category": "技术",
    "views": 1000,
    "likes": 50,
    "publish_date": "2026-03-27T00:00:00Z",
    "status": "published"
  }'

11.3 日志索引 #

bash
curl -X POST "http://localhost:8983/solr/logs/update/json/docs" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "log-001",
    "level": "ERROR",
    "message": "Database connection failed",
    "host": "server-001",
    "service": "user-service",
    "timestamp": "2026-03-27T10:00:00Z",
    "stack_trace": "java.sql.SQLException...",
    "request_id": "req-12345"
  }'

十二、总结 #

索引方式对比:

方式 优点 缺点
JSON 简单易用 大文件内存占用高
XML 功能完整 格式冗长
CSV 批量导入快 不支持嵌套
post工具 方便快捷 依赖命令行

最佳实践:

  • 使用JSON格式索引
  • 合理设置批量大小
  • 配置自动提交策略
  • 监控索引性能
  • 处理索引错误

下一步,让我们学习文档更新!

最后更新:2026-03-27