🎊 solr入门:5分钟快速上手

solr入门:5分钟快速上手

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

启动Solr云模式启动第一个节点要启动Solr,请运行:

Unix或MacOS:bin/solr start -c

Windows:bin\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将数据保存在集合中。可以按以下方式创建集合:

12345678$ 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个副本)

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

12345678910111213141516$ 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

商店位置

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

123456789101112131415$ 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 }'

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

1234567891011121314151617181920212223242526272829$ 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 }]'

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

1234$ 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)。

手动提交可以手动提交更改:

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

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

123$ 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毫秒)自动提交一次更改。

提交策略说明

提交类型

特点

适用场景

硬提交

数据持久化到磁盘

数据安全要求高

软提交

数据可搜索但未持久化

实时搜索要求高

自动提交

定期自动执行

生产环境推荐

执行基本搜索查询现在您可以尝试搜索文档:

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

查询语法说明

q=name:lightning - 在name字段中搜索”lightning”

URL编码:%3A 代表冒号:

更多查询示例1234567891011121314# 搜索所有文档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响应结构12345678910111213141516171819202122{ "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管理界面:

访问界面:打开浏览器访问 http://localhost:8983/solr

选择集合:在左侧选择”techproducts”集合

执行查询:点击”Query”菜单进行可视化查询

查看文档:使用”Documents”菜单添加或查看文档

快速故障排除常见问题

端口冲突

12345# 检查端口使用情况netstat -an | grep 8983# 使用其他端口启动bin/solr start -c -p 8984

集合创建失败

12345# 检查Solr状态bin/solr status# 查看日志tail -f server/logs/solr.log

文档未找到

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

清理环境如果需要重新开始:

12345# 删除集合curl -X DELETE 'http://localhost:8983/api/collections/techproducts'# 停止Solrbin/solr stop -all

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

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

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

下一步学习

Solr入门:管理界面详解 - 学习使用Web界面

Solr教程:练习一 - 索引Techproducts数据 - 深入的实践教程

Solr概念:文档字段与模式设计 - 理解模式设计原理

Solr概念:搜索功能与查询语法 - 掌握查询技巧

🎯 相关推荐

【活动爆料】惊虹宝库心动来袭,HeartShot夺宝十连抽等你拿!
黄金戒指一个多少钱
365bet提款审核

黄金戒指一个多少钱

📅 07-04 👀 3551
老上海的花国世界
office365企业邮箱设置

老上海的花国世界

📅 10-05 👀 7538
DERE戴睿笔记本怎么样?性价比的品牌之选
365bet提款审核

DERE戴睿笔记本怎么样?性价比的品牌之选

📅 07-08 👀 3527
财富三问:钱是什么?钱在哪里?怎么赚钱?
office365企业邮箱设置

财富三问:钱是什么?钱在哪里?怎么赚钱?

📅 06-30 👀 3373
青浦赵巷新长宁·水韵名邸现房测评!双轨+精装三房真香?
王者荣耀:如何克制芈月,这三位英雄出场让她不要放肆
揭秘传销洗脑机制:从心理学到法律规制的全方位解析
office365企业邮箱设置

揭秘传销洗脑机制:从心理学到法律规制的全方位解析

📅 10-27 👀 4021
69元是一坨真垃圾?还是性价比之王?卡佐AZ50鼠标