forked from toptal/chewy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping.rb
211 lines (198 loc) · 7.66 KB
/
mapping.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
module Chewy
class Type
module Mapping
extend ActiveSupport::Concern
included do
class_attribute :root_object, instance_reader: false, instance_writer: false
class_attribute :_templates
class_attribute :_agg_defs
self._agg_defs = {}
class_attribute :outdated_sync_field
self.outdated_sync_field = :updated_at
end
module ClassMethods
# Defines root object for mapping and is optional for type
# definition. Use it only if you need to pass options for root
# object mapping, such as `date_detection` or `dynamic_date_formats`
#
# class UsersIndex < Chewy::Index
# define_type User do
# # root object defined implicitly and optionless for current type
# field :full_name, type: 'string'
# end
# end
#
# class CarsIndex < Chewy::Index
# define_type Car do
# # explicit root definition with additional options
# root dynamic_date_formats: ['yyyy-MM-dd'] do
# field :model_name, type: 'string'
# end
# end
# end
#
def root(options = {}, &block)
raise 'Root is already defined' if root_object
build_root(options, &block)
end
# Defines mapping field for current type
#
# class UsersIndex < Chewy::Index
# define_type User do
# # passing all the options to field definition:
# field :full_name, type: 'string', analyzer: 'special'
# end
# end
#
# The `type` is optional and defaults to `string` if not defined:
#
# field :full_name
#
# Also, multiple fields might be defined with one call and
# with the same options:
#
# field :first_name, :last_name, analyzer: 'special'
#
# The only special option in the field definition
# is `:value`. If no `:value` specified then just corresponding
# method will be called for the indexed object. Also
# `:value` might be a proc or indexed object method name:
#
# class User < ActiveRecord::Base
# def user_full_name
# [first_name, last_name].join(' ')
# end
# end
#
# field :full_name, type: 'string', value: :user_full_name
#
# The proc evaluates inside the indexed object context if
# its arity is 0 and in present contexts if there is an argument:
#
# field :full_name, type: 'string', value: -> { [first_name, last_name].join(' ') }
#
# separator = ' '
# field :full_name, type: 'string', value: ->(user) { [user.first_name, user.last_name].join(separator) }
#
# If array was returned as value - it will be put in index as well.
#
# field :tags, type: 'string', value: -> { tags.map(&:name) }
#
# Fields supports nesting in case of `object` field type. If
# `user.quiz` will return an array of objects, then result index content
# will be an array of hashes, if `user.quiz` is not a collection association
# then just values hash will be put in the index.
#
# field :quiz do
# field :question, :answer
# field :score, type: 'integer'
# end
#
# Nested fields are composed from nested objects:
#
# field :name, value: -> { name_translations } do
# field :ru, value: ->(name) { name['ru'] }
# field :en, value: ->(name) { name['en'] }
# end
#
# Of course it is possible to define object fields contents dynamically
# but make sure evaluation proc returns hash:
#
# field :name, type: 'object', value: -> { name_translations }
#
# The special case is multi_field. If type options and block are
# both present field is treated as a multi-field. In that case field
# composition changes satisfy elasticsearch rules:
#
# field :full_name, type: 'string', analyzer: 'name', value: ->{ full_name.try(:strip) } do
# field :sorted, analyzer: 'sorted'
# end
#
def field(*args, &block)
options = args.extract_options!
build_root
if args.size > 1
args.map { |name| field(name, options) }
else
expand_nested(Chewy::Fields::Base.new(args.first, options), &block)
end
end
# Defines an aggregation that can be bound to a query or filter
#
# Suppose that a user has posts and each post has ratings
# avg_post_rating is the mean of all ratings
#
# class UsersIndex < Chewy::Index
# define_type User do
# field :posts do
# field :rating
# end
#
# agg :avg_rating do
# { avg: { field: 'posts.rating' } }
# end
# end
# end
def agg(name, &block)
build_root
self._agg_defs = _agg_defs.merge(name => block)
end
alias_method :aggregation, :agg
# Defines dynamic template in mapping root objects
#
# class CarsIndex < Chewy::Index
# define_type Car do
# template 'model.*', type: 'string', analyzer: 'special'
# field 'model', type: 'object' # here we can put { de: 'Der Mercedes', en: 'Mercedes' }
# # and template will be applyed to this field
# end
# end
#
# Name for each template is generated with the following
# rule: "template_#{dynamic_templates.size + 1}".
#
# template 'tit*', mapping_hash
# template 'title.*', mapping_hash # dot in template causes "path_match" using
# template /tit.+/, mapping_hash # using "match_pattern": "regexp"
# template /title\..+/, mapping_hash # "\." - escaped dot causes "path_match" using
# template /tit.+/, 'string', mapping_hash # "match_mapping_type" as the optionsl second argument
# template template42: {match: 'hello*', mapping: {type: 'object'}} # or even pass a template as is
#
def template(*args)
build_root.dynamic_template(*args)
end
alias_method :dynamic_template, :template
# Returns compiled mappings hash for current type
#
def mappings_hash
root_object ? root_object.mappings_hash : {}
end
# Check whether the type has outdated_sync_field defined with a simple value.
#
# @return [true, false]
def supports_outdated_sync?
updated_at_field = root_object.child_hash[outdated_sync_field] if root_object && outdated_sync_field
!!updated_at_field && updated_at_field.value.nil?
end
private
def expand_nested(field, &block)
if @_current_field
field.parent = @_current_field
@_current_field.children.push(field)
end
return unless block
previous_field = @_current_field
@_current_field = field
yield
@_current_field = previous_field
end
def build_root(options = {}, &block)
return root_object if root_object
self.root_object = Chewy::Fields::Root.new(type_name, Chewy.default_root_options.merge(options))
expand_nested(root_object, &block)
@_current_field = root_object
end
end
end
end
end