Solr入门:5分钟快速上手

这个练习将指导您在短短5分钟内开始使用Solr!通过这个快速入门教程,您将体验到Solr的核心功能。

启动Solr云模式

启动第一个节点

要启动Solr,请运行:

  • Unix或MacOSbin/solr start -c
  • Windowsbin\solr.cmd start -c

参数 -c 表示以SolrCloud模式启动,这是Solr的分布式模式。

启动第二个节点(可选)

要启动另一个Solr节点并让它加入第一个节点组成集群:

1
$ bin/solr start -c -z localhost:9983 -p 8984

参数说明:

  • -c:以SolrCloud模式启动
  • -z localhost:9983:连接到ZooKeeper(第一个节点自动启动的)
  • -p 8984:使用端口8984(避免与第一个节点的8983端口冲突)

创建集合

就像数据库系统将数据保存在表中一样,Solr将数据保存在集合中。可以按以下方式创建集合:

1
2
3
4
5
6
7
8
$ curl --request POST \
--url http://localhost:8983/api/collections \
--header 'Content-Type: application/json' \
--data '{
"name": "techproducts",
"numShards": 1,
"replicationFactor": 1
}'

参数解释:

  • name:集合名称为”techproducts”
  • numShards:分片数量为1
  • replicationFactor:副本因子为1(每个分片有1个副本)

定义模式

让我们定义文档将包含的一些字段。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ curl --request POST \
--url http://localhost:8983/api/collections/techproducts/schema \
--header 'Content-Type: application/json' \
--data '{
"add-field": [
{"name": "name", "type": "text_general", "multiValued": false},
{"name": "cat", "type": "string", "multiValued": true},
{"name": "manu", "type": "string"},
{"name": "features", "type": "text_general", "multiValued": true},
{"name": "weight", "type": "pfloat"},
{"name": "price", "type": "pfloat"},
{"name": "popularity", "type": "pint"},
{"name": "inStock", "type": "boolean", "stored": true},
{"name": "store", "type": "location"}
]
}'

字段定义说明

字段名 类型 说明 多值
name text_general 产品名称,支持全文搜索
cat string 产品类别
manu string 制造商
features text_general 产品特性描述
weight pfloat 重量(浮点数)
price pfloat 价格(浮点数)
popularity pint 受欢迎程度(整数)
inStock boolean 是否有库存
store location 商店位置

索引文档

索引单个文档

可以按以下方式索引单个文档:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ curl --request POST \
--url 'http://localhost:8983/api/collections/techproducts/update' \
--header 'Content-Type: application/json' \
--data '{
"id" : "978-0641723445",
"cat" : ["book","hardcover"],
"name" : "The Lightning Thief",
"author" : "Rick Riordan",
"series_t" : "Percy Jackson and the Olympians",
"sequence_i" : 1,
"genre_s" : "fantasy",
"inStock" : true,
"price" : 12.50,
"pages_i" : 384
}'

索引多个文档

可以在同一个请求中索引多个文档:

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 --request POST \
--url 'http://localhost:8983/api/collections/techproducts/update' \
--header 'Content-Type: application/json' \
--data '[
{
"id" : "978-0641723445",
"cat" : ["book","hardcover"],
"name" : "The Lightning Thief",
"author" : "Rick Riordan",
"series_t" : "Percy Jackson and the Olympians",
"sequence_i" : 1,
"genre_s" : "fantasy",
"inStock" : true,
"price" : 12.50,
"pages_i" : 384
},
{
"id" : "978-1423103349",
"cat" : ["book","paperback"],
"name" : "The Sea of Monsters",
"author" : "Rick Riordan",
"series_t" : "Percy Jackson and the Olympians",
"sequence_i" : 2,
"genre_s" : "fantasy",
"inStock" : true,
"price" : 6.49,
"pages_i" : 304
}
]'

从文件索引文档

可以按以下方式索引包含文档的文件:

1
2
3
4
$ curl -H "Content-Type: application/json" \
-X POST \
-d @example/exampledocs/books.json \
--url 'http://localhost:8983/api/collections/techproducts/update?commit=true'

注意URL中的?commit=true参数,它会在索引完成后立即提交更改。

提交更改

文档索引到集合后,并不会立即可供搜索。为了使它们可搜索,需要执行提交操作(在其他搜索引擎如OpenSearch等中也称为refresh)。

手动提交

可以手动提交更改:

1
2
3
4
$ curl --request POST \
--url 'http://localhost:8983/api/collections/techproducts/update' \
--header 'Content-Type: application/json' \
--data '{"commit": {}}'

自动提交配置

可以使用自动提交在定期间隔安排提交:

1
2
3
$ curl -X POST -H 'Content-type: application/json' \
-d '{"set-property":{"updateHandler.autoCommit.maxTime":15000}}' \
http://localhost:8983/api/collections/techproducts/config

这将配置Solr每15秒(15000毫秒)自动提交一次更改。

提交策略说明

提交类型 特点 适用场景
硬提交 数据持久化到磁盘 数据安全要求高
软提交 数据可搜索但未持久化 实时搜索要求高
自动提交 定期自动执行 生产环境推荐

执行基本搜索查询

现在您可以尝试搜索文档:

1
curl 'http://localhost:8983/solr/techproducts/select?q=name%3Alightning'

查询语法说明

  • q=name:lightning - 在name字段中搜索”lightning”
  • URL编码:%3A 代表冒号:

更多查询示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 搜索所有文档
curl 'http://localhost:8983/solr/techproducts/select?q=*:*'

# 在所有字段中搜索
curl 'http://localhost:8983/solr/techproducts/select?q=lightning'

# 搜索特定类别的书籍
curl 'http://localhost:8983/solr/techproducts/select?q=cat:book'

# 价格范围搜索
curl 'http://localhost:8983/solr/techproducts/select?q=price:[10+TO+15]'

# 布尔查询
curl 'http://localhost:8983/solr/techproducts/select?q=name:lightning+AND+inStock:true'

常见响应格式

JSON响应结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"responseHeader": {
"status": 0,
"QTime": 1,
"params": {
"q": "name:lightning"
}
},
"response": {
"numFound": 1,
"start": 0,
"docs": [
{
"id": "978-0641723445",
"name": "The Lightning Thief",
"cat": ["book", "hardcover"],
"price": 12.5,
"inStock": true
}
]
}
}

响应字段说明

  • status: 查询状态(0表示成功)
  • QTime: 查询耗时(毫秒)
  • numFound: 找到的文档总数
  • start: 结果起始位置
  • docs: 文档列表

使用Web界面

除了命令行,您也可以使用Solr的Web管理界面:

  1. 访问界面:打开浏览器访问 http://localhost:8983/solr
  2. 选择集合:在左侧选择”techproducts”集合
  3. 执行查询:点击”Query”菜单进行可视化查询
  4. 查看文档:使用”Documents”菜单添加或查看文档

快速故障排除

常见问题

  1. 端口冲突

    1
    2
    3
    4
    5
    # 检查端口使用情况
    netstat -an | grep 8983

    # 使用其他端口启动
    bin/solr start -c -p 8984
  2. 集合创建失败

    1
    2
    3
    4
    5
    # 检查Solr状态
    bin/solr status

    # 查看日志
    tail -f server/logs/solr.log
  3. 文档未找到

    1
    2
    3
    # 确保提交了更改
    curl -X POST 'http://localhost:8983/api/collections/techproducts/update' \
    -H 'Content-Type: application/json' -d '{"commit": {}}'

清理环境

如果需要重新开始:

1
2
3
4
5
# 删除集合
curl -X DELETE 'http://localhost:8983/api/collections/techproducts'

# 停止Solr
bin/solr stop -all

5分钟总结

恭喜!您已经在5分钟内完成了:

启动SolrCloud - 运行分布式Solr实例
创建集合 - 建立数据存储容器
定义模式 - 配置文档字段结构
索引文档 - 添加可搜索的数据
执行搜索 - 查找和检索信息

这个快速入门为您提供了Solr核心功能的实践体验。您现在已经准备好深入学习Solr的更多高级特性了!

下一步学习

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