练习 0:五分钟搜索!
本练习将指导您如何在短短 5 分钟内开始使用 Solr!
在 SolrCloud 模式下启动 Solr
要启动 Solr,请运行:在 Unix 或 MacOS 上运行 bin/solr start -c
;在 Windows 上运行 bin\solr.cmd start -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 '{
"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'