Skip to content

Commit

Permalink
Added Chewy.configuration[:index] options
Browse files Browse the repository at this point in the history
  • Loading branch information
pyromaniac committed Mar 3, 2014
1 parent 453a602 commit 92c129c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# master

* Added `Chewy.configuration[:index]` config to setup common indexes options.

* `Chewy.client_options` replaced with `Chewy.configuration`

* Using source filtering instead of fields filter (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-source-filtering.html).

# Version 0.2.3
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ Or install it yourself as:

### Client configuration

There are 2 ways to configure Chewy client: `Chewy.client_options` hash and `chewy.yml`
There are 2 ways to configure Chewy client: `Chewy.configuration` hash and `chewy.yml`

```ruby
# config/initializers/chewy.rb
Chewy.client_options = {host: 'localhost:9250'} # do not use environments
Chewy.configuration = {host: 'localhost:9250'} # do not use environments
```

```yaml
Expand All @@ -61,7 +61,7 @@ development:
The result config merges both hashes. Client options are passed as is to Elasticsearch::Transport::Client except the `:prefix` - it is used internally by chewy to create prefixed index names:

```ruby
Chewy.client_options = {prefix: 'testing'}
Chewy.configuration = {prefix: 'testing'}
UsersIndex.index_name # => 'testing_users'
```

Expand Down
10 changes: 5 additions & 5 deletions lib/chewy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Config
include Singleton

attr_reader :analyzers, :tokenizers, :filters, :char_filters
attr_accessor :client_options, :urgent_update, :query_mode, :filter_mode, :logger
attr_accessor :configuration, :urgent_update, :query_mode, :filter_mode, :logger

def self.delegated
public_instance_methods - self.superclass.public_instance_methods - Singleton.public_instance_methods
Expand All @@ -21,7 +21,7 @@ def #{name}(name, options = nil)

def initialize
@urgent_update = false
@client_options = {}
@configuration = {}
@query_mode = :must
@filter_mode = :and
@analyzers = {}
Expand Down Expand Up @@ -63,8 +63,8 @@ def initialize
#
repository :char_filter

def client_options
options = @client_options.merge(yaml_options)
def configuration
options = @configuration.deep_symbolize_keys.merge(yaml_options)
options.merge!(logger: logger) if logger
options
end
Expand All @@ -74,7 +74,7 @@ def client?
end

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

def atomic?
Expand Down
4 changes: 2 additions & 2 deletions lib/chewy/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class Index
#
def self.index_name(suggest = nil)
if suggest
@index_name = build_index_name(suggest, prefix: Chewy.client_options[:prefix])
@index_name = build_index_name(suggest, prefix: Chewy.configuration[:prefix])
else
@index_name ||= begin
build_index_name(
name.gsub(/Index\Z/, '').demodulize.underscore,
prefix: Chewy.client_options[:prefix]
prefix: Chewy.configuration[:prefix]
) if name
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/chewy/index/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ def initialize(params = {})
end

def to_hash
return {} unless @params.present?
settings = @params.deep_symbolize_keys

params = @params.deep_dup
params[:analysis] = resolve_analysis(params[:analysis]) if params[:analysis]
settings[:analysis] = resolve_analysis(settings[:analysis]) if settings[:analysis]
if settings[:index] || Chewy.configuration[:index]
settings[:index] = (Chewy.configuration[:index] || {})
.deep_merge((settings[:index] || {}).deep_symbolize_keys)
end

{settings: params}
settings.present? ? {settings: settings} : {}
end

private
Expand Down
4 changes: 2 additions & 2 deletions spec/chewy/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
its(:query_mode) { should == :must }
its(:filter_mode) { should == :and }
its(:logger) { should be_nil }
its(:client_options) { should_not have_key :logger }
its(:configuration) { should_not have_key :logger }
its(:analyzers) { should == {} }
its(:tokenizers) { should == {} }
its(:filters) { should == {} }
Expand Down Expand Up @@ -57,7 +57,7 @@
before { subject.logger = double(:logger) }

its(:logger) { should_not be_nil }
its(:client_options) { should have_key :logger }
its(:configuration) { should have_key :logger }
end

describe '#atomic?' do
Expand Down
12 changes: 12 additions & 0 deletions spec/chewy/index/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,17 @@
tokenizer: {tokenizer1: {options: 43}}
}}} }
end

context ':index' do
specify { described_class.new(index: {number_of_shards: 3}).to_hash
.should == {settings: {index: {number_of_shards: 3}}} }

context do
before { Chewy.configuration = {index: {number_of_shards: 7, number_of_replicas: 2}} }

specify { described_class.new(index: {number_of_shards: 3}).to_hash
.should == {settings: {index: {number_of_shards: 3, number_of_replicas: 2}}} }
end
end
end
end
6 changes: 5 additions & 1 deletion spec/chewy/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
specify { stub_const('DevelopersIndex', Class.new(Chewy::Index)).index_name.should == 'developers' }

context do
before { Chewy.stub(client_options: {prefix: 'testing'}) }
before { Chewy.stub(configuration: {prefix: 'testing'}) }
specify { DummiesIndex.index_name.should == 'testing_dummies' }
specify { stub_index(:dummies) { index_name :users }.index_name.should == 'testing_users' }
end
Expand All @@ -98,6 +98,8 @@
end

describe '.index_params' do
before { Chewy.stub(config: Chewy::Config.send(:new)) }

specify { stub_index(:documents).index_params.should == {} }
specify { stub_index(:documents) { settings number_of_shards: 1 }.index_params.keys.should == [:settings] }
specify { stub_index(:documents) do
Expand All @@ -114,6 +116,8 @@
end

describe '.settings_hash' do
before { Chewy.stub(config: Chewy::Config.send(:new)) }

specify { stub_index(:documents).settings_hash.should == {} }
specify { stub_index(:documents) { settings number_of_shards: 1 }.settings_hash.should == {settings: {number_of_shards: 1}} }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
end
end

Chewy.client_options = { host: 'localhost:9250' }
Chewy.configuration = {host: 'localhost:9250', index: {number_of_shards: 1, number_of_replicas: 0}}

RSpec.configure do |config|
config.mock_with :rspec
Expand Down

0 comments on commit 92c129c

Please sign in to comment.