Skip to content

Commit d527240

Browse files
committed
[MODEL] Added mappings and settings method to set up index options for the model
Example: class Article mapping dynamic: 'strict' do indexes :foo do indexes :bar end indexes :baz end end > Article.mapping.to_hash # => {:article => {:dynamic=>"strict", :properties=> {:foo=>{:type=>"object", :properties=>{:bar=>{:type=>"string"}}}, :baz=>{:type=>"string"}}}} When the model would define the `mapping(s)` or `settings` method, access the interface through the `__elasticsearch__` proxy: class Article __elasticsearch__ do mapping dynamic: 'strict' do indexes :foo do indexes :bar end indexes :baz end end end Of course, you can call the method directly: Article.mapping(dynamic: 'strict') { indexes :foo, type: 'long' } > Article.mapping.to_hash { :dynamic=>"strict", :properties=>{:foo=>{:type=>"long"}}}}
1 parent ee24edc commit d527240

File tree

2 files changed

+154
-3
lines changed

2 files changed

+154
-3
lines changed

elasticsearch-model/lib/elasticsearch/model.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def self.included(base)
8181
end
8282

8383
extend Support::Forwardable
84-
forward :'self.__elasticsearch__', :search unless respond_to?(:search)
84+
forward :'self.__elasticsearch__', :search unless respond_to?(:search)
85+
forward :'self.__elasticsearch__', :mapping unless respond_to?(:mapping)
86+
forward :'self.__elasticsearch__', :settings unless respond_to?(:settings)
8587
end
8688
end
8789

elasticsearch-model/lib/elasticsearch/model/indexing.rb

+151-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,162 @@
11
module Elasticsearch
22
module Model
3+
4+
# This module provides the necessary support to set up index options (mappings, settings)
5+
# as well as instance methods to create, update or delete documents in the index.
6+
#
37
module Indexing
48

9+
# Wraps the [index settings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-configuration.html#configuration-index-settings)
10+
#
11+
class Settings
12+
attr_accessor :settings
13+
14+
def initialize(settings={})
15+
@settings = settings
16+
end
17+
18+
def to_hash
19+
@settings
20+
end
21+
22+
def as_json(options={})
23+
to_hash
24+
end
25+
end
26+
27+
# Wraps the [index mappings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping.html)
28+
#
29+
class Mappings
30+
attr_accessor :options
31+
32+
def initialize(type, options={})
33+
@type = type
34+
@options = options
35+
@mapping = {}
36+
end
37+
38+
def indexes(name, options = {}, &block)
39+
@mapping[name] = options
40+
41+
if block_given?
42+
@mapping[name][:type] ||= 'object'
43+
@mapping[name][:properties] ||= {}
44+
45+
previous = @mapping
46+
begin
47+
@mapping = @mapping[name][:properties]
48+
self.instance_eval(&block)
49+
ensure
50+
@mapping = previous
51+
end
52+
end
53+
54+
# Set the `type` to string by default
55+
#
56+
@mapping[name][:type] ||= 'string'
57+
58+
self
59+
end
60+
61+
def to_hash
62+
{ @type.to_sym => @options.merge( properties: @mapping ) }
63+
end
64+
65+
def as_json(options={})
66+
to_hash
67+
end
68+
end
69+
570
module ClassMethods
71+
72+
# Defines mappings for the index
73+
#
74+
# @example Define mapping for model
75+
#
76+
# class Article
77+
# mapping dynamic: 'strict' do
78+
# indexes :foo do
79+
# indexes :bar
80+
# end
81+
# indexes :baz
82+
# end
83+
# end
84+
#
85+
# Article.mapping.to_hash
86+
#
87+
# # => { :article =>
88+
# # { :dynamic => "strict",
89+
# # :properties=>
90+
# # { :foo => {
91+
# # :type=>"object",
92+
# # :properties => {
93+
# # :bar => { :type => "string" }
94+
# # }
95+
# # }
96+
# # },
97+
# # :baz => { :type=> "string" }
98+
# # }
99+
# # }
100+
#
101+
# @example Define index settings and mappings
102+
#
103+
# class Article
104+
# settings number_of_shards: 1 do
105+
# mappings do
106+
# indexes :foo
107+
# end
108+
# end
109+
# end
110+
#
111+
# @example Call the mapping method directly
112+
#
113+
# Article.mapping(dynamic: 'strict') { indexes :foo, type: 'long' }
114+
#
115+
# Article.mapping.to_hash
116+
#
117+
# # => {:article=>{:dynamic=>"strict", :properties=>{:foo=>{:type=>"long"}}}}
118+
#
119+
# The `mappings` and `settings` methods are accessible directly on the model class,
120+
# when it doesn't already defines them. Use the `__elasticsearch__` proxy otherwise.
121+
#
122+
def mapping(options={}, &block)
123+
@mapping ||= Mappings.new(document_type, options)
124+
125+
if block_given?
126+
@mapping.options.update(options)
127+
128+
@mapping.instance_eval(&block)
129+
return self
130+
else
131+
@mapping
132+
end
133+
end; alias_method :mappings, :mapping
134+
135+
# Define settings for the index
136+
#
137+
# @example Define index settings
138+
#
139+
# Article.settings(index: { number_of_shards: 1 })
140+
#
141+
# Article.settings.to_hash
142+
#
143+
# # => {:index=>{:number_of_shards=>1}}
144+
#
145+
def settings(settings={}, &block)
146+
@settings ||= Settings.new(settings)
147+
148+
@settings.settings.update(settings) unless settings.empty?
149+
150+
if block_given?
151+
self.instance_eval(&block)
152+
return self
153+
else
154+
@settings
155+
end
156+
end
6157
end
7158

8159
module InstanceMethods
9-
10160
def index_document(options={})
11161
document = self.as_indexed_json
12162

@@ -31,7 +181,6 @@ def update_document(options={})
31181
# to perform update by partial doc.
32182
index_document(options)
33183
end
34-
35184
end
36185

37186
end

0 commit comments

Comments
 (0)