Skip to content

Commit

Permalink
Implement preference usage on search scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Clay Whetung committed Nov 26, 2014
1 parent 500f106 commit 5e1d9de
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
8 changes: 7 additions & 1 deletion lib/elastictastic/scope.rb
Expand Up @@ -326,6 +326,7 @@ def multi_get_params
def multi_search_headers
{'type' => type, 'index' => @index.name}.tap do |params|
params['routing'] = @routing if @routing
params['preference'] = @search.preference if @search.preference
end
end

Expand Down Expand Up @@ -357,6 +358,7 @@ def search_all
return @materialized_hits if defined? @materialized_hits
search_params = {:search_type => 'query_then_fetch'}
search_params[:routing] = @routing if @routing
search_params[:preference] = @search.preference if @search.preference
self.response = search(search_params)
@materialized_hits
end
Expand All @@ -368,7 +370,8 @@ def search_in_batches(&block)
scope = scope_with_size.from(from)
params = {:search_type => 'query_then_fetch'}
params[:routing] = @routing if @routing
response = scope.search(params)
params[:preference] = @search.preference if @search.preference
response= scope.search(params)
self.counts = response
yield materialize_hits(response['hits']['hits'])
from += size
Expand All @@ -383,6 +386,8 @@ def scan_in_batches(batch_options, &block)
:size => batch_options[:batch_size] || ::Elastictastic.config.default_batch_size
}
scroll_options[:routing] = @routing if @routing
scroll_options[:preference] = @search.preference if @search.preference

scan_response = ::Elastictastic.client.search(
@index,
@clazz.type,
Expand All @@ -403,6 +408,7 @@ def scan_in_batches(batch_options, &block)
def populate_counts
params = {:search_type => 'count'}
params[:routing] = @routing if @routing
params[:preference] = @search.preference if @search.preference
self.counts = search(params)
end

Expand Down
1 change: 0 additions & 1 deletion lib/elastictastic/search.rb
Expand Up @@ -47,7 +47,6 @@ def params
params['highlight'] = highlight
params['fields'] = maybe_array(fields)
params['script_fields'] = script_fields
params['preference'] = preference
params['facets'] = facets
params['_source'] = maybe_array(_source)
params.reject! { |k, v| v.blank? }
Expand Down
6 changes: 5 additions & 1 deletion spec/examples/multi_search_spec.rb
Expand Up @@ -16,14 +16,16 @@
[
Post.query(:query_string => { :query => 'pizza' }).size(10),
Blog.in_index('my_index').query(:term => { 'name' => 'Pasta' }).size(10),
Photo.routing('7').query(:query_string => {:query => 'pizza'}).size(10)
Photo.routing('7').query(:query_string => {:query => 'pizza'}).size(10),
Photo.preference('_primary').query(:query_string => {:query => 'pizza'}).size(10)
]
end

before do
stub_es_msearch(
Array.new(3) { |i| generate_es_hit('post', :source => { 'title' => "post #{i}" }) },
Array.new(5) { |i| generate_es_hit('blog', :source => { 'name' => "blog #{i}" }) },
Array.new(2) { |i| generate_es_hit('photo', :source => { 'caption' => "photo #{i}" }) },
Array.new(2) { |i| generate_es_hit('photo', :source => { 'caption' => "photo #{i}" }) }
)
Elastictastic::MultiSearch.query(scopes)
Expand All @@ -33,12 +35,14 @@
request_components[0].should == { 'index' => 'default', 'type' => 'post', 'search_type' => 'query_then_fetch' }
request_components[2].should == { 'index' => 'my_index', 'type' => 'blog', 'search_type' => 'query_then_fetch' }
request_components[4].should == { 'index' => 'default', 'type' => 'photo', 'search_type' => 'query_then_fetch', 'routing' => '7' }
request_components[6].should == { 'index' => 'default', 'type' => 'photo', 'search_type' => 'query_then_fetch', 'preference' => '_primary' }
end

it 'should send correct scope params' do
request_components[1].should == scopes[0].params
request_components[3].should == scopes[1].params
request_components[5].should == scopes[2].params
request_components[5].should == scopes[2].params
end

it 'should populate scopes with results' do
Expand Down
32 changes: 32 additions & 0 deletions spec/examples/scope_spec.rb
Expand Up @@ -218,6 +218,38 @@
end
end
end

describe 'with preference' do
it 'should send preference param in single-search query' do
stub_es_search(
'default', 'post',
'hits' => {'total' => 2, 'hits' => make_hits(2)}
)
Post.preference('_primary').size(10).to_a
last_request_uri.query.split('&').should include('preference=_primary')
end

it 'should send routing param in scan query' do
stub_es_scan(
'default', 'post', 2, *make_hits(3)
)
Post.preference('_primary').to_a
URI.parse(FakeWeb.requests.first.path).query.split('&').
should include('preference=_primary')
end

it 'should send routing param in batch-search queries' do
Elastictastic.config.default_batch_size = 2
stub_es_search(
'default', 'post',
make_hits(3).each_slice(2).map { |batch| { 'hits' => { 'hits' => batch, 'total' => 3 }} }
)
Post.preference('_primary').sort(:score).to_a
FakeWeb.requests.each do |request|
URI.parse(request.path).query.split('&').should include('preference=_primary')
end
end
end
end # describe '#each'

describe 'hit metadata' do
Expand Down
8 changes: 1 addition & 7 deletions spec/examples/search_spec.rb
Expand Up @@ -18,7 +18,6 @@
'_source' => %w(title body),
'fields' => %w(title body),
'script_fields' => { 'rtitle' => { 'script' => "_source.title.reverse()" }},
'preference' => '_local',
'facets' => { 'tags' => { 'terms' => { 'field' => 'tags' }}}
}.each_pair do |method, value|
it "should build scope for #{method} param" do
Expand Down Expand Up @@ -325,12 +324,7 @@
'script_fields' => { 'test1' => { 'script' => '1' }, 'test2' => { 'script' => '2' }}
}
end

it 'should overwrite chained preference' do
scope.preference('_local').preference('_primary').params.should ==
{ 'preference' => '_primary' }
end


it 'should merge facets' do
scope = self.scope.facets(:title => { :terms => { :field => 'title' }})
scope = scope.facets(:tags => { :terms => { :field => 'tags' }})
Expand Down

0 comments on commit 5e1d9de

Please sign in to comment.