[英]Ruby: ElasticSearch + Tire error Tire::Search::SearchRequestFailed - IndexMissingException?
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 SearchFtroller #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? 有关如何解决此问题的任何想法吗?
......甚至更好,只需运行:
rake environment tire:import CLASS=Event FORCE=true
Set an Initializer ( the following will work locally on your machine and on heroku with bonsai add-on too, just in case ... ): 设置一个初始化程序(以下内容将在您的机器本地和onoku上使用盆景附加组件,以防万一...):
# 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并重试。
... 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. 这样,所有记录都被加载到内存中,序列化为JSON,并通过线路发送到ElasticSearch。 This, as well as the two solutions before, should evoid "IndexMissingException[[events] missing]" error.
这个,以及之前的两个解决方案,应该避免“IndexMissingException [[events] missing]”错误。
This can happen if you modify the configuration. 如果修改配置,可能会发生这种情况。 Try removing the Tire.configure if you define one.
如果您定义了Tire.configure,请尝试删除它。
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"
desc“Reindex活动”
task :reindex_events => :environment dotask: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
结束
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.