module Elasticsearchable

For details, see github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/README.md

Public Instance Methods

add_to_index() click to toggle source
# File lib/buweb/concerns/elasticsearchable.rb, line 56
def add_to_index
  added =
    if dont_index?
      logger.debug "This object is set to not be indexed."
      remove_from_index
      false
    else
      __elasticsearch__.index_document
      true
    end

  reindex_dependencies

  added
end
as_indexed_json(*) click to toggle source

Overwrite this in the model It needs to respond to one argument, an options hash. But we don't really care about that so I am using an asterisk to accept whatever parameters

# File lib/buweb/concerns/elasticsearchable.rb, line 23
def as_indexed_json(*)
  raise NotImplementedError, "#as_indexed_json should be overriden in #{self.class}"
  # searchable_attribute: value,
  # Make sure normalized data exists and is in this format for search results
  # normalized_data: {
  #   title: nil,
  #   subtitles: [],
  #   short_description: nil,  # this should just be a really short description.
  #   location: nil,
  #   image_url: nil,
  #   phone: nil,
  #   alternate_phone: nil,
  #   email: nil,
  # }
end
dependent_indexes() click to toggle source

This should be overridden in the models if there are any dependent indexes. A dependent index is a association that should be reindexed when this model is reindexed. Example: [:user, :departments]

# File lib/buweb/concerns/elasticsearchable.rb, line 52
def dependent_indexes
  []
end
dont_index?() click to toggle source

This can be overridden if it should be true.

# File lib/buweb/concerns/elasticsearchable.rb, line 40
def dont_index?
  false
end
remove_from_index() click to toggle source
# File lib/buweb/concerns/elasticsearchable.rb, line 72
def remove_from_index
  begin
    __elasticsearch__.delete_document
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
    # By default, elasticsearch will complain if it can't find the object you are trying to remove
    #   from the index, but we don't really care since we are trying to remove the object from the index anyways.
    logger.debug "Object was not removed from Elasticsearch because it couldn't be found"
  end

  reindex_dependencies
end

Protected Instance Methods

reindex_dependencies() click to toggle source
# File lib/buweb/concerns/elasticsearchable.rb, line 86
def reindex_dependencies
  dependent_indexes.each do |association_name|
    association = send(association_name)

    if association.respond_to? :each
      association.each do |assoc|
        assoc.add_to_index if assoc.present?
      end
    elsif !association.nil?
      association.add_to_index
    end
  end
end