用意するもの
- Docker
- curlコマンド
Docker で Solr の起動
command
docker run -d -p 8983:8983 --name my_solr solr
Docker で Solr コレクションの作成
command
docker exec -it my_solr bin/solr create -c sandbox
output
WARNING: Using _default configset with data driven schema functionality. NOT RECOMMENDED for production use.
To turn off: bin/solr config -c sandbox -p 8983 -action set-user-property -property update.autoCreateFields -value false
Created new core 'sandbox'
>
フィールドタイプの定義
ここでは例としてベクトルのサイズが4のフィールドタイプを定義しています。
curl -X POST -H 'Content-type:application/json' --data-binary '{"add-field-type":{"name":"vector4","class":"solr.DenseVectorField","vectorDimension":4,"similarityFunction":"cosine"}}' http://localhost:8983/solr/sandbox/schema
フィールドの定義
curl -X POST -H 'Content-type:application/json' --data-binary '{ "add-field":{ "name":"v4", "type":"vector4", "indexed":true, "stored":true, } }' http://localhost:8983/solr/sandbox/schema
検索対象データの追加
curl -X POST -H 'Content-Type:application/json' --data-binary '[ { "id": "1", "v4": [1.0, 0.0, 0.0, 0.0] } ]' http://localhost:8983/solr/sandbox/update/json/docs?commit=true
curl -X POST -H 'Content-Type:application/json' --data-binary '[ { "id": "2", "v4": [0.0, 1.0, 0.0, 0.0] } ]' http://localhost:8983/solr/sandbox/update/json/docs?commit=true
curl -X POST -H 'Content-Type:application/json' --data-binary '[ { "id": "3", "v4": [0.0, 0.0, 1.0, 0.0] } ]' http://localhost:8983/solr/sandbox/update/json/docs?commit=true
curl -X POST -H 'Content-Type:application/json' --data-binary '[ { "id": "4", "v4": [0.0, 0.0, 0.0, 1.0] } ]' http://localhost:8983/solr/sandbox/update/json/docs?commit=true
検索対象データの追加(複数文書をまとめて追加したい場合)
curl -X POST -H 'Content-Type:application/json' --data-binary '[ { "id": "1", "v4": [1.0, 0.0, 0.0, 0.0] }, { "id": "2", "v4": [0.0, 1.0, 0.0, 0.0] }, { "id": "3", "v4": [0.0, 0.0, 1.0, 0.0] }, { "id": "4", "v4": [0.0, 0.0, 0.0, 1.0] } ]' http://localhost:8983/solr/sandbox/update/json/docs?commit=true
検索
curl -X GET 'http://localhost:8983/solr/sandbox/select?q=%7B!knn%20f=v4%20topK=10%7D%5B1.0%2C1.0%2C0.0%2C0.0%5D'
検索結果
{
"responseHeader":{
"status":0,
"QTime":91,
"params":{
"q":"{!knn f=v4 topK=10}[1.0,1.0,0.0,0.0]"
}
},
"response":{
"numFound":4,
"start":0,
"numFoundExact":true,
"docs":[{
"id":"1",
"v4":[1.0,0.0,0.0,0.0],
"_version_":1804607180982190080
},{
"id":"2",
"v4":[0.0,1.0,0.0,0.0],
"_version_":1804607188756332544
},{
"id":"3",
"v4":[0.0,0.0,1.0,0.0],
"_version_":1804607196582903808
},{
"id":"4",
"v4":[0.0,0.0,0.0,1.0],
"_version_":1804607205283987456
}]
}
}
以上.