Apache Solr入門の第三章、インデックスの更新について読んでいく。
実験用のコアの作成
書籍のサンプルでもよいのだけど、solrのリポジトリにexampledocsがあるのを発見したので、折角なのでそちらを使ってみる。
参照:solr/solr/example at releases/solr/9.2.1 · apache/solr · GitHub
まず、デフォルトで準備されている設定を使ってtechproductという名前のコアを作成する。
solr@91ae230d58a3:/opt/solr-9.2.1$ /opt/solr/bin/solr create_core -c techproducts -d sample_techproducts_configs Created new core 'techproducts'
exampledocs以下には様々なデータが準備されているのだけど、どうやらtechproductsのスキーマはそれらが入るようにいい感じに準備されている様子。
solr/managed-schema.xml at releases/solr/9.2.1 · apache/solr · GitHub
データの投入(curl)
どれか一つの形式で入れられれば不都合ない気がするので、jsonで入れていく。jsonはjqで簡単に処理できるし、pythonとかで触るのも比較的慣れているので使いやすい。
まず、q以外何も指定しない男気クエリを使って、最初は何も入っていないことを確認する。
$ curl "http://localhost:8983/solr/techproducts/select?q=*%3A*" { "responseHeader":{ "status":0, "QTime":2, "params":{ "q":"*:*"}}, "response":{"numFound":0,"start":0,"numFoundExact":true,"docs":[] }}
次に、books.json をcurlで入れてみる。
公式のガイドだと、投入の方法はこの辺りに記載がある。 Indexing with Update Handlers :: Apache Solr Reference Guide
$ curl 'http://localhost:8983/solr/techproducts/update?commit=true' --data-binary @example/exampledocs/books.json -H 'Content-type:application/json' { "responseHeader":{ "status":0, "QTime":484}}
ドキュメントが投入されていることが確認できる。
$ curl "http://localhost:8983/solr/techproducts/select?q=*%3A*&fl=name" { "responseHeader":{ "status":0, "QTime":2, "params":{ "q":"*:*", "fl":"name"}}, "response":{"numFound":4,"start":0,"numFoundExact":true,"docs":[ { "name":"The Lightning Thief"}, { "name":"The Sea of Monsters"}, { "name":"Sophie's World : The Greek Philosophers"}, { "name":"Lucene in Action, Second Edition"}] }}
データの投入(postツール)
一回すべてのドキュメントを削除する。
$ curl "http://localhost:8983/solr/techproducts/update?commit=true" --data-binary '{"delete":{"query":"*:*"}}' { "responseHeader":{ "status":0, "QTime":321}}
postツールを使って更新してみる。-format solr
をつけないと、ドキュメントは入るのだけど、フィールドがパースされない( _src
にjsonがそのまま入っておわりだった)ので注意が必要。
solr@91ae230d58a3:/opt/solr-9.2.1$ /opt/solr/bin/post -c techproducts -format solr /opt/solr/example/exampledocs/books.json /opt/java/openjdk/bin/java -classpath /opt/solr/server/solr-webapp/webapp/WEB-INF/lib/solr-core-9.2.1.jar -Dauto=yes -Dformat=solr -Dc=techproducts -Ddata=files org.apache.solr.util.SimplePostTool /opt/solr/example/exampledocs/books.json SimplePostTool version 5.0.0 Posting files to [base] url http://localhost:8983/solr/techproducts/update... Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log POSTing file books.json (application/json) to [base] 1 files indexed. COMMITting Solr index changes to http://localhost:8983/solr/techproducts/update... Time spent: 0:00:00.242
-format solrをつけることで、jsonが/update/json/docsではなく/updateに送られる。 ガイドを見た感じ、/update/json/docsは単一のドキュメントを更新するのに使って、/updateは複数を受け付けられるのかなと思う。それぞれ期待するjsonの形式が違っている。 https://solr.apache.org/guide/solr/9_2/indexing-guide/indexing-with-update-handlers.html
solr@91ae230d58a3:/opt/solr-9.2.1$ /opt/solr/bin/post -h Usage: post -c <collection> [OPTIONS] <files|directories|urls|-d ["...",...]> or post -help collection name defaults to DEFAULT_SOLR_COLLECTION if not specified OPTIONS ======= Solr options: -url <base Solr update URL> (overrides collection, host, and port) -host <host> (default: localhost) -p or -port <port> (default: 8983) -commit yes|no (default: yes) -u or -user <user:pass> (sets BasicAuth credentials) Web crawl options: -recursive <depth> (default: 1) -delay <seconds> (default: 10) Directory crawl options: -delay <seconds> (default: 0) stdin/args options: -type <content/type> (default: application/xml) Other options: -filetypes <type>[,<type>,...] (default: xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log) -params "<key>=<value>[&<key>=<value>...]" (values must be URL-encoded; these pass through to Solr update request) -out yes|no (default: no; yes outputs Solr response to console) -format solr (sends application/json content as Solr commands to /update instead of /update/json/docs) Examples: * JSON file: /opt/solr/bin/post -c wizbang events.json * XML files: /opt/solr/bin/post -c records article*.xml * CSV file: /opt/solr/bin/post -c signals LATEST-signals.csv * Directory of files: /opt/solr/bin/post -c myfiles ~/Documents * Web crawl: /opt/solr/bin/post -c gettingstarted https://solr.apache.org/ -recursive 1 -delay 1 * Standard input (stdin): echo '{commit: {}}' | /opt/solr/bin/post -c my_collection -type application/json -out yes -d * Data as string: /opt/solr/bin/post -c signals -type text/csv -out yes -d $'id,value\n1,0.47'
それにしてもこのツール、多機能でびっくりだ。コマンド自体はシンプルなシェルスクリプトなんだけど、実態は以下のクラスで、Simple?と言いたくなるくらいいろいろ機能がある様子。
solr/SimplePostTool.java at releases/solr/9.2.1 · apache/solr · GitHub