LoginSignup
8
7

More than 5 years have passed since last update.

MongoDBにあるデータがElasticsearchに同期されないときの対処方法

Last updated at Posted at 2014-11-21

Elasticsearch に MongoDB River Plugin を使うことで、MongoDB のデータを ElasticSearch に同期させることができる。
しかし、同期の設定をした後に、MongoDB にカラムを追加したりすると Elasticsearch に反映されないことがあり、その対処方法がわかったので書いておく。

使用したバージョン

同期されなくなる手順

簡単に言えば、サーバーの起動順が Elasticsearch -> MongoDB だとダメらしい。

  1. MongoDB を起動し、カラムを追加する
  2. MongoDB を停止する
  3. Elasticsearch を起動する
    • ログにエラーが出る
[2014-10-31 13:34:09,356][WARN ]    [org.elasticsearch.river.mongodb.MongoDBRiver] Fail to start river portal
com.mongodb.MongoException: can't find a master
        at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:517)
  1. MongoDB を起動する
  2. Elasticsearch にカラムが追加されていない

一度この順番で起動してしまうと、3.と4.を入れ替えた順序で起動し直しても同期されず、Elasticsearch起動時にステータスエラーが出る。

[2014-10-31 13:45:08,028][INFO ][org.elasticsearch.river.mongodb.MongoDBRiver] Starting river portal
[2014-10-31 13:45:08,031][ERROR][org.elasticsearch.river.mongodb.MongoDBRiver] Cannot start river portal. Current status is START_FAILED

対処方法(1)

おそらく正しい方法だろうと思われる。

-1. _riverstatus を削除する

curl -XDELETE 'http://localhost:9200/_river/testdb/_riverstatus'

-2. Elasticsearch を停止する

curl -XPOST 'http://localhost:9200/_shutdown'

-3. Elasticsearch を起動する

elasticsearch -d

対処方法(2)

強制的に全部作り直しても、直ることは直る。

-0. 現在の設定を確認しておく

$ curl -XGET "http://localhost:9200/_river/testdb/_meta" 

-1. Elasticsearch側のDBを消す

$ curl -XDELETE 'http://localhost:9200/testdb'

-2. ElasticsearchのRiverも消す

$ curl -XDELETE 'http://localhost:9200/_river'

-3. MongoDB River Plugin の設定をやり直す

$ curl -XPUT "localhost:9200/_river/testdb/_meta" -d '
{
  "type": "mongodb",
  "mongodb": {
    "db": "testdb",
    "servers": [
      { "host": "127.0.0.1", "port": 27101 },
      { "host": "127.0.0.1", "port": 27102 }
    ],
    "options": { "secondary_read_preference": true, "import_all_collections": true }
  },
  "index": { "name": "testdb" }
}'
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7