演習 0: 5分で検索!
この演習では、わずか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 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/products.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