Class: Chewy::Index
- Defined in:
- lib/chewy/index.rb,
lib/chewy/index/actions.rb,
lib/chewy/index/aliases.rb,
lib/chewy/index/settings.rb,
lib/chewy/index/specification.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Actions, Aliases Classes: Settings, Specification
Class Method Summary collapse
-
.base_name ⇒ String
Base name for the index.
- .build_index_name(*args) ⇒ Object
-
.define_type(target, options = {}, &block) ⇒ Object
Defines type for the index.
- .derivable_index_name ⇒ Object
-
.derivable_name ⇒ String?
Similar to the Index.base_name but respects the class namespace, also, can't be redefined.
- .index_name(suggest = nil, prefix: nil, suffix: nil) ⇒ Object
- .index_params ⇒ Object
- .mappings_hash ⇒ Object
-
.method_missing(name, *args, &block) ⇒ Object
Handling old default_prefix if it is not defined.
-
.prefix ⇒ String
Used as a default value for Index.index_name.
- .prefix_with_deprecation ⇒ Object
-
.scopes ⇒ Object
Returns list of public class methods defined in current index.
-
.settings(params = {}, &block) ⇒ Object
Used as a part of index definition DSL.
- .settings_hash ⇒ Object
-
.specification ⇒ Chewy::Index::Specification
A specification object instance for this particular index.
-
.specification_hash ⇒ Hash
Returns a hash containing the index settings and mappings Used for the ES index creation as body.
-
.type(type_name) ⇒ Object
Returns named type:.
-
.type_names ⇒ Object
Returns defined types names:.
-
.types(*args) ⇒ Object
Types method has double usage.
Class Method Details
.base_name ⇒ String
Base name for the index. Uses the default value inferred from the class name unless redefined.
77 78 79 80 81 |
# File 'lib/chewy/index.rb', line 77 def base_name @base_name ||= name.sub(/Index\z/, '').demodulize.underscore if name raise UndefinedIndex if @base_name.blank? @base_name end |
.build_index_name(*args) ⇒ Object
272 273 274 275 |
# File 'lib/chewy/index.rb', line 272 def build_index_name(*args) ActiveSupport::Deprecation.warn '`Chewy::Index.build_index_name` is deprecated and will be removed soon, use `Chewy::Index.index_name` instead' index_name(args.) end |
.define_type(target, options = {}, &block) ⇒ Object
Defines type for the index. Arguments depends on adapter used. For ActiveRecord you can pass model or scope and options
class CarsIndex < Chewy::Index define_type Car do ... end # defines VehiclesIndex::Car type end
Type name might be passed in complicated cases:
class VehiclesIndex < Chewy::Index define_type Vehicle.cars.includes(:manufacturer), name: 'cars' do ... end # defines VehiclesIndex::Cars type
define_type Vehicle.motocycles.includes(:manufacturer), name: 'motocycles' do
...
end # defines VehiclesIndex::Motocycles type
end
For plain objects:
class PlanesIndex < Chewy::Index define_type :plane do ... end # defines PlanesIndex::Plane type end
The main difference between using plain objects or ActiveRecord models for indexing
is import. If you will call CarsIndex::Car.import - it will import all the cars
automatically, while PlanesIndex::Plane.import(my_planes) requires import data to be
passed.
148 149 150 151 152 153 |
# File 'lib/chewy/index.rb', line 148 def define_type(target, = {}, &block) raise 'Multiple types are deprecated' if type_hash.present? type_class = Chewy.create_type(self, target, , &block) self.type_hash = type_hash.merge(type_class.type_name => type_class) end |
.derivable_index_name ⇒ Object
248 249 250 251 |
# File 'lib/chewy/index.rb', line 248 def derivable_index_name ActiveSupport::Deprecation.warn '`Chewy::Index.derivable_index_name` is deprecated and will be removed soon, use `Chewy::Index.derivable_name` instead' derivable_name end |
.derivable_name ⇒ String?
Similar to the base_name but respects the class namespace, also, can't be redefined. Used to reference index with the string identifier
94 95 96 |
# File 'lib/chewy/index.rb', line 94 def derivable_name @derivable_name ||= name.sub(/Index\z/, '').underscore if name end |
.index_name(suggest) ⇒ String .index_name(prefix: nil, suffix: nil) ⇒ String
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/chewy/index.rb', line 53 def index_name(suggest = nil, prefix: nil, suffix: nil) if suggest @base_name = suggest.to_s.presence else [ prefix || prefix_with_deprecation, base_name, suffix ].reject(&:blank?).join('_') end end |
.index_params ⇒ Object
237 238 239 240 |
# File 'lib/chewy/index.rb', line 237 def index_params ActiveSupport::Deprecation.warn '`Chewy::Index.index_params` is deprecated and will be removed soon, use `Chewy::Index.specification_hash`' specification_hash end |
.mappings_hash ⇒ Object
223 224 225 226 |
# File 'lib/chewy/index.rb', line 223 def mappings_hash mappings = types.map(&:mappings_hash).inject(:merge) mappings.present? ? {mappings: mappings} : {} end |
.method_missing(name, *args, &block) ⇒ Object
Handling old default_prefix if it is not defined.
254 255 256 257 258 259 260 261 |
# File 'lib/chewy/index.rb', line 254 def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing if name == :default_prefix ActiveSupport::Deprecation.warn '`Chewy::Index.default_prefix` is deprecated and will be removed soon, use `Chewy::Index.prefix` instead' prefix else super end end |
.prefix ⇒ String
Used as a default value for index_name. Return prefix from the configuration but can be redefined per-index to be more dynamic.
110 111 112 |
# File 'lib/chewy/index.rb', line 110 def prefix Chewy.configuration[:prefix] end |
.prefix_with_deprecation ⇒ Object
263 264 265 266 267 268 269 270 |
# File 'lib/chewy/index.rb', line 263 def prefix_with_deprecation if respond_to?(:default_prefix) ActiveSupport::Deprecation.warn '`Chewy::Index.default_prefix` is deprecated and will be removed soon, define `Chewy::Index.prefix` method instead' default_prefix else prefix end end |
.scopes ⇒ Object
Returns list of public class methods defined in current index
215 216 217 |
# File 'lib/chewy/index.rb', line 215 def scopes public_methods - Chewy::Index.public_methods end |
.settings(params = {}, &block) ⇒ Object
Used as a part of index definition DSL. Defines settings:
class UsersIndex < Chewy::Index settings analysis: { analyzer: { name: { tokenizer: 'standard', filter: ['lowercase', 'icu_folding', 'names_nysiis'] } } } end
See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html for more details
It is possible to store analyzers settings in Chewy repositories
and link them form index class. See Chewy::Index::Settings for details.
209 210 211 |
# File 'lib/chewy/index.rb', line 209 def settings(params = {}, &block) self._settings = Chewy::Index::Settings.new(params, &block) end |
.settings_hash ⇒ Object
219 220 221 |
# File 'lib/chewy/index.rb', line 219 def settings_hash _settings.to_hash end |
.specification ⇒ Chewy::Index::Specification
Returns a specification object instance for this particular index.
244 245 246 |
# File 'lib/chewy/index.rb', line 244 def specification @specification ||= Specification.new(self) end |
.specification_hash ⇒ Hash
Returns a hash containing the index settings and mappings Used for the ES index creation as body.
233 234 235 |
# File 'lib/chewy/index.rb', line 233 def specification_hash [settings_hash, mappings_hash].inject(:merge) end |
.type(type_name) ⇒ Object
Returns named type:
UserIndex.type('admin') # => UsersIndex::Admin
186 187 188 |
# File 'lib/chewy/index.rb', line 186 def type(type_name) type_hash.fetch(type_name) { raise UndefinedType, "Unknown type in #{name}: #{type_name}" } end |
.type_names ⇒ Object
Returns defined types names:
UsersIndex.type_names # => ['admin', 'manager', 'user']
178 179 180 |
# File 'lib/chewy/index.rb', line 178 def type_names type_hash.keys end |
.types(*args) ⇒ Object
Types method has double usage. If no arguments are passed - it returns array of defined types:
UsersIndex.types # => [UsersIndex::Admin, UsersIndex::Manager, UsersIndex::User]
If arguments are passed it treats like a part of chainable query DSL and adds types array for index to select.
UsersIndex.filters { name =~ 'ro' }.types(:admin, :manager) UsersIndex.types(:admin, :manager).filters { name =~ 'ro' } # the same as the first example
166 167 168 169 170 171 172 |
# File 'lib/chewy/index.rb', line 166 def types(*args) if args.present? all.types(*args) else type_hash.values end end |