Solr6.1.0インストール・コア作成・サンプルデータ投入まで

インストール

事前準備

まずはインストールに必要なJavaと、solrダウンロードに利用するwgetをインストールします。

# yum install java-1.8.0-openjdk-devel wget

# java -version
openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

Solrのインストール

今回はSolr6.1.0を利用します。ダウンロードURLは以下から選びます。

http://www.apache.org/dyn/closer.lua/lucene/solr/6.1.0

ダウンロードして解凍します。

# wget http://ftp.kddilabs.jp/infosystems/apache/lucene/solr/6.1.0/solr-6.1.0.tgz

# tar xvf solr-6.1.0.tgz

手動でインストールも可能ですが、インストール用のスクリプトも準備されているので、これ実行すればインストール完了です。

# ./solr-6.1.0/bin/install_solr_service.sh ./solr-6.1.0.tgz
<省略>
Found 1 Solr nodes:

Solr process 11946 running on port 8983
{
  "solr_home":"/var/solr/data",
  "version":"6.1.0 4726c5b2d2efa9ba160b608d46a977d0a6b83f94 - jpountz - 2016-06-13 09:46:58",
  "startTime":"2016-07-16T15:35:10.939Z",
  "uptime":"0 days, 0 hours, 0 minutes, 16 seconds",
  "memory":"80.6 MB (%16.4) of 490.7 MB"}

Service solr installed.

インストールされたようです。

稼働確認

SysVinitでインストールされます。インストール完了時点で起動された状態になっています。

# /etc/init.d/solr status

Found 1 Solr nodes:

Solr process 11983 running on port 8983
{
  "solr_home":"/var/solr/data",
  "version":"6.1.0 4726c5b2d2efa9ba160b608d46a977d0a6b83f94 - jpountz - 2016-06-13 09:46:58",
  "startTime":"2016-07-14T02:23:49.892Z",
  "uptime":"0 days, 0 hours, 0 minutes, 28 seconds",
  "memory":"85.4 MB (%17.4) of 490.7 MB"}

自動起動もONになっています。

$ chkconfig --list solr
solr            0:off  1:off  2:on   3:on   4:on   5:on   6:off

インストール状況確認

Solr用のユーザとしてsolrが作成されています。

# su - solr

jpsでプロセス確認してみます。

$ /bin/jps -lv
1876 start.jar -Xms512m -Xmx512m -XX:NewRatio=3 -XX:SurvivorRatio=4 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=8 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:ConcGCThreads=4 -XX:ParallelGCThreads=4 -XX:+CMSScavengeBeforeRemark -XX:PretenureSizeThreshold=64m -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=50 -XX:CMSMaxAbortablePrecleanTime=6000 -XX:+CMSParallelRemarkEnabled -XX:+ParallelRefProcEnabled -verbose:gc -XX:+PrintHeapAtGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:/var/solr/logs/solr_gc.log -Djetty.port=8983 -DSTOP.PORT=7983 -DSTOP.KEY=solrrocks -Duser.timezone=UTC -Djetty.home=/opt/solr/server -Dsolr.solr.home=/var/solr/data -Dsolr.install.dir=/opt/solr -Dlog4j.configuration=file:/var/solr/log4j.properties -Xss256k -XX:OnOutOfMemoryError=/opt/solr/bin/oom_solr.sh 8983 /var/solr/logs
3861 sun.tools.jps.Jps -Dapplication.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-1.b14.el7_2.x86_64 -Xms8m

Javaのパラメータみると以下のようなことがわかります。

項目
WebサーバJettyのポート Djetty.port=8983
Solrインストール先 install.dir=/opt/solr
Solrホーム solr.home=/var/solr/data
Solrログ /var/solr/logs

そしてWebサーバJettyのポートに繋ぐため、以下のアドレスにアクセスすればSolrの管理画面が開けるはずです。

http://[solr_server_ip]:8983/solr/

f:id:yomon8:20160717113718p:plain

SolrCore作成

solrコマンド利用してコアを作成します。

# su - solr
$ /opt/solr/bin/solr create_core -c mycore

サンプルデータ登録

サンプルデータのあるbooks.csvというファイルを元にデータを登録してみます。このCSVファイルには10件の書籍データが登録されています。

$ cat /opt/solr/example/exampledocs/books.csv
id,cat,name,price,inStock,author,series_t,sequence_i,genre_s
0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,"A Song of Ice and Fire",1,fantasy
0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,"A Song of Ice and Fire",2,fantasy
055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,"A Song of Ice and Fire",3,fantasy
0553293354,book,Foundation,7.99,true,Isaac Asimov,Foundation Novels,1,scifi
0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy
0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi
0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy
0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy
0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy
080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy

CSVファイルのデータをsolrにアップロードしてみます。

cd /opt/solr
./bin/post -c mycore example/exampledocs/books.csv

10件のデータが登録されているのが管理画面上からも確認できます。 f:id:yomon8:20160717113619p:plain

URL叩いて検索結果を確認してみます。検索文字列を「Game」にして叩いてみました。

curl http://[solr_server_ip]:8983/solr/mycore/select?q=Game

結果はこちら。2件返ってきました。

<?xml version="1.0" encoding="UTF-8"?>
<response>

<lst name="responseHeader">
  <int name="status">0</int>
  <int name="QTime">0</int>
  <lst name="params">
    <str name="q">Game</str>
    <str name="indent">on</str>
  </lst>
</lst>
<result name="response" numFound="2" start="0">
  <doc>
    <str name="id">0812550706</str>
    <arr name="cat">
      <str>book</str>
    </arr>
    <arr name="name">
      <str>Ender's Game</str>
    </arr>
    <arr name="price">
      <double>6.99</double>
    </arr>
    <arr name="inStock">
      <bool>true</bool>
    </arr>
    <arr name="author">
      <str>Orson Scott Card</str>
    </arr>
    <arr name="series_t">
      <str>Ender</str>
    </arr>
    <int name="sequence_i">1</int>
    <str name="genre_s">scifi</str>
    <long name="_version_">1540066411554013184</long></doc>
  <doc>
    <str name="id">0553573403</str>
    <arr name="cat">
      <str>book</str>
    </arr>
    <arr name="name">
      <str>A Game of Thrones</str>
    </arr>
    <arr name="price">
      <double>7.99</double>
    </arr>
    <arr name="inStock">
      <bool>true</bool>
    </arr>
    <arr name="author">
      <str>George R.R. Martin</str>
    </arr>
    <arr name="series_t">
      <str>A Song of Ice and Fire</str>
    </arr>
    <int name="sequence_i">1</int>
    <str name="genre_s">fantasy</str>
    <long name="_version_">1540066411225808896</long></doc>
</result>
</response>

ブラウザのGUI上から確認したい場合は、

Queryツールや、

http://[solr_server_ip]:8983/solr/#/mycore/query

簡易な検索画面もあります。

http://[solr_server_ip]:8983/solr/mycore/browse?q=game

f:id:yomon8:20160717113600p:plain