练习 0:五分钟学会搜索!

本练习将引导您如何在短短 5 分钟内开始使用 Solr!

以 SolrCloud 模式启动 Solr

要启动 Solr,请在 Unix 或 MacOS 上运行:bin/solr start -c;在 Windows 上运行:bin\solr.cmd start -c

要启动另一个 Solr 节点并使其加入第一个节点旁边的集群,

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

创建一个集合

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

$ curl --request POST \
--url https://127.0.0.1:8983/api/collections \
--header 'Content-Type: application/json' \
--data '{
  "name": "techproducts",
  "numShards": 1,
  "replicationFactor": 1
}'

定义一个模式

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

$ curl --request POST \
  --url https://127.0.0.1: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 'https://127.0.0.1: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 'https://127.0.0.1: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/exampledocs/books.json \
       --url 'https://127.0.0.1:8983/api/collections/techproducts/update?commit=true'

提交更改

将文档索引到集合后,它们不会立即用于搜索。为了使它们可搜索,需要进行提交操作(在其他搜索引擎(如 OpenSearch 等)中也称为 refresh)。可以使用自动提交按如下所示的周期性间隔安排提交。

$ curl -X POST -H 'Content-type: application/json' -d '{"set-property":{"updateHandler.autoCommit.maxTime":15000}}' https://127.0.0.1:8983/api/collections/techproducts/config

进行一些基本搜索查询

您现在可以尝试搜索您的文档,如下所示

curl 'https://127.0.0.1:8983/solr/techproducts/select?q=name%3Alightning'