我有一个带有帖子模型和控制器的Rails应用程序,我使用ActiveAdmin for CMS。我已经实现了ElasticSearch和SearchKick,现在正尝试使用SearchBox部署到Heroku。 该应用程序在本地运行没有问题,大部分功能在Heroku上运行,但是当我在Active Admin中更新或创建帖子时,我收到一个非常烦人的错误。
Elasticsearch :: Transport :: Transport :: Errors :: NotFound in Admin :: PostsController #update [404] {"错误" {" ROOT_CAUSE":[{"类型":" document_missing_exception""理由":& #34; [交] [11]: 文献 缺失""指数":"帖子""碎片":" 0"}],"类型&#34 ;:" document_missing_exception""理由":" [交] [11]: 文件遗失","索引":"帖子","碎片":" 0"},"状态&#34 ;:404}
即使它抛出此错误,Post仍然是更新或创建正常。如果我刷新页面,它将按预期解析为ActiveAdmin中的所有帖子屏幕。搜索功能在前端完全可用。
在SearchBox中,ElasticSearch索引名为posts_production_20160220081603930。无法解决如何让ActiveAdmin看到它。 Post Model看到了它,我能够按预期进行搜索。
后控制器...
class PostsController < ApplicationController
before_filter :find_post, :only => [:show, :edit, :update, :destroy]
def index
@post = Post.all
if params[:q].present?
@postsearch = Post.search params[:q], fields: [:title, :body], operator: "or", suggest: true
end
end
def show
if params[:q].present?
@postsearch = Post.search params[:q], fields: [:title, :body], operator: "or", suggest: true
else
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
end
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
format.json { render :json => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.json { render :json => @post.errors, :status => :unprocessable_entity }
end
end
end
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
flash[:notice] = 'Post was successfully updated.'
format.html { redirect_to(@post) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors,
:status => :unprocessable_entity }
end
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
private
def find_post
@post = Post.find(params[:id])
end
end
发布模型
require 'elasticsearch/model'
class Post < ActiveRecord::Base
searchkick suggest: [:title]
#add attachement declaration to moidels for refile image uploading
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
validates_presence_of :title, :body
attachment :profile_image
attachment :image
end
ActiveAdmin Post
ActiveAdmin.register Post do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
permit_params :title, :body, :profile_image, :image
form do |f|
inputs 'Details' do
input :title
input :body, :input_html => {:class => "redactor"}
input :profile_image, :required => false, :as => :file, destroy: false, :direct => true
input :image, :required => false, :as => :file, destroy: false, :direct => true
actions
end
end
end
要创建我原来的索引
heroku run rake searchkick:reindex CLASS = Post
在dev中,索引会自动更新,但在prod中我必须手动重新索引。 Haven尚未设置sidekiq。
非常感谢任何帮助。
我有点生气。
干杯
丹
答案 0 :(得分:1)
解决了问题...... 我曾经使用过Searchkick进行创建和索引,但奇怪的是,似乎还需要使用Post创建一个索引。 elasticsearch .create_index! force:true即非Searchkick命令。 所以Searchbox中的索引是 帖子 和 posts_production
全部全面运作。