0

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'
vinnar
  • 15
  • 2
  • 8
  • Which ES version are you running? – Val Feb 17 '23 at 12:59
  • I am using ES vsn 8.5.3 deployed as a docker container. – vinnar Feb 17 '23 at 22:18
  • I see you answered a similar question here. Could be the same issue, however the elasticsearch-model gem latest vsn is 7.2.1, there is not a 8.x vsn of it.. so am I limited to using ES vsn 7.x with rails... ? https://stackoverflow.com/questions/71812420/no-handler-found-for-uri-index-doc-document-id-update-and-method-post – vinnar Feb 17 '23 at 22:46
  • That might as well be the case... Compatibility table: https://github.com/elastic/elasticsearch-rails#compatibility – vinnar Feb 17 '23 at 22:50
  • Oh well, confirmed that. I downgraded to 7.17.0 and index update is working.. I guess we will just have to wait for them to release a v8.x compatible version. Thanks for pushing me in the right direction! – vinnar Feb 17 '23 at 23:04
  • Cool, glad you figured it out – Val Feb 18 '23 at 11:57

0 Answers0