Ruby:ElasticSearch +轮胎错误轮胎:: Search :: SearchRequestFailed - IndexMissingException?
我想使用ElasticSearch + Tire搜索存储在MongoDB中。Ruby:ElasticSearch +轮胎错误轮胎:: Search :: SearchRequestFailed - IndexMissingException?
不过,我得到以下错误,当我尝试执行搜索:
轮胎::搜索:: SearchRequestFailed在SearchController#指数
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 
任何想法如何解决这个吗?
回答:
您是否为您的事件模型建立了索引?你的场地模型?弹性搜索也寻找场地指数
我有一个rake任务,以重新编制我的模型
desc “的重新索引事件”
任务:reindex_events =>:环境做batch_size = 1000count = 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
回答:
设置一个初始化程序(以下内容将在本地机器上和在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 > 
现在开始导轨并重新尝试。
回答:
...甚至更好,只需要运行:
rake environment tire:import CLASS=Event FORCE=true 回答:
...还看到:
Event.index.import Event.all 这样,所有的记录都加载到内存中,序列化为JSON,并送将导线连接到ElasticSearch。这个以及之前的两个解决方案都应该避免“IndexMissingException [[events] missing]”错误。
回答:
如果您修改配置,可能会发生这种情况。如果您定义了一个,请尝试删除Tire.configure。
以上是 Ruby:ElasticSearch +轮胎错误轮胎:: Search :: SearchRequestFailed - IndexMissingException? 的全部内容, 来源链接: utcz.com/qa/263884.html







