Implementing elasticsearch index update in ruby on rails application

197 Views Asked by At

I am creating a rails application with integration to elasticsearch for fulltext search. I have the search working for a model, however when another part of the app does an attribute_update on it, I get below error:

Elasticsearch::Transport::Transport::Errors::BadRequest ([400] {"error":"no handler found for uri [/cards/_doc/123/_update] and method [POST]"}):

I tried to add a call to update_document in the model, however, I still get the same error as above.

class Card < ApplicationRecord
  include Searchable
  validates :attr1, uniqueness: true
  after_update :my_es_update

  def my_es_update
    self.__elasticsearch__.update_document
  end


  def self.es_full_search
    response = Card.__elasticsearch__.search(
      query: {
        match_all: {}
      },
      size: 150
    ).results
    @cards = response.results
  end

Update call:

  def pin
    @card = Card.find(params[:id])
    @card.update_attribute("pinned", true)
    redirect_back(fallback_location: root_path)
  end

What would be the right way to update the index in ES upon update to persistence layer?

Edit:

I have the Elasticsearch::Model::Callbacks module included in my model, per the documentation it should take care of updates...I am not sure what part of the setup I am missing here.

module Searchable
    extend ActiveSupport::Concern
  
    included do
      include Elasticsearch::Model
      include Elasticsearch::Model::Callbacks
  
      mapping do
        indexes :name, type: :text
        indexes :type, type: :text
        indexes :size, type: :text
      end

    end
end

Update2:

I am using ES vsn 8.5.3 and the 2 below gems in my Gemfile for communicating with it:

gem 'elasticsearch'
gem 'elasticsearch-model', '~> 7.2.1'
0

There are 0 best solutions below