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

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



79
80
81
82
83
84
85
86
87
88
# File 'lib/chewy/index.rb', line 79

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)

  class_eval <<-METHOD, __FILE__, __LINE__ + 1
    def self.#{type_class.type_name}
      type_hash['#{type_class.type_name}']
    end
  METHOD
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: Chewy.configuration[:prefix])
  else
    @index_name ||= begin
      build_index_name(
        name.sub(/Index\Z/, '').demodulize.underscore,
        prefix: Chewy.configuration[:prefix]
      ) if name
    end
  end
  @index_name or raise UndefinedIndex
end

.scopesObject

Returns list of public class methods defined in current index



142
143
144
# File 'lib/chewy/index.rb', line 142

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.



136
137
138
# File 'lib/chewy/index.rb', line 136

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

.type_namesObject

Returns defined types names:

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


113
114
115
# File 'lib/chewy/index.rb', line 113

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


101
102
103
104
105
106
107
# File 'lib/chewy/index.rb', line 101

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