如何使用elasticsearch-model创建索引,批量索引不起作用

时间:2014-12-24 20:19:00

标签: ruby activerecord elasticsearch activemodel

我使用elasticsearch-model gem将elasticsearch与activerecord集成,我正在使用这个模块:

#http://www.codinginthecrease.com/news_article/show/409843?referrer_id=
module PostImport

  def self.import
    Post.find_in_batches do |posts|
      bulk_index(posts)
    end
  end


  def self.prepare_records(posts)
    posts.map do |post|
      { index: { _id: post.id, data: post.as_indexed_json } }
    end
  end

  def self.delete_index
    Post.__elasticsearch__.client
      .indices.delete index: ::Post.__elasticsearch__.index_name rescue nil
  end

  def self.create_index
    Post.__elasticsearch__.create_index!
  end

  def self.bulk_index(posts)
    Post.__elasticsearch__.client
      .bulk({
             index: ::Post.__elasticsearch__.index_name,
             type: ::Post.__elasticsearch__.document_type,
             body: prepare_records(posts),
             refresh: true
            })
  end
end

根据elasticsearch-model example

它使用它来批量创建索引:

client.bulk index: 'articles',
            type:  'article',
            body:  Article.all.as_json.map { |a| { index: { _id: a.delete('id'), data: a } } },
            refresh: true

但这提出了:

Elasticsearch::Transport::Transport::Errors::NotFound: [404] {"error":"IndexMissingException[[posts] missing]","status":404}
from /home/ubuntu/.rvm/gems/ruby-2.1.3/gems/elasticsearch-transport-1.0.6/lib/elasticsearch/transport/transport/base.rb:132:in `__raise_transport_error'

因此,我的模块中的等效代码:PostImport::import会引发此异常,如果我先调用Post.__elasticsearch__.create_index!,然后执行导入,则可以正常工作。

使用elasticsearch-model gem初始化elasticsearch和创建索引的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

您可以使用elasticsearch-rails gem。请参阅此处的文档 https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-rails

为了便于将模型中的数据导入Elasticsearch,需要在应用程序中定义任务,例如。使用以下内容创建lib / tasks / elasticsearch.rake文件:

require 'elasticsearch/rails/tasks/import'

要首次从文章模型导入记录并创建索引,请运行以下命令:

$ bundle exec rake environment elasticsearch:import:model CLASS='Article' FORCE=y