Solr教程:练习三 - 索引您自己的数据

本练习将指导您使用自己的数据构建Solr搜索应用,这是将Solr应用于实际项目的关键一步。

关键目标

  • 选择适合的个人数据集进行索引
  • 为特定数据需求准备Solr配置
  • 考虑重要的规划问题
  • 选择合适的索引方法

规划阶段的重要问题

在开始之前,请思考以下关键问题:

1. 数据类型分析

  • 您将索引什么类型的数据?(文档、产品信息、日志、用户数据等)
  • 数据的格式是什么?(JSON、XML、CSV、PDF、Word文档等)
  • 数据量有多大?更新频率如何?

2. 字段和分析规则

  • 需要哪些字段?(标题、内容、日期、分类等)
  • 哪些字段需要搜索?哪些只需要存储?
  • 需要什么样的文本分析?(分词、同义词、拼音等)

3. 搜索功能需求

  • 用户将如何搜索?(关键词、短语、高级搜索)
  • 需要哪些搜索特性?(高亮、拼写检查、自动建议)
  • 如何排序和过滤结果?

4. 测试要求

  • 如何验证搜索结果的准确性?
  • 需要多少测试数据?
  • 性能要求是什么?

索引方法选择

方法1:使用bin/solr post工具索引本地文件

最简单的方式,适合快速原型开发:

1
2
3
4
5
# 创建集合
bin/solr create -c localDocs

# 索引本地目录中的文件
bin/solr post -c localDocs ~/Documents

支持的文件格式:

  • JSON、XML、CSV
  • HTML、PDF
  • Microsoft Office文档(Word、Excel、PowerPoint)
  • 纯文本文件

示例:索引特定类型的文件

1
2
3
4
5
# 只索引PDF文件
bin/solr post -c localDocs ~/Documents -filetypes pdf

# 索引JSON数据
bin/solr post -c localDocs data.json

方法2:使用SolrJ(Java客户端)

适合程序化控制和批量处理:

1
2
3
4
5
6
7
8
9
10
// 示例代码结构
SolrClient solr = new HttpSolrClient.Builder("http://localhost:8983/solr/localDocs").build();

SolrInputDocument document = new SolrInputDocument();
document.addField("id", "1");
document.addField("title", "我的文档标题");
document.addField("content", "文档内容...");

solr.add(document);
solr.commit();

优势:

  • 完全的程序控制
  • 批量操作优化
  • 错误处理机制
  • 与现有Java应用集成

方法3:使用Admin UI的文档界面

适合测试和小批量数据:

  1. 访问 http://localhost:8983/solr
  2. 选择您的集合
  3. 点击”Documents”
  4. 选择文档格式(JSON、XML、CSV)
  5. 输入或粘贴文档数据
  6. 提交索引

示例JSON文档:

1
2
3
4
5
6
7
8
{
"id": "doc1",
"title": "示例文档",
"author": "张三",
"content": "这是文档的主要内容...",
"category": ["技术", "教程"],
"publish_date": "2024-01-15T10:30:00Z"
}

实践示例:索引博客文章

假设您要为博客构建搜索功能:

步骤1:创建专门的集合

1
bin/solr create -c myblog

步骤2:定义模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 添加标题字段
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field": {
"name": "title",
"type": "text_general",
"stored": true,
"indexed": true
}
}' http://localhost:8983/solr/myblog/schema

# 添加作者字段
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field": {
"name": "author",
"type": "string",
"stored": true,
"indexed": true
}
}' http://localhost:8983/solr/myblog/schema

# 添加发布日期字段
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field": {
"name": "publish_date",
"type": "pdate",
"stored": true,
"indexed": true
}
}' http://localhost:8983/solr/myblog/schema

步骤3:索引数据

创建blog_posts.json文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[
{
"id": "1",
"title": "Solr入门教程",
"author": "技术博主",
"content": "本文介绍Solr的基本概念...",
"tags": ["搜索", "Solr", "教程"],
"publish_date": "2024-01-15T10:00:00Z"
},
{
"id": "2",
"title": "高级搜索技巧",
"author": "技术博主",
"content": "深入探讨Solr的高级功能...",
"tags": ["高级", "优化", "性能"],
"publish_date": "2024-01-20T14:30:00Z"
}
]

索引文件:

1
bin/solr post -c myblog blog_posts.json

重要注意事项

唯一键字段

  • 使用唯一键字段(通常是”id”)防止文档重复
  • 重新索引相同id的文档会更新原有文档

删除文档

1
2
3
4
5
6
7
8
9
# 通过ID删除
curl -X POST -H 'Content-type:application/json' --data-binary '{
"delete": {"id": "1"}
}' http://localhost:8983/solr/myblog/update?commit=true

# 通过查询删除
curl -X POST -H 'Content-type:application/json' --data-binary '{
"delete": {"query": "author:测试用户"}
}' http://localhost:8983/solr/myblog/update?commit=true

迭代优化

  • 从简单模式开始,逐步优化
  • 测试不同的字段类型和分析器
  • 监控查询性能和相关性
  • 根据用户反馈调整配置

推荐步骤总结

  1. 创建集合

    1
    bin/solr create -c mydata
  2. 选择索引方法

    • 快速测试:使用post工具
    • 生产环境:使用SolrJ或API
    • 小批量测试:使用Admin UI
  3. 测试和优化

    • 索引样本数据
    • 测试搜索功能
    • 调整字段和分析器
    • 优化性能
  4. 扩展功能

    • 添加分面搜索
    • 实现自动建议
    • 配置同义词
    • 设置高亮显示

下一步行动

  • 选择您的数据集
  • 规划字段结构
  • 开始小规模测试
  • 逐步扩展功能
  • 参考Solr文档深入学习

相关资源

© 2025 Solr Community of China All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero