Module: Tire::Model::Indexing::ClassMethods
- Included in:
- Search::ClassMethodsProxy
- Defined in:
- lib/tire/model/indexing.rb
Instance Method Summary collapse
-
#create_elasticsearch_index ⇒ Object
Creates the corresponding index with desired settings and mappings, when it does not exists yet.
-
#indexes(name, options = {}, &block) ⇒ Object
Define mapping for the property passed as the first argument (
name
) using definition from the second argument (options
). -
#mapping(*args) ⇒ Object
Define the mapping 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.
- #mapping_options ⇒ Object
- #mapping_to_hash ⇒ Object
-
#settings(*args) ⇒ Object
Define settings for the corresponding index, such as number of shards and replicas, custom analyzers, etc.
Instance Method Details
#create_elasticsearch_index ⇒ Object
Creates the corresponding index with desired settings and mappings, when it does not exists yet.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/tire/model/indexing.rb', line 106 def create_elasticsearch_index unless index.exists? new_index = index unless result = new_index.create(:mappings => mapping_to_hash, :settings => settings) STDERR.puts "[ERROR] There has been an error when creating the index -- elasticsearch returned:", new_index.response result end end rescue Errno::ECONNREFUSED => e STDERR.puts "Skipping index creation, cannot connect to Elasticsearch", "(The original exception was: #{e.inspect})" false 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 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, = {}, &block) mapping[name] = 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 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_options ⇒ Object
121 122 123 |
# File 'lib/tire/model/indexing.rb', line 121 def @mapping_options || {} end |
#mapping_to_hash ⇒ Object
125 126 127 |
# File 'lib/tire/model/indexing.rb', line 125 def mapping_to_hash { document_type.to_sym => .merge({ :properties => mapping }) } end |
#settings(*args) ⇒ Object
Define settings 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 |