Solr教程
本教程介绍如何启动和运行Solr,将各种数据源导入Solr集合,以及如何使用Solr管理和搜索界面。
本教程分为三个部分,每个部分都基于前面的部分。第一个练习将要求您启动Solr,创建一个集合,索引一些基本文档,然后执行一些搜索。
第二个练习使用不同的数据集,并探索数据集的请求方面。
第三个练习鼓励您开始使用自己的数据,并开始制定实施计划。
最后,我们将介绍空间搜索,并向您展示如何将Solr实例恢复到干净状态。
开始之前
要遵循本教程,您需要…
- 满足系统要求
- Apache Solr版本下载。本教程是为Apache Solr 9.1设计的。
为了获得最佳效果,请在同一台机器上运行显示本教程的浏览器和Solr服务器,以便教程链接正确指向Solr服务器。
打开solr
首先解压缩Solr发行版并将工作目录更改为安装Solr的子目录。例如,对于UNIX、Cygwin或MacOS中的shell:
~$ ls solr*
solr-9.1.1.tgz
~$ tar -xzf solr-9.1.1.tgz
~$ cd solr-9.1.1/
如果您想在开始第一个练习之前了解更多Solr的目录布局,请参阅“目录布局”一节了解详细信息。
空间查询
Solr具有复杂的地理空间支持,包括在给定位置的指定距离范围内(或在边界框内)搜索、按距离排序,甚至按距离提升结果。
我们在练习1中索引的一些示例技术产品文档具有与它们相关的位置,以说明空间功能。要重新索引此数据,请参见练习1。
要了解有关Solr空间功能的更多信息,请参阅空间搜索一节。
包装
如果您已经运行了本快速入门指南中的全部命令集,请执行以下操作:
- 启动Solr进入SolrCloud模式,两个节点,两个集合,包括碎片和副本
- 为几种类型的文件编制索引
- 使用架构API修改架构
- 打开管理控制台,使用其查询界面获取结果
- 打开/浏览界面,以更友好和熟悉的界面探索Solr的功能
干得好!
清理
在完成本教程时,您可能需要停止Solr并将环境重置回起点。以下命令行将停止Solr并删除练习1中创建的两个节点的目录:
bin/solr stop -all ; rm -Rf example/cloud/
练习0:五分钟搜索!
本练习将在5分钟内指导您如何开始使用Solr!
在SolrCloud模式下启动Solr
要启动Solr,请在Unix或MacOS上运行:bin/Solr start-c;bin\solr.cmd在Windows上启动-c。
为了启动另一Solr节点并使其与第一节点一起加入集群,
bin/solr -c -z localhost:9983 -p 8984
创建集合
与数据库系统在表中保存数据一样,Solr在集合中保存数据。可以按如下方式创建集合:
curl --request POST \
--url http://localhost:8983/api/collections \
--header 'Content-Type: application/json' \
--data '{
"create": {
"name": "techproducts",
"numShards": 1,
"replicationFactor": 1
}
}'
定义架构
让我们定义文档将包含的一些字段。
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"}
]
}'
为某些文档编制索引
单个文档可以索引为
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
}'
可以在同一请求中为多个文档编制索引:
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
}
]'
包含文档的文件可以按如下方式编制索引:
curl -H "Content-Type: application/json" \
-X POST \
-d @example/products.json \
--url 'http://localhost:8983/api/collections/techproducts/update?commit=true'
提交更改
文档编入集合后,无法立即进行搜索。为了使它们可搜索,需要一个提交操作(在OpenSearch等其他搜索引擎中也称为刷新)。可以使用自动提交按如下方式定期安排提交。
curl -X POST -H 'Content-type: application/json' -d '{"set-property":{"updateHandler.autoCommit.maxTime":15000}}' http://localhost:8983/api/collections/techproducts/config