2015-04-12 27 views
0
  • 擁有Rails 4應用程序,想要使用ElasticSearch添加全文搜索。
  • 該應用程序運行正常在本地使用的工頭,但提交的在Heroku上部署版本的搜索 查詢時,我得到一個錯誤:(參見下面的完整的錯誤日誌) 「IndexMissingException」
  • 我認爲這個問題是有關我沒有正確指向heroku上的ElasticSearch索引。我試過了,但說實話,我並不完全理解「使用索引部分」,我認爲這是問題的根源。

任何建議,可以提供正確的設置與Heroku的ElasticSearch的Rails 4應用程序將不勝感激!Heroku上的Rails 4應用程序獲得Elasticsearch IndexMissingException

這裏有一些細節,希望可以幫助調試。

的Ruby/Rails版本

ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin14.0] 
Rails 4.2.1 

從Heroku的日誌

2015-04-12T20:52:36.506728+00:00 app[web.1]: Started GET "/posts?utf8=%E2%9C%93&q=better" for 98.14.231.17 at 2015-04-12 20:52:36 +0000 
2015-04-12T20:52:36.553113+00:00 app[web.1]: 
2015-04-12T20:52:36.553116+00:00 app[web.1]: ActionView::Template::Error ([404] {"error":"IndexMissingException[[posts] missing]","status":404}): 
2015-04-12T20:52:36.553118+00:00 app[web.1]:  7: <% end %> 
2015-04-12T20:52:36.553120+00:00 app[web.1]:  8: 
2015-04-12T20:52:36.553131+00:00 app[web.1]: 
2015-04-12T20:52:36.553121+00:00 app[web.1]:  9: <ul> 
2015-04-12T20:52:36.553123+00:00 app[web.1]:  10:   <% @posts.each do |post| %> 
2015-04-12T20:52:36.550831+00:00 app[web.1]: Rendered posts/index.html.erb within layouts/application (27.2ms) 
2015-04-12T20:52:36.553124+00:00 app[web.1]:  11:     <li> 
2015-04-12T20:52:36.553126+00:00 app[web.1]:  12:         <%= post.content %> 
2015-04-12T20:52:36.553129+00:00 app[web.1]: app/views/posts/index.html.erb:10:in `_app_views_posts_index_html_erb___4115668764297473519_69883416675900' 
2015-04-12T20:52:36.553127+00:00 app[web.1]:  13:     </li> 
2015-04-12T20:52:36.553132+00:00 app[web.1]: 
2015-04-12T20:52:36.549950+00:00 app[web.1]: Post Search (18.6ms) {index: "posts", type: "post", q: "better"} 
2015-04-12T20:52:36.516050+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "q"=>"better"} 
2015-04-12T20:52:36.515947+00:00 app[web.1]: Processing by PostsController#index as HTML 
2015-04-12T20:52:36.551104+00:00 app[web.1]: Completed 500 Internal Server Error in 35ms (ActiveRecord: 0.0ms | Elasticsearch: 18.6ms) 

的Gemfile

source 'https://rubygems.org' 

ruby '2.1.3' 
gem 'rails', '4.2.1' 
gem 'pg' 
gem 'sass-rails', '~> 5.0' 
gem 'uglifier', '>= 1.3.0' 
gem 'coffee-rails', '~> 4.1.0' 
gem 'jquery-rails' 
gem 'turbolinks' 
gem 'jbuilder', '~> 2.0' 
gem 'sdoc', '~> 0.4.0', group: :doc 

group :development, :test do 
    gem 'byebug' 
    gem 'web-console', '~> 2.0' 
    gem 'spring' 
end 

gem 'rails_12factor', group: :production 
gem "figaro" 
gem 'annotate', '~> 2.6.5', group: :development 
gem 'pry-rails', group: :development 
gem 'bootstrap-sass', '~> 3.3.4' 
gem 'autoprefixer-rails' 
gem 'font-awesome-sass' 
gem 'ffaker' 
gem 'react-rails', '~> 1.0.0.pre', github: 'reactjs/react-rails' 
gem 'thin' 
gem 'elasticsearch-model' 
gem 'elasticsearch-rails' 
gem 'bonsai-elasticsearch-rails' 

型號

require 'elasticsearch/model' 

class Post < ActiveRecord::Base 
    include Elasticsearch::Model 
    include Elasticsearch::Model::Callbacks 
end 

Post.import 

控制器

class PostsController < ApplicationController 
    def index 
     if params[:q].nil? 
     @posts = [] 
    else 
     @posts = Post.search(params[:q]) 
    end 
    end 
end 

查看

<%= form_for posts_path, method: 'get' do |f| %> 
    <%= f.label "Search for" %> 
    <%= text_field_tag :q, params[:q] %> 
    <%= submit_tag "Go", name: nil %> 
    <% end %> 

    <ul> 
  <% @posts.each do |post| %> 
    <li> 
        <%= post.content %> 
    </li> 
  <% end %> 
</ul> 

Heroku的盆景附加組件

我跟着instructions at Heroku for installing Bonsai

創建索引

curl -X POST http://[[MY BONSAI URL]] 

,並得到:

{"ok":true,"acknowledged":true} 

正如我所說,我認爲這個問題是我不能正常使用綁定到該/指點到索引。任何意見,將不勝感激!

回答

0

的問題是,我沒有從我在Heroku模型進口記錄。這首先涉及到確保你需要elasticsearch rake任務,然後用一個力在Heroku上運行導入任務= true參數

  1. https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-rails
  2. 向下滾動到Rake任務,並按照指示。
  3. 當 在Heroku進口的記錄,一定要包括FORCE = TRUE,如

    $ Heroku上運行包的exec耙環境elasticsearch:進口:型號CLASS = '文章' FORCE =真

0

我有類似的問題。對我來說,導入修正了錯誤。

bundle exec rake environment elasticsearch:import:all 
相關問題