简体   繁体   中英

Elasticsearch::Transport::Transport::Errors on Heroku with ActiveAdmin, SearchKick and SearchBox

I have a Rails App with a Post Model and Controller and I'm using ActiveAdmin for CMS. I have implemented ElasticSearch and SearchKick and am now attempting to deploy to Heroku using SearchBox. The app runs on local no issues and the majority of the functionality works on Heroku but I am getting a very annoying error when I Update or Create Post in Active Admin.

Elasticsearch::Transport::Transport::Errors::NotFound in Admin::PostsController#update [404] {"error":{"root_cause":[{"type":"document_missing_exception","reason":"[post][11]: document missing","index":"posts","shard":"0"}],"type":"document_missing_exception","reason":"[post][11]: document missing","index":"posts","shard":"0"},"status":404}

Even though it is throwing this error the Post is still either Updating or Creating fine. If I refresh the page it resolves to the view all posts screen in ActiveAdmin as expected. The search functionality is fully operational on the front end.

In SearchBox the ElasticSearch index is named posts_production_20160220081603930. Can't work out how to make ActiveAdmin see it. The Post Model sees it and I am able to search as expected.

Post Controller...

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

Post Model

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

To create the original index I ran

heroku run rake searchkick:reindex CLASS=Post

In dev the index gets updated automatically but in prod I have to reindex manually. Haven't setup sidekiq yet.

Any help would be greatly appreciated.

Am going a bit mad.

Cheers

Dan

Solved issue... I had used Searchkick to create and index however quirkily it would appear that an index also needs to be created using Post. elasticsearch .create_index! force: true ie the non Searchkick command. So the indices in Searchbox are posts and posts_production

All fully operational.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM