class Chewy::Config

Attributes

console_strategy[RW]
default_field_type[RW]
default_root_options[RW]
disable_refresh_async[RW]
indices_path[RW]
logger[RW]
request_strategy[RW]
reset_disable_refresh_interval[RW]
reset_no_replicas[RW]
root_strategy[RW]
search_class[R]
settings[RW]
transport_logger[R]
transport_tracer[R]
use_after_commit_callbacks[RW]

Public Class Methods

delegated() click to toggle source
# File lib/chewy/config.rb, line 47
def self.delegated
  public_instance_methods - superclass.public_instance_methods - Singleton.public_instance_methods
end
new() click to toggle source
# File lib/chewy/config.rb, line 51
def initialize
  @settings = {}
  @root_strategy = :base
  @request_strategy = :atomic
  @console_strategy = :urgent
  @use_after_commit_callbacks = true
  @reset_disable_refresh_interval = false
  @reset_no_replicas = false
  @disable_refresh_async = false
  @indices_path = 'app/chewy'
  @default_root_options = {}
  @default_field_type = 'text'.freeze
  @search_class = build_search_class(Chewy::Search::Request)
end

Public Instance Methods

configuration() click to toggle source

Chewy core configurations. There is two ways to set it up: use `Chewy.settings=` 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 heavy
   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
# File lib/chewy/config.rb, line 119
def configuration
  yaml_settings.merge(settings.deep_symbolize_keys).tap do |configuration|
    configuration[:logger] = transport_logger if transport_logger
    configuration[:indices_path] ||= indices_path if indices_path
    configuration.merge!(tracer: transport_tracer) if transport_tracer
  end
end
transport_logger=(logger) click to toggle source
# File lib/chewy/config.rb, line 66
def transport_logger=(logger)
  Chewy.client.transport.logger = logger
  @transport_logger = logger
end
transport_tracer=(tracer) click to toggle source
# File lib/chewy/config.rb, line 71
def transport_tracer=(tracer)
  Chewy.client.transport.tracer = tracer
  @transport_tracer = tracer
end

Private Instance Methods

build_search_class(base) click to toggle source
# File lib/chewy/config.rb, line 143
def build_search_class(base)
  Class.new(base).tap do |search_class|
    search_class.send :include, Chewy::Search::Pagination::Kaminari if defined?(::Kaminari)
  end
end
yaml_settings() click to toggle source
# File lib/chewy/config.rb, line 129
def yaml_settings
  @yaml_settings ||= begin
    if defined?(Rails::VERSION)
      file = Rails.root.join('config', 'chewy.yml')

      if File.exist?(file)
        yaml = ERB.new(File.read(file)).result
        hash = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(yaml) : YAML.load(yaml) # rubocop:disable Security/YAMLLoad
        hash[Rails.env].try(:deep_symbolize_keys) if hash
      end
    end || {}
  end
end