全文検索エンジンの Apache Solr 7.5のインストールする (Mac/Linux)

2021年5月8日土曜日

Solr

t f B! P L

全文検索エンジンである、Apache Solrのインストールから起動するまでの話です。

スポンサーリンク

ダウンロード

http://ftp.jaist.ac.jp/pub/apache/lucene/solr/7.5.0/

上のページから、「solr-7.5.0.tgz」をダウンロード。
ダウンロードが終わったら解凍します。

tar xzf solr-7.5.0.tgz

Solrの起動

解凍先のディレクトリに移動して、以下のコマンドを入力。

cd solr-7.5.0
./bin/solr start -e cloud

起動オプションを入力

対話式で、いくつか起動オプションを聞かれるので、答えていきます。
Solrであらかじめ用意されている、サンプルスキーマ / サンプルデータを使って、起動していきたいと思います。

  • 起動するノード数

デフォルト値を使用する為、そのままEnter

This interactive session will help you launch a SolrCloud cluster on your local workstation.
To begin, how many Solr nodes would you like to run in your local cluster? (specify 1-4 nodes) [2]:
  • 1つ目のノードの、ポート番号 (デフォルト値を使用)

デフォルト値を使用する為、そのままEnter

Ok, let's start up 2 Solr nodes for your example SolrCloud cluster.
Please enter the port for node1 [8983]:
  • 2つ目のノードの、ポート番号

デフォルト値を使用する為、そのままEnter

Please enter the port for node2 [7574]:
  • コレクション名

techproducts と入力して Enter

Now let's create a new collection for indexing documents in your 2-node cluster.
Please provide a name for your new collection: [gettingstarted] techproducts
  • インデックスの分割数

デフォルト値を使用する為、そのままEnter

How many shards would you like to split techproducts into? [2]
  • レプリカの数

デフォルト値を使用する為、そのままEnter

How many replicas per shard would you like to create? [2]
  • configSet

sample_techproducts_configsと入力して Enter

Please choose a configuration for the techproducts collection, available options are:
_default or sample_techproducts_configs [_default] sample_techproducts_configs

すべての起動オプションを入力すると、以下のメッセージが表示されて、Solrが起動します。

Enabling auto soft-commits with maxTime 3 secs using the Config API
POSTing request to Config API: http://localhost:8983/solr/techproducts/config{"set-property":{"updateHandler.autoSoftCommit.maxTime":"3000"}}
Successfully set-property updateHandler.autoSoftCommit.maxTime to 3000
SolrCloud example running, please visit: http://localhost:8983/solr

メッセージに表示されている、http://localhost:8983/solrの部分をブラウザに入力して、管理コンソールが開けば、起動成功です。

enter image description here

サンプルデータの投入

あらかじめ Solrが用意しているサンプルデータを投入します。

bin/post -c techproducts example/exampledocs/*

java -classpath /Users/teramotosatoshishi/Temp/solr-7.5.0/dist/solr-core-7.5.0.jar -Dauto=yes -Dc=techproducts -Ddata=files org.apache.solr.util.SimplePostTool example/exampledocs/books.csv example/exampledocs/books.json example/exampledocs/gb18030-example.xml example/exampledocs/hd.xml example/exampledocs/ipod_other.xml example/exampledocs/ipod_video.xml example/exampledocs/manufacturers.xml example/exampledocs/mem.xml example/exampledocs/money.xml example/exampledocs/monitor.xml example/exampledocs/monitor2.xml example/exampledocs/more_books.jsonl example/exampledocs/mp500.xml example/exampledocs/post.jar example/exampledocs/sample.html example/exampledocs/sd500.xml example/exampledocs/solr-word.pdf example/exampledocs/solr.xml example/exampledocs/test_utf8.sh example/exampledocs/utf8-example.xml example/exampledocs/vidcard.xml
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.csv (text/csv) to [base]
POSTing file books.json (application/json) to [base]/json/docs
POSTing file gb18030-example.xml (application/xml) to [base]
POSTing file hd.xml (application/xml) to [base]
POSTing file ipod_other.xml (application/xml) to [base]
POSTing file ipod_video.xml (application/xml) to [base]
POSTing file manufacturers.xml (application/xml) to [base]
POSTing file mem.xml (application/xml) to [base]
POSTing file money.xml (application/xml) to [base]
POSTing file monitor.xml (application/xml) to [base]
POSTing file monitor2.xml (application/xml) to [base]
POSTing file more_books.jsonl (application/json) to [base]/json/docs
POSTing file mp500.xml (application/xml) to [base]
POSTing file post.jar (application/octet-stream) to [base]/extract
POSTing file sample.html (text/html) to [base]/extract
POSTing file sd500.xml (application/xml) to [base]
POSTing file solr-word.pdf (application/pdf) to [base]/extract
POSTing file solr.xml (application/xml) to [base]
POSTing file test_utf8.sh (application/octet-stream) to [base]/extract
POSTing file utf8-example.xml (application/xml) to [base]
POSTing file vidcard.xml (application/xml) to [base]
21 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/techproducts/update...
Time spent: 0:00:06.648

スポンサーリンク

検索

投入したサンプルデータに対し、検索を行います。

curl "http://localhost:8983/solr/techproducts/select?q=foundation&wt=xml&indent=true"

URLパラメータで検索条件を指定します。

q:検索文字列
wt:出力フォーマット (xml, jsonなど)
indent:出力結果にインデントを設定する場合は true

レスポンスのXMLは以下のようになります。

<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
  <bool name="zkConnected">true</bool>
  <int name="status">0</int>
  <int name="QTime">24</int>
  <lst name="params">
    <str name="q">foundation</str>
    <str name="wt">xml</str>
    <str name="_">1541994275045</str>
  </lst>
</lst>
<result name="response" numFound="4" start="0" maxScore="2.671292">
  <doc>
    <str name="id">0553293354</str>
    <arr name="cat">
      <str>book</str>
    </arr>
    <str name="name">Foundation</str>
    <float name="price">7.99</float>
    <str name="price_c">7.99,USD</str>
    <bool name="inStock">true</bool>
    <str name="author">Isaac Asimov</str>
    <str name="author_s">Isaac Asimov</str>
    <str name="series_t">Foundation Novels</str>
    <int name="sequence_i">1</int>
    <str name="genre_s">scifi</str>
    <long name="_version_">1616883289396609024</long>
    <long name="price_c____l_ns">799</long></doc>
  <doc>
    <str name="id">UTF8TEST</str>
    <str name="name">Test with some UTF-8 encoded characters</str>
    <str name="manu">Apache Software Foundation</str>
    <arr name="cat">
      <str>software</str>
      <str>search</str>
    </arr>
    <arr name="features">
      <str>No accents here</str>
      <str>This is an e acute: é</str>
      <str>eaiou with circumflexes: êâîôû</str>
      <str>eaiou with umlauts: ëäïöü</str>
      <str>tag with escaped chars: &lt;nicetag/&gt;</str>
      <str>escaped ampersand: Bonnie &amp; Clyde</str>
      <str>Outside the BMP:𐌈 codepoint=10308, a circle with an x inside. UTF8=f0908c88 UTF16=d800 df08</str>
    </arr>
    <float name="price">0.0</float>
    <str name="price_c">0.0,USD</str>
    <bool name="inStock">true</bool>
    <long name="_version_">1616883291442380800</long>
    <long name="price_c____l_ns">0</long>
   </doc>
   ~ 中略 ~
</result>
</response>

Solrの停止

停止するには、以下のコマンドを入力します。

bin/solr stop -all

掃除

今回作成した、2つのノードを削除し、最初の状態に戻すには、以下のコマンドを入力します。

bin/solr stop -all ; rm -Rf example/cloud/
スポンサーリンク
スポンサーリンク

このブログを検索

Profile

自分の写真
Webアプリエンジニア。 日々新しい技術を追い求めてブログでアウトプットしています。
プロフィール画像は、猫村ゆゆこ様に書いてもらいました。

仕事募集もしていたり、していなかったり。

QooQ