module Tire::Model::Indexing::ClassMethods

Public Instance Methods

create_elasticsearch_index() click to toggle source

Creates the corresponding index with desired settings and mappings, when it does not exists yet.

# File lib/tire/model/indexing.rb, line 98
def create_elasticsearch_index
  unless index.exists?
    index.create :mappings => mapping_to_hash, :settings => settings
  end
rescue Errno::ECONNREFUSED => e
  STDERR.puts "Skipping index creation, cannot connect to ElasticSearch",
              "(The original exception was: #{e.inspect})"
end
indexes(name, options = {}) { || ... } click to toggle source

Define mapping for the property passed as the first argument (`name`) using definition from the second argument (`options`).

`:type` is optional and defaults to `'string'`.

Usage:

  • Index property but do not analyze it: `indexes :id, :index => :not_analyzed`

  • Use different analyzer for indexing a property: `indexes :title, :analyzer => 'snowball'`

  • Use the `:as` option to dynamically define the serialized property value, eg:

    :as => 'content.split(/\W/).length'
    

Please refer to the [mapping documentation](www.elasticsearch.org/guide/reference/mapping/index.html) for more information.

# File lib/tire/model/indexing.rb, line 78
def indexes(name, options = {}, &block)
  if block_given?
    mapping[name] ||= { :type => 'object', :properties => {} }.update(options)
    @_nested_mapping = name
    nested = yield
    @_nested_mapping = nil
    self
  else
    options[:type] ||= 'string'
    if @_nested_mapping
      mapping[@_nested_mapping][:properties][name] = options
    else
      mapping[name] = options
    end
    self
  end
end
mapping(*args) { || ... } click to toggle source

Define the [mapping](www.elasticsearch.org/guide/reference/mapping/index.html) for the corresponding index, telling ElasticSearch how to understand your documents: what type is which property, whether it is analyzed or no, which analyzer to use, etc.

You may pass the top level mapping properties (such as `_source` or `_all`) as a Hash.

Usage:

class Article
  # ...
  mapping :_source => { :compress => true } do
    indexes :id,    :index    => :not_analyzed
    indexes :title, :analyzer => 'snowball', :boost => 100
    indexes :words, :as       => 'content.split(/\W/).length'
    # ...
  end
end
# File lib/tire/model/indexing.rb, line 48
def mapping(*args)
  @mapping ||= {}
  if block_given?
    @mapping_options = args.pop
    yield
    create_elasticsearch_index
  else
    @mapping
  end
end
mapping_options() click to toggle source
# File lib/tire/model/indexing.rb, line 107
def mapping_options
  @mapping_options || {}
end
mapping_to_hash() click to toggle source
# File lib/tire/model/indexing.rb, line 111
def mapping_to_hash
  { document_type.to_sym => mapping_options.merge({ :properties => mapping }) }
end
settings(*args) { || ... } click to toggle source

Define [settings](www.elasticsearch.org/guide/reference/api/admin-indices-create-index.html) for the corresponding index, such as number of shards and replicas, custom analyzers, etc.

Usage:

class Article
  # ...
  settings :number_of_shards => 1 do
    mapping do
      # ...
    end
  end
end
# File lib/tire/model/indexing.rb, line 24
def settings(*args)
  @settings ||= {}
  args.empty?  ? (return @settings) : @settings = args.pop
  yield if block_given?
end