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

时间:2022-06-03 00:15:10

I want to use ElasticSearch + Tire to search stored in MongoDB.

我想使用ElasticSearch + Tire来搜索存储在MongoDB中的内容。

However, I'm getting the following error when I try to perform a search:

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

Tire::Search::SearchRequestFailed in SearchController#index

SearchAtroller #index中的Tire :: Search :: SearchRequestFailed

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

From what I understand, this tells me that the indexes are missing for the Event, even though I've told it to generate them when I ran db:setup.

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

Model:

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

Controller:

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

Any ideas on how to resolve this please?

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

5 个解决方案

#1


12  

... or even better, just run :

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

rake environment tire:import CLASS=Event FORCE=true

#2


3  

Set an Initializer ( the following will work locally on your machine and on heroku with bonsai add-on too, just in case ... ):

设置一个初始化程序(以下内容将在您的计算机本地和带有盆景附加组件的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

In your model add the index_name :

在您的模型中添加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

Then open your Rails console with rails c and run :

然后用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

You should see something similar :

你应该看到类似的东西:

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 > 

Now start rails and re-try.

现在启动rails并重新尝试。

#3


1  

... see also :

... 也可以看看 :

Event.index.import Event.all

This way all your records are loaded into memory, serialized into JSON, and sent down the wire to ElasticSearch. This, as well as the two solutions before, should evoid "IndexMissingException[[events] missing]" error.

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

#4


1  

This can happen if you modify the configuration. Try removing the Tire.configure if you define one.

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

#5


0  

Have you indexed your Event model ? And your Venues model ? Elastic search is also looking for the Venues index

你有没有为你的事件模型编制索引?你的场地模特?弹性搜索也在寻找Venues指数

I have a rake task to reindex my Models

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

desc "Reindex event"
task :reindex_events => :environment do

desc“Reindex event”任务:reindex_events =>:环境做

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
}    

end

#1


12  

... or even better, just run :

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

rake environment tire:import CLASS=Event FORCE=true

#2


3  

Set an Initializer ( the following will work locally on your machine and on heroku with bonsai add-on too, just in case ... ):

设置一个初始化程序(以下内容将在您的计算机本地和带有盆景附加组件的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

In your model add the index_name :

在您的模型中添加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

Then open your Rails console with rails c and run :

然后用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

You should see something similar :

你应该看到类似的东西:

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 > 

Now start rails and re-try.

现在启动rails并重新尝试。

#3


1  

... see also :

... 也可以看看 :

Event.index.import Event.all

This way all your records are loaded into memory, serialized into JSON, and sent down the wire to ElasticSearch. This, as well as the two solutions before, should evoid "IndexMissingException[[events] missing]" error.

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

#4


1  

This can happen if you modify the configuration. Try removing the Tire.configure if you define one.

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

#5


0  

Have you indexed your Event model ? And your Venues model ? Elastic search is also looking for the Venues index

你有没有为你的事件模型编制索引?你的场地模特?弹性搜索也在寻找Venues指数

I have a rake task to reindex my Models

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

desc "Reindex event"
task :reindex_events => :environment do

desc“Reindex event”任务:reindex_events =>:环境做

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
}    

end