2015-08-17 67 views
0

我想在nodejs中使用elasticsearch的官方客戶端來爲chouchDB創建索引。真的很感激任何幫助。使用elasticssearch客戶端創建索引

這是我正在創建一個索引:

esClient.indices.create({index: "users_index", 
      body: { 
       "type" : "couchdb", 
       "couchdb" : { 
        "host" : "localhost", 
        "port" :"5984", 
        "db" :"users" 
       }, 
       "index" : { 
        "index" : "users_index", 
        "type" : "users", 
        "bulk_size" : "100", 
        "bulk_timeout" : "10ms" 
       } 
      }}).then(function(x){ 
       console.log(x); 
       callback(null); 
      }, 
      function(err){ 
       console.log(err); 
       callback(null); 
      }); 

當我搜索這樣的檢測數據(GET users_index/users_index/_search),我得到這個沒有任何數據: { 「了「:1, 「TIMED_OUT」:假的, 「_shards」:{ 「總」:1, 「成功」:1, 「失敗」:0 }, 「命中」:{ 「總」 :0, 「max_score」:null, 「hits」:[] } }

我真的很感謝任何幫助。

在創建索引之前,我也嘗試創建一個索引模板,以便我的所有索引都獲得相同的映射。我像下面這樣做。我還沒有走過那麼遠去驗證它是否正確。請讓我知道這是否有任何錯誤。

esClient.indices.putTemplate({ 
      name: "vw_index_template", 
      body:{ 
       "template" : "*_index", 
       "settings" : { 
        "number_of_shards" : 1 
       }, 
       "mappings" : { 
        "id_prkey_template" : { 
         "properties" : { 
          "_id" : {"type" : "string", "index": "not_analyzed"}, 
          "prkey" : {"type" : "string", "index": "analyzed"} 
         } 
        } 
       } 
      } 
     }).then(function(res){ 
      console.log(res); 
      callback(null); 

     }, function(error){ 
      console.log(error); 
      callback(null); 
     }); 

真的很感激任何幫助。

非常感謝。

回答

0

您無法將文檔(正文)附加到indices.createsee here)。如果你想簡單索引的文件到任何現有的或不存在的索引,你應該使用index,例如:

esClient.index({ 
    index: "users_index", 
    type: "couchdb", 
    body: { 
    field1: 'test', 
    field2: 123 
    } 
}).then(function (x) { 
    console.log(x); 
    callback(null); 
    }, 
    function (err) { 
    console.log(err); 
    callback(null); 
    }); 

而且,你不應該指定_id領域的任何映射,因爲它是一個內部ES領域。

+0

我試過你的建議,但似乎不工作。我想要做的是創建一個從couchdb導入數據的索引(我正在使用coucdb river插件進行elasticsearch)。 – Imad

+0

我嘗試了下面的代碼來完成我在上面評論中所說的內容。 'curl -XPUT「http:// localhost:9200/_river/user_idx/_meta」-d「{type:」couchdb「,」couchdb「:{」host「:」localhost「,」port「 5984,「db」:「users」,「filter」:null}, \t「index」:{「index」:「user_idx」,「type」:「users」,「bulk_size」:「100」,「bulk_timeout 「:」10ms「}}」',它工作正常。現在試圖在elasticsearchclient的nodejs中做同樣的事情。我嘗試了你的建議,但似乎沒有做我想要的。 – Imad

+0

關於你對不使用_id進行映射的建議,你能告訴我如何讓我的couchdb數據庫的_id可以被搜索,即如何在索引中添加它?謝謝。 – Imad

相關問題