Class: Chewy::Index

Inherits:
Object show all
Includes:
Actions, Aliases, Search
Defined in:
lib/chewy/index.rb,
lib/chewy/index/actions.rb,
lib/chewy/index/aliases.rb,
lib/chewy/index/settings.rb

Defined Under Namespace

Modules: Actions, Aliases Classes: Settings

Class Method Summary collapse

Class Method Details

.default_prefixObject

Prefix to use



47
48
49
# File 'lib/chewy/index.rb', line 47

def self.default_prefix
  Chewy.configuration[:prefix]
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.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chewy/index.rb', line 85

def self.define_type(target, options = {}, &block)
  type_class = Chewy.create_type(self, target, options, &block)
  self.type_hash = type_hash.merge(type_class.type_name => type_class)

  unless respond_to?(type_class.type_name)
    class_eval <<-METHOD, __FILE__, __LINE__ + 1
      def self.#{type_class.type_name}
        ActiveSupport::Deprecation.warn("`#{self}.#{type_class.type_name}` accessor is deprecated and will be removed soon. Use `#{type_class}` directly instead.")
        type_hash['#{type_class.type_name}']
      end
    METHOD
  end
end

.index_name(suggest = nil) ⇒ Object

Setups or returns ElasticSearch index name

class UsersIndex < Chewy::Index
end
UsersIndex.index_name # => 'users'

class UsersIndex < Chewy::Index
  index_name 'dudes'
end
UsersIndex.index_name # => 'dudes'


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chewy/index.rb', line 31

def self.index_name(suggest = nil)
  if suggest
    @index_name = build_index_name(suggest, prefix: default_prefix)
  else
    @index_name ||= begin
      build_index_name(
        name.sub(/Index\Z/, '').demodulize.underscore,
        prefix: default_prefix
      ) if name
    end
  end
  @index_name or raise UndefinedIndex
end

.scopesObject

Returns list of public class methods defined in current index



151
152
153
# File 'lib/chewy/index.rb', line 151

def self.scopes
  public_methods - Chewy::Index.public_methods - type_names.map(&:to_sym)
end

.settings(params) ⇒ 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 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.



145
146
147
# File 'lib/chewy/index.rb', line 145

def self.settings(params)
  self._settings = Chewy::Index::Settings.new params
end

.type_namesObject

Returns defined types names:

UsersIndex.type_names # => ['admin', 'manager', 'user']


122
123
124
# File 'lib/chewy/index.rb', line 122

def self.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


110
111
112
113
114
115
116
# File 'lib/chewy/index.rb', line 110

def self.types *args
  if args.present?
    all.types *args
  else
    type_hash.values
  end
end