Skip to content

Commit

Permalink
Configure update_format (json/xml) via yaml config file (#897)
Browse files Browse the repository at this point in the history
* Configure update_format (json/xml) via yaml config file.
Test sunspot gem against both update formats
  • Loading branch information
serggl committed Mar 28, 2018
1 parent 0c8a888 commit 0cfa5d2
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 21 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Expand Up @@ -18,10 +18,15 @@ rvm:
# - jruby

env:
- GEM=sunspot
- GEM=sunspot UPDATE_FORMAT=xml
- GEM=sunspot UPDATE_FORMAT=json
- GEM=sunspot_rails
- GEM=sunspot_solr

matrix:
allow_failures:
- env: GEM=sunspot UPDATE_FORMAT=json

script:
- ci/travis.sh

1 change: 1 addition & 0 deletions sunspot/lib/sunspot/configuration.rb
Expand Up @@ -26,6 +26,7 @@ def build #:nodoc:
read_timeout nil
open_timeout nil
proxy nil
update_format :xml
end
master_solr do
url nil
Expand Down
13 changes: 7 additions & 6 deletions sunspot/lib/sunspot/session.rb
Expand Up @@ -254,12 +254,13 @@ def batch
# RSolr::Connection::Base:: The connection for this session
#
def connection
@connection ||=
self.class.connection_class.connect(:url => config.solr.url,
:read_timeout => config.solr.read_timeout,
:open_timeout => config.solr.open_timeout,
:proxy => config.solr.proxy,
:update_format => :xml)
@connection ||= self.class.connection_class.connect(
url: config.solr.url,
read_timeout: config.solr.read_timeout,
open_timeout: config.solr.open_timeout,
proxy: config.solr.proxy,
update_format: config.solr.update_format || :xml
)
end

def indexer
Expand Down
22 changes: 16 additions & 6 deletions sunspot/spec/api/query/fulltext_examples.rb
Expand Up @@ -66,7 +66,9 @@
end

it 'puts default dismax parameters in subquery' do
expect(subqueries(:q).last[:qf].split(' ').sort).to eq(%w(backwards_title_text body_textsv tags_textv title_text))
expect(subqueries(:q).last[:qf].split(' ').sort).to(
eq(%w(backwards_title_text body_textsv tags_textv text_array_text title_text))
)
end

it 'puts field list in main query' do
Expand All @@ -78,14 +80,18 @@
search = search do
keywords 'keyword search'
end
expect(connection.searches.last[:qf].split(' ').sort).to eq(%w(backwards_title_text body_textsv tags_textv title_text))
expect(connection.searches.last[:qf].split(' ').sort).to(
eq(%w(backwards_title_text body_textsv tags_textv text_array_text title_text))
)
end

it 'searches both stored and unstored text fields' do
search Post, Namespaced::Comment do
keywords 'keyword search'
end
expect(connection.searches.last[:qf].split(' ').sort).to eq(%w(author_name_text backwards_title_text body_text body_textsv tags_textv title_text))
expect(connection.searches.last[:qf].split(' ').sort).to(
eq(%w(author_name_text backwards_title_text body_text body_textsv tags_textv text_array_text title_text))
)
end

it 'searches only specified text fields when specified' do
Expand All @@ -101,7 +107,7 @@
exclude_fields :backwards_title, :body_mlt
end
end
expect(connection.searches.last[:qf].split(' ').sort).to eq(%w(body_textsv tags_textv title_text))
expect(connection.searches.last[:qf].split(' ').sort).to eq(%w(body_textsv tags_textv text_array_text title_text))
end

it 'assigns boost to fields when specified' do
Expand Down Expand Up @@ -173,7 +179,9 @@
boost_fields :title => 1.5
end
end
expect(connection.searches.last[:qf].split(' ').sort).to eq(%w(backwards_title_text body_textsv tags_textv title_text^1.5))
expect(connection.searches.last[:qf].split(' ').sort).to(
eq(%w(backwards_title_text body_textsv tags_textv text_array_text title_text^1.5))
)
end

it 'ignores boost fields that do not apply' do
Expand All @@ -182,7 +190,9 @@
boost_fields :bogus => 1.2, :title => 1.5
end
end
expect(connection.searches.last[:qf].split(' ').sort).to eq(%w(backwards_title_text body_textsv tags_textv title_text^1.5))
expect(connection.searches.last[:qf].split(' ').sort).to(
eq(%w(backwards_title_text body_textsv tags_textv text_array_text title_text^1.5))
)
end

it 'sets default boost with default fields' do
Expand Down
1 change: 1 addition & 0 deletions sunspot/spec/helpers/integration_helper.rb
Expand Up @@ -2,6 +2,7 @@ module IntegrationHelper
def self.included(base)
base.before(:all) do
Sunspot.config.solr.url = ENV['SOLR_URL'] || 'http://localhost:8983/solr/default'
Sunspot.config.solr.update_format = ENV['UPDATE_FORMAT'].to_sym if ENV['UPDATE_FORMAT']
Sunspot.reset!(true)
end
end
Expand Down
3 changes: 3 additions & 0 deletions sunspot/spec/mocks/post.rb
Expand Up @@ -37,6 +37,9 @@ def custom_boolean

Sunspot.setup(Post) do
text :title, :boost => 2
text :text_array, :boost => 3 do
[title, title]
end
text :body, :stored => true, :more_like_this => true
text :backwards_title do
title.reverse if title
Expand Down
8 changes: 2 additions & 6 deletions sunspot/spec/spec_helper.rb
Expand Up @@ -23,14 +23,10 @@
config.order = 'random'

# Mock session available to all spec/api tests
config.include MockSessionHelper,
:type => :api,
:file_path => /spec[\\\/]api/
config.include MockSessionHelper, type: :api, file_path: /spec[\\\/]api/

# Real Solr instance is available to integration tests
config.include IntegrationHelper,
:type => :integration,
:file_path => /spec[\\\/]integration/
config.include IntegrationHelper, type: :integration, file_path: /spec[\\\/]integration/

# Nested under spec/api
[:indexer, :query, :search].each do |spec_type|
Expand Down
2 changes: 2 additions & 0 deletions sunspot_rails/lib/sunspot/rails.rb
Expand Up @@ -53,6 +53,7 @@ def master_config(sunspot_rails_configuration)
config.solr.read_timeout = sunspot_rails_configuration.read_timeout
config.solr.open_timeout = sunspot_rails_configuration.open_timeout
config.solr.proxy = sunspot_rails_configuration.proxy
config.solr.update_format = sunspot_rails_configuration.update_format
config
end

Expand All @@ -68,6 +69,7 @@ def slave_config(sunspot_rails_configuration)
config.solr.read_timeout = sunspot_rails_configuration.read_timeout
config.solr.open_timeout = sunspot_rails_configuration.open_timeout
config.solr.proxy = sunspot_rails_configuration.proxy
config.solr.update_format = sunspot_rails_configuration.update_format
config
end
end
Expand Down
4 changes: 4 additions & 0 deletions sunspot_rails/lib/sunspot/rails/configuration.rb
Expand Up @@ -301,6 +301,10 @@ def open_timeout
@open_timeout ||= user_configuration_from_key('solr', 'open_timeout')
end

def update_format
@update_format ||= user_configuration_from_key('solr', 'update_format')
end

def proxy
@proxy ||= user_configuration_from_key('solr', 'proxy')
end
Expand Down
12 changes: 10 additions & 2 deletions sunspot_rails/spec/configuration_spec.rb
Expand Up @@ -50,11 +50,15 @@
end

it "should set the read timeout to nil when not set" do
@config.read_timeout == nil
expect(@config.read_timeout).to be_nil
end

it "should set the open timeout to nil when not set" do
@config.open_timeout == nil
expect(@config.open_timeout).to be_nil
end

it "should set the update_format to nil when not set" do
expect(@config.update_format).to be_nil
end

it "should set 'log_level' property using Rails log level when not set" do
Expand Down Expand Up @@ -163,6 +167,10 @@
expect(@config.open_timeout).to eq(0.5)
end

it "should handle the 'update_format' property when set" do
expect(@config.update_format).to eq('json')
end

it "should handle the 'proxy' property when set" do
expect(@config.proxy).to eq('http://proxy.com:12345')
end
Expand Down
1 change: 1 addition & 0 deletions sunspot_rails/spec/rails_app/config/sunspot.yml
Expand Up @@ -22,6 +22,7 @@ config_test:
bind_address: 127.0.0.1
read_timeout: 2
open_timeout: 0.5
update_format: json
proxy: http://proxy.com:12345
auto_commit_after_request: false
auto_commit_after_delete_request: true
Expand Down

0 comments on commit 0cfa5d2

Please sign in to comment.