Sha256: c389a40b554cfe363a1231485a732e428787ad19fe3eea252d6d48524e052e19
Contents?: true
Size: 1.74 KB
Versions: 25
Compression:
Stored size: 1.74 KB
Contents
require 'elasticsearch/model' require 'custom_attributes/fluent_search_query' module CustomAttributes module Searchable extend ActiveSupport::Concern included do include Elasticsearch::Model index_name "#{CustomAttributes.config[:search_index_prefix]}-#{name.downcase.pluralize}-#{Rails.env}" # For ActiveRecord-based models, use the after_commit callback to # protect your data against inconsistencies caused by transaction rollbacks if any? { |obj| obj.is_a?(ActiveRecord::Base) } after_commit on: [:create] do index_document_or_fail end after_commit on: [:update] do index_document_or_fail end after_commit on: [:destroy] do delete_document_or_fail end else # everything else can use the standard implementation include Elasticsearch::Model::Callbacks end # helper function to initialize index def self.set_up_search __elasticsearch__.create_index! __elasticsearch__.refresh_index! import end # entrypoint for the fluent query builder def self.searchable CustomAttributes::FluentSearchQuery.new new end # default connection error handling, override in model! def self.handle_search_connection_error(exception) raise exception end private def index_document_or_fail __elasticsearch__.index_document rescue Faraday::ConnectionFailed => e self.class.handle_search_connection_error(e) end def delete_document_or_fail __elasticsearch__.delete_document rescue Faraday::ConnectionFailed => e self.class.handle_search_connection_error(e) end end end end
Version data entries
25 entries across 25 versions & 1 rubygems