Solr で solrcloud による分散検索(Distributed Search)

 

少し前に、Solr で sharding による分散検索(Distributed Search) - mziの日記 で分散検索の方法を書いた。実は、これはLegacyな方法。

Solr 3.0以降はsolrcloudがメインの使い方のようです。まあ必要以上にする必要はないですけど。

 

で、今回は、SolrCloudのGetting Startを読むことにする。

SolrCloud - Apache Solr Reference Guide - Apache Software Foundation

Getting Started with SolrCloud - Apache Solr Reference Guide - Apache Software Foundation

 

単純なshardingとの違いは、Zookeeperを利用しているということ。

これにより、検索対象のshardの指定がいらなかったり、設定の配布ができたり、勝手にレプリケーションができたりする。

 

ひとまずGettingStartはかなり簡単。

  1. 一つ目のSolrを内蔵のZookeeper付きで立ち上げる。その際、いくつのShardを使うか指定する。
  2. 残りのshardを立ちあげ、立ち上がっているzookeeperにつなぐ

これだけ。

1で指定したshard数以上を接続すると、勝手にレプリケーションとして動作するようになる。

cd <SOLR_DIST_HOME>

cp -r example node1

cp -r example node2

1つ目

cd node1

java -DzkRun -DnumShards=2 -Dbootstrap_confdir=./solr/collection1/conf -Dcollection.configName=myconf -jar start.jar

2つ目

cd node2

java -Djetty.port=7574 -DzkHost=localhost:9983 -jar start.jar

これで、以下にアクセスすると

http://localhost:8983/solr/#/~cloud

こうなる

二つのshardが接続されていることがわかる。

で、あとは、それぞれのshardでインデックスにデータを登録。

cd exampledocs

curl http://localhost:8983/solr/update?commit=true -H "Content-Type: text/xml" -d "@mem.xml"

curl http://localhost:7574/solr/update?commit=true -H "Content-Type: text/xml" -d "@monitor2.xml"

そして、検索してみる。

以下の2つは同じ結果を返してくれる。何の設定もなく、自動的に両方のshardを検索して結果を統合してくれる。

http://localhost:8983/solr/collection1/select?q=*:*

http://localhost:7574/solr/collection1/select?q=*:*

 4件の同じ結果が返ってくる。

"distrib=false"のオプションを付けると、そのShardだけの結果が返ってくるようなので、やってみる。

http://localhost:8983/solr/collection1/select?q=*:*&distrib=false

http://localhost:7574/solr/collection1/select?q=*:*&distrib=false

両方の結果件数が変わるのがわかる。

 

結構簡単。