ruby-on-rails – 盆景:使用create_index!或者:force选项来创建它

在过去的4周里,我一直在与Bonsai和Elasticsearch进行一场激烈的战斗,这在某种程度上令人沮丧,因为几个月前我让它在另一个应用程序上工作.
我的应用程序崩溃了,当我去Heroku时,我得到了“应用程序错误”警告.当我查看Herkou Logs时,我发现了一些我认为是问题的东西:

2016-08-31T23:23:47.189996+00:00 heroku[web.1]: Process exited with status 1
2016-09-01T00:12:50.088164+00:00 heroku[web.1]: State changed from crashed to starting
2016-09-01T00:12:57.130922+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 14657 -e production`
2016-09-01T00:13:00.947244+00:00 app[web.1]: => Booting WEBrick
2016-09-01T00:13:00.947263+00:00 app[web.1]: => Rails 4.2.5.1 application starting in production on http://0.0.0.0:14657
2016-09-01T00:13:00.947264+00:00 app[web.1]: => Run `rails server -h` for more startup options
2016-09-01T00:13:00.947265+00:00 app[web.1]: => Ctrl-C to shutdown server
2016-09-01T00:13:00.947266+00:00 app[web.1]: Exiting
2016-09-01T00:13:00.947281+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/elasticsearch-model-0.1.9/lib/elasticsearch/model/importing.rb:118:in `import': professors does not exist to be imported into. Use create_index! or the :force option to create it. (ArgumentError)
2016-09-01T00:13:00.947282+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.3.0/gems/elasticsearch-model-0.1.9/lib/elasticsearch/model.rb:116:in `import'
2016-09-01T00:13:00.947283+00:00 app[web.1]:    from /app/app/models/professor.rb:31:in `<top (required)>'

特别是这一行:

import': professors does not exist to be imported into. Use create_index! or the :force option to create it. (ArgumentError)

在Localhost上一切正常,但它与Heroku崩溃.我发现this question我认为与我的相似,其中一个答案是尝试:

$rails console

# Create the index for Service model on elasticsearch
> Service.__elasticsearch__.create_index!
=> {"ackNowledged"=>true}

# Import current Service records into the index
> Service.import
  Service Load (207.3ms)  SELECT  "services".* FROM "services"  ORDER BY "services"."id" ASC LIMIT 1000

# Sample search returning total results
> Service.__elasticsearch__.search("mykeyword").results.total
=> 123

但每当我在Heroku中运行rails控制台时,heroku运行rails控制台我得到了几乎相同的错误

/app/vendor/bundle/ruby/2.3.0/gems/elasticsearch-model-0.1.9/lib/elasticsearch/model/importing.rb:118:in `import': professors does not exist to be imported into. Use create_index! or the :force option to create it. (ArgumentError)
from /app/vendor/bundle/ruby/2.3.0/gems/elasticsearch-model-0.1.9/lib/elasticsearch/model.rb:116:in `import'

解决方法

只需使用Service.import(force:true)即可​​导入数据.

如果索引已经存在,它将删除索引并在导入数据之前重新创建它.

如果索引不存在,它将直接创建索引和导入数据.

所以不需要服务.__ elasticsearch __.create_index!了.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...