【发布时间】:2018-09-04 23:46:05
【问题描述】:
- 有一个 Rails 4 应用,想使用 ElasticSearch 添加全文搜索。
- 应用程序在本地使用工头运行良好,但在提交搜索时 在heroku上的部署版本中查询我得到一个错误: “IndexMissingException”(请参阅下面的完整错误日志。)
- 我认为这个问题与我没有正确指向 heroku 上的 ElasticSearch 索引有关。我曾尝试关注heroku instructions,但老实说,我并不完全理解“使用索引部分”,我认为这是问题的根源。
如果能提供任何关于在 Heroku 上使用 ElasticSearch 正确设置 Rails 4 应用程序的建议,我们将不胜感激!
这里有一些细节,希望可以帮助调试。
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)
宝石文件
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}
正如我所提到的,我认为问题与我没有正确使用/指向索引有关。任何建议将不胜感激!
【问题讨论】:
标签: ruby-on-rails heroku elasticsearch