Ruby:ElasticSearch + Tire错误Tire :: Search :: SearchRequestFailed - IndexMissingException?

时间:2012-04-19 02:24:37

标签: ruby-on-rails ruby mongodb elasticsearch tire

我想使用ElasticSearch + Tire搜索存储在MongoDB中。

但是,当我尝试执行搜索时出现以下错误:

Tire :: Search :: SearchRequestFailed in SearchController #index

404 : {"error":"IndexMissingException[[events] missing]","status":404}

根据我的理解,这告诉我事件中缺少索引,即使我已经告诉它在我运行db:setup时生成它们。

型号:

class Event
  include Mongoid::Document
  include Mongoid::Timestamps 

  include Tire::Model::Search
  include Tire::Model::Callbacks

  field :name, :type => String
  field :description, :type => String
  field :started_at => Time
  field :ended_at => Time

  def to_indexed_json
    self.as_json
  end
end

控制器:

  def search
    Event.tire.search(params[:q])
  end

有关如何解决这个问题的任何想法吗?

5 个答案:

答案 0 :(得分:12)

......甚至更好,只需运行:

rake environment tire:import CLASS=Event FORCE=true

答案 1 :(得分:3)

设置一个初始化程序(以下内容将在您的计算机本地和带有盆景附加组件的heroku上运行,以防万一...):

# config/initializers/bonsai.rb

if ENV['BONSAI_INDEX_URL']
  Tire.configure do
    url "http://index.bonsai.io"
  end
  BONSAI_INDEX_NAME = ENV['BONSAI_INDEX_URL'][/[^\/]+$/]
else
  app_name = Rails.application.class.parent_name.underscore.dasherize
  BONSAI_INDEX_NAME = "#{app_name}-#{Rails.env}"
end

在您的模型中添加index_name

class Event
  include Mongoid::Document
  include Mongoid::Timestamps 

  include Tire::Model::Search
  include Tire::Model::Callbacks

  index_name BONSAI_INDEX_NAME

  field :name, :type => String
  field :description, :type => String
  field :started_at => Time
  field :ended_at => Time

  def to_indexed_json
    self.as_json
  end
end

然后使用rails c打开您的Rails控制台并运行:

1.9.2p290 :001 >Event.create_elasticsearch_index
 => 200 : {"ok":true,"acknowledged":true} 
1.9.2p290 :002 > Tire.index BONSAI_INDEX_NAME
1.9.2p290 :003 >        import Event.all
1.9.2p290 :004?>   refresh
1.9.2p290 :005?> end

你应该看到类似的东西:

MONGODB (0ms) ... ['system.namespaces'].find({})
MONGODB (0ms) ... ['events'].find({})
 => #<Tire::Index:0xca8fb18 @name="your-app-name-development",@response=200 : {"ok":true,"_shards":{"total":10,"successful":5,"failed":0}}> 
1.9.2p290 :006 > 

现在启动rails并重新尝试。

答案 2 :(得分:1)

...另见:

Event.index.import Event.all

这样,所有记录都会加载到内存中,序列化为JSON,然后通过线路发送到ElasticSearch。这个,以及之前的两个解决方案,应该避免“IndexMissingException [[events] missing]”错误。

答案 3 :(得分:1)

如果修改配置,可能会发生这种情况。如果您定义了Tire.configure,请尝试删除它。

答案 4 :(得分:0)

您是否已为自己的活动模型编制索引?你的场地模特?弹性搜索也在寻找场地指数

我有一个rake任务来重新索引我的模型

  

desc“Reindex event”
  任务:reindex_events =&gt; :环境做

batch_size = 1000
count = batch_size

Event.all.find_in_batches(:batch_size => batch_size) { |objects|

  puts "Count: " + count.to_s
  count += batch_size

  Event.index.import objects
}