Class: Chewy::Config

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/chewy/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



49
50
51
52
53
54
55
56
57
58
# File 'lib/chewy/config.rb', line 49

def initialize
  @configuration = {}
  @urgent_update = false
  @query_mode = :must
  @filter_mode = :and
  @analyzers = {}
  @tokenizers = {}
  @filters = {}
  @char_filters = {}
end

Instance Attribute Details

#analyzersObject (readonly)

Returns the value of attribute analyzers.



5
6
7
# File 'lib/chewy/config.rb', line 5

def analyzers
  @analyzers
end

#char_filtersObject (readonly)

Returns the value of attribute char_filters.



5
6
7
# File 'lib/chewy/config.rb', line 5

def char_filters
  @char_filters
end

#configurationObject

Chewy core configurations. There is two ways to set it up: use ‘Chewy.configuration=` method or, for Rails application, create `config/chewy.yml` file. Btw, `config/chewy.yml` supports ERB the same way as ActiveRecord’s config.

Configuration options:

1. Chewy client options. All the options Elasticsearch::Client
   supports.

     test:
       host: 'localhost:9250'

2. Chewy self-configuration:

   :prefix - used as prefix for any index created.

     test:
       host: 'localhost:9250'
       prefix: test<%= ENV['TEST_ENV_NUMBER'] %>

   Then UsersIndex.index_name will be "test42_users"
   in case TEST_ENV_NUMBER=42

   :wait_for_status - if this option set - chewy actions such
   as creating or deleting index, importing data will wait for
   the status specified. Extremely useful for tests under havy
   indexes manipulations.

     test:
       host: 'localhost:9250'
       wait_for_status: green

3. Index settings. All the possible ElasticSearch index settings.
   Will be merged as defaults with index settings on every index
   creation.

     test: &test
       host: 'localhost:9250'
       index:
         number_of_shards: 1
         number_of_replicas: 0


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

def configuration
  @configuration
end

#filter_modeObject

Returns the value of attribute filter_mode.



6
7
8
# File 'lib/chewy/config.rb', line 6

def filter_mode
  @filter_mode
end

#filtersObject (readonly)

Returns the value of attribute filters.



5
6
7
# File 'lib/chewy/config.rb', line 5

def filters
  @filters
end

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/chewy/config.rb', line 6

def logger
  @logger
end

#post_filter_modeObject

Returns the value of attribute post_filter_mode.



6
7
8
# File 'lib/chewy/config.rb', line 6

def post_filter_mode
  @post_filter_mode
end

#query_modeObject

Returns the value of attribute query_mode.



6
7
8
# File 'lib/chewy/config.rb', line 6

def query_mode
  @query_mode
end

#tokenizersObject (readonly)

Returns the value of attribute tokenizers.



5
6
7
# File 'lib/chewy/config.rb', line 5

def tokenizers
  @tokenizers
end

#urgent_updateObject

Returns the value of attribute urgent_update.



6
7
8
# File 'lib/chewy/config.rb', line 6

def urgent_update
  @urgent_update
end

Class Method Details

.delegatedObject



35
36
37
# File 'lib/chewy/config.rb', line 35

def self.delegated
  public_instance_methods - self.superclass.public_instance_methods - Singleton.public_instance_methods
end

.repository(name) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/chewy/config.rb', line 39

def self.repository name
  plural_name = name.to_s.pluralize

  class_eval <<-METHOD, __FILE__, __LINE__ + 1
    def #{name}(name, options = nil)
      options ? #{plural_name}[name.to_sym] = options : #{plural_name}[name.to_sym]
    end
  METHOD
end

Instance Method Details

#atomicObject



150
151
152
153
154
155
# File 'lib/chewy/config.rb', line 150

def atomic
  stash.push({})
  yield
ensure
  stash.pop.each { |type, ids| type.import(ids) }
end

#atomic?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/chewy/config.rb', line 146

def atomic?
  stash.any?
end

#clientObject



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

def client
  Thread.current[:chewy_client] ||= ::Elasticsearch::Client.new configuration
end

#stash(*args) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/chewy/config.rb', line 157

def stash *args
  if args.any?
    type, ids = *args
    raise ArgumentError.new('Only Chewy::Type accepted as the first argument') unless type < Chewy::Type
    stash.last[type] ||= []
    stash.last[type] |= ids
  else
    Thread.current[:chewy_cache] ||= []
  end
end