module Esse::IndexType::ClassMethods

www.elastic.co/guide/en/elasticsearch/reference/master/indices-put-mapping.html

Public Instance Methods

action(name, options = {}, &block) click to toggle source
# File lib/esse/index_type/actions.rb, line 6
def action(name, options = {}, &block); end
backend() click to toggle source
# File lib/esse/index_type/backend.rb, line 6
def backend
  Esse::Backend::IndexType.new(self)
end
collection(&block) click to toggle source

Used to define the source of data. A block is required. And its content should yield an array of each object that should be serialized. The list of arguments will be passed throught the serializer method.

Here is an example of how this should work:

collection do |conditions, &block|
  User.where(conditions).find_in_batches(batch_size: 5000) do |batch|
    block.call(batch, conditions)
  end
end
# File lib/esse/index_type/serializer.rb, line 49
def collection(&block)
  raise(SyntaxError, 'No block given') unless block_given?

  @collection_proc = block
end
each_batch(*args, &block) click to toggle source

Used to fetch all batch of data defined on the collection model. Arguments can be anything. They will just be passed through the block. Useful when the collection depends on scope or any other conditions Example

each_batch(active: true) do |data, _opts|
  puts data.size
end
# File lib/esse/index_type/serializer.rb, line 62
def each_batch(*args, &block)
  unless @collection_proc
    raise NotImplementedError, format('there is no collection defined for the %<k>p index', k: to_s)
  end

  @collection_proc.call(*args, &block)
rescue LocalJumpError
  raise(SyntaxError, 'block must be explicitly declared in the collection definition')
end
each_serialized_batch(*args, &block) click to toggle source

Wrap collection data into serialized batches

@param args [*Object] Any argument is allowed here. The collection will be called with same arguments.

And the serializer will be initialized with those arguments too.

@yield [Array, *Object] serialized collection and method arguments

# File lib/esse/index_type/serializer.rb, line 77
def each_serialized_batch(*args, &block)
  each_batch(*args) do |batch, *serializer_args|
    entries = batch.map { |entry| serialize(entry, *serializer_args) }.compact
    block.call(entries, *args)
  end
end
mappings(hash = {}, &block) click to toggle source

This method is only used to define mapping

# File lib/esse/index_type/mappings.rb, line 8
def mappings(hash = {}, &block)
  @mapping = Esse::IndexMapping.new(body: hash, paths: template_dirs, filenames: mapping_filenames)
  return unless block_given?

  @mapping.define_singleton_method(:as_json, &block)
end
mappings_hash() click to toggle source

This is the actually content that will be passed through the ES api

# File lib/esse/index_type/mappings.rb, line 16
def mappings_hash
  hash = mapping.body
  {
    type_name => (hash.key?('properties') ? hash : { 'properties' => hash }),
  }
end
serialize(model, *args) click to toggle source
# File lib/esse/index_type/serializer.rb, line 31
def serialize(model, *args)
  unless @serializer_proc
    raise NotImplementedError, format('there is no serializer defined for the %<k>p index', k: to_s)
  end

  @serializer_proc.call(model, *args)
end
serializer(klass = nil, &block) click to toggle source

Convert ruby object to json. Arguments will be same of passed through the collection. It's allowed a block or a class with the `as_json` instance method. Example with block

serializer do |model, context = {}|
  {
    id: model.id,
    admin: context[:is_admin],
  }
end

Example with serializer class

serializer UserSerializer
# File lib/esse/index_type/serializer.rb, line 17
def serializer(klass = nil, &block)
  if block_given?
    @serializer_proc = block
  elsif klass.is_a?(Class) && klass.instance_methods.include?(:as_json)
    @serializer_proc = proc { |*args| klass.new(*args).as_json }
  elsif klass.is_a?(Class) && klass.instance_methods.include?(:call)
    @serializer_proc = proc { |*args| klass.new(*args).call }
  else
    msg = format('%<arg>p is not a valid serializer. The serializer should ' \
                 'respond with `as_json` instance method.', arg: klass,)
    raise ArgumentError, msg
  end
end

Private Instance Methods

mapping() click to toggle source
# File lib/esse/index_type/mappings.rb, line 25
def mapping
  @mapping ||= Esse::IndexMapping.new(paths: template_dirs, filenames: mapping_filenames)
end
mapping_filenames() click to toggle source
# File lib/esse/index_type/mappings.rb, line 35
def mapping_filenames
  Esse::IndexMapping::FILENAMES.map { |str| [type_name, str].join('_') }
end
template_dirs() click to toggle source
# File lib/esse/index_type/mappings.rb, line 29
def template_dirs
  return [] unless respond_to?(:index)

  index.template_dirs
end