Module: Tire::Model::Indexing::ClassMethods

Included in:
Search::ClassMethodsProxy
Defined in:
lib/tire/model/indexing.rb

Instance Method Summary collapse

Instance Method Details

#create_elasticsearch_indexObject

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



106
107
108
109
110
111
112
113
# File 'lib/tire/model/indexing.rb', line 106

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 = {}, &block) ⇒ Object

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.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tire/model/indexing.rb', line 86

def indexes(name, options = {}, &block)
  mapping[name] = options

  if block_given?
    mapping[name][:type]       ||= 'object'
    mapping[name][:properties] ||= {}

    previous = @mapping
    @mapping = mapping[name][:properties]
    yield
    @mapping = previous
  end

  mapping[name][:type] ||= 'string'

  self
end

#mapping(*args) ⇒ Object

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'

    indexes :comments do
      indexes :body
      indexes :author do
        indexes :name
      end
    end

    # ...
  end
end


56
57
58
59
60
61
62
63
64
65
# File 'lib/tire/model/indexing.rb', line 56

def mapping(*args)
  @mapping ||= {}
  if block_given?
    @mapping_options = args.pop
    yield
    create_elasticsearch_index
  else
    @mapping
  end
end

#mapping_optionsObject



115
116
117
# File 'lib/tire/model/indexing.rb', line 115

def mapping_options
  @mapping_options || {}
end

#mapping_to_hashObject



119
120
121
# File 'lib/tire/model/indexing.rb', line 119

def mapping_to_hash
  { document_type.to_sym => mapping_options.merge({ :properties => mapping }) }
end

#settings(*args) ⇒ Object

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


24
25
26
27
28
# File 'lib/tire/model/indexing.rb', line 24

def settings(*args)
  @settings ||= {}
  args.empty?  ? (return @settings) : @settings = args.pop
  yield if block_given?
end