練習 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'