lib/elasticsearch/model/indexing.rb in elasticsearch-model-0.1.7 vs lib/elasticsearch/model/indexing.rb in elasticsearch-model-0.1.8
- old
+ new
@@ -32,26 +32,29 @@
end
# Wraps the [index mappings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping.html)
#
class Mappings
- attr_accessor :options
+ attr_accessor :options, :type
+ # @private
+ TYPES_WITH_EMBEDDED_PROPERTIES = %w(object nested)
+
def initialize(type, options={})
raise ArgumentError, "`type` is missing" if type.nil?
@type = type
@options = options
@mapping = {}
end
- def indexes(name, options = {}, &block)
+ def indexes(name, options={}, &block)
@mapping[name] = options
if block_given?
@mapping[name][:type] ||= 'object'
- properties = @mapping[name][:type] == 'multi_field' ? :fields : :properties
+ properties = TYPES_WITH_EMBEDDED_PROPERTIES.include?(@mapping[name][:type]) ? :properties : :fields
@mapping[name][properties] ||= {}
previous = @mapping
begin
@@ -61,11 +64,10 @@
@mapping = previous
end
end
# Set the type to `string` by default
- #
@mapping[name][:type] ||= 'string'
self
end
@@ -126,18 +128,18 @@
# Article.mapping.to_hash
#
# # => {:article=>{:dynamic=>"strict", :properties=>{:foo=>{:type=>"long"}}}}
#
# The `mappings` and `settings` methods are accessible directly on the model class,
- # when it doesn't already defines them. Use the `__elasticsearch__` proxy otherwise.
+ # when it doesn't already define them. Use the `__elasticsearch__` proxy otherwise.
#
def mapping(options={}, &block)
@mapping ||= Mappings.new(document_type, options)
- if block_given?
- @mapping.options.update(options)
+ @mapping.options.update(options) unless options.empty?
+ if block_given?
@mapping.instance_eval(&block)
return self
else
@mapping
end
@@ -151,11 +153,43 @@
#
# Article.settings.to_hash
#
# # => {:index=>{:number_of_shards=>1}}
#
+ # You can read settings from any object that responds to :read
+ # as long as its return value can be parsed as either YAML or JSON.
+ #
+ # @example Define index settings from YAML file
+ #
+ # # config/elasticsearch/articles.yml:
+ # #
+ # # index:
+ # # number_of_shards: 1
+ # #
+ #
+ # Article.settings File.open("config/elasticsearch/articles.yml")
+ #
+ # Article.settings.to_hash
+ #
+ # # => { "index" => { "number_of_shards" => 1 } }
+ #
+ #
+ # @example Define index settings from JSON file
+ #
+ # # config/elasticsearch/articles.json:
+ # #
+ # # { "index": { "number_of_shards": 1 } }
+ # #
+ #
+ # Article.settings File.open("config/elasticsearch/articles.json")
+ #
+ # Article.settings.to_hash
+ #
+ # # => { "index" => { "number_of_shards" => 1 } }
+ #
def settings(settings={}, &block)
+ settings = YAML.load(settings.read) if settings.respond_to?(:read)
@settings ||= Settings.new(settings)
@settings.settings.update(settings) unless settings.empty?
if block_given?
@@ -164,10 +198,14 @@
else
@settings
end
end
+ def load_settings_from_io(settings)
+ YAML.load(settings.read)
+ end
+
# Creates an index with correct name, automatically passing
# `settings` and `mappings` defined in the model
#
# @example Create an index for the `Article` model
#
@@ -184,15 +222,31 @@
def create_index!(options={})
target_index = options.delete(:index) || self.index_name
delete_index!(options.merge index: target_index) if options[:force]
- unless ( self.client.indices.exists(index: target_index) rescue false )
+ unless index_exists?(index: target_index)
self.client.indices.create index: target_index,
body: {
settings: self.settings.to_hash,
mappings: self.mappings.to_hash }
end
+ end
+
+ # Returns true if the index exists
+ #
+ # @example Check whether the model's index exists
+ #
+ # Article.__elasticsearch__.index_exists?
+ #
+ # @example Check whether a specific index exists
+ #
+ # Article.__elasticsearch__.index_exists? index: 'my-index'
+ #
+ def index_exists?(options={})
+ target_index = options[:index] || self.index_name
+
+ self.client.indices.exists(index: target_index) rescue false
end
# Deletes the index with corresponding name
#
# @example Delete the index for the `Article` model