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
270 271 272 273 |
# File 'lib/chewy/index.rb', line 270 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 |
# File 'lib/chewy/index.rb', line 148 def define_type(target, = {}, &block) 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
246 247 248 249 |
# File 'lib/chewy/index.rb', line 246 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
235 236 237 238 |
# File 'lib/chewy/index.rb', line 235 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
221 222 223 224 |
# File 'lib/chewy/index.rb', line 221 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.
252 253 254 255 256 257 258 259 |
# File 'lib/chewy/index.rb', line 252 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
261 262 263 264 265 266 267 268 |
# File 'lib/chewy/index.rb', line 261 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
213 214 215 |
# File 'lib/chewy/index.rb', line 213 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.
207 208 209 |
# File 'lib/chewy/index.rb', line 207 def settings(params = {}, &block) self._settings = Chewy::Index::Settings.new(params, &block) end |
.settings_hash ⇒ Object
217 218 219 |
# File 'lib/chewy/index.rb', line 217 def settings_hash _settings.to_hash end |
.specification ⇒ Chewy::Index::Specification
Returns a specification object instance for this particular index.
242 243 244 |
# File 'lib/chewy/index.rb', line 242 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.
231 232 233 |
# File 'lib/chewy/index.rb', line 231 def specification_hash [settings_hash, mappings_hash].inject(:merge) end |
.type(type_name) ⇒ Object
Returns named type:
UserIndex.type('admin') # => UsersIndex::Admin
184 185 186 |
# File 'lib/chewy/index.rb', line 184 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']
176 177 178 |
# File 'lib/chewy/index.rb', line 176 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
164 165 166 167 168 169 170 |
# File 'lib/chewy/index.rb', line 164 def types(*args) if args.present? all.types(*args) else type_hash.values end end |