Skip to content

Commit

Permalink
added adjust_solr_params dsl logic, you are now able to changed all p…
Browse files Browse the repository at this point in the history
…arameters passed to solr
  • Loading branch information
Benjamin Krause authored and Mat Brown committed Oct 20, 2009
1 parent 945841a commit 8d15e31
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
20 changes: 20 additions & 0 deletions lib/sunspot/dsl/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ def paginate(options = {})
@query.paginate(page, per_page)
end

# Adjust or reset the parameters passed to solr. The adjustment
# will take place just before talking to solr, after all other
# dsl-commands have modified the query parameters.
#
# Please bear in mind, that you are changing the parameters that
# will be passed to solr, and will not be checked for validity
# by sunspot.
#
# ==== Example
#
# Sunspot.search(Post) do
# adjust_solr_params do |params|
# params[:q] += ' AND something_s:more'
# end
# end
#
def adjust_solr_params( &block )
@query.set_solr_parameter_adjustment( block )
end

#
# Scope the search by geographical distance from a given point.
# +coordinates+ should either respond to #first and #last (e.g. a
Expand Down
8 changes: 6 additions & 2 deletions lib/sunspot/query/query.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module Sunspot
module Query
class Query
attr_accessor :scope
attr_accessor :fulltext
attr_accessor :scope, :fulltext, :parameter_adjustment

def initialize(types)
@scope = Scope.new
Expand All @@ -19,6 +18,10 @@ def initialize(types)
def set_fulltext(keywords)
@fulltext = Dismax.new(keywords)
end

def set_solr_parameter_adjustment( block )
@parameter_adjustment = block
end

def add_location_restriction(coordinates, radius)
@local = Local.new(coordinates, radius)
Expand Down Expand Up @@ -73,6 +76,7 @@ def to_params
Sunspot::Util.deep_merge!(params, @sort.to_params)
Sunspot::Util.deep_merge!(params, @pagination.to_params) if @pagination
Sunspot::Util.deep_merge!(params, @local.to_params) if @local
@parameter_adjustment.call(params) if @parameter_adjustment
params[:q] ||= '*:*'
params
end
Expand Down
37 changes: 37 additions & 0 deletions spec/api/query/adjust_params_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require File.join(File.dirname(__FILE__), 'spec_helper')

describe 'typed query' do
it "should send query to solr with adjusted parameters (keyword example)" do
session.search Post do
keywords 'keyword search'
adjust_solr_params do |params|
params[:q] = 'new search'
params[:some] = 'param'
end
end
connection.should have_last_search_with(:q => 'new search')
connection.should have_last_search_with(:some => 'param')
end

it "should work, even without another dsl command" do
session.search Post do
adjust_solr_params do |params|
params[:q] = 'napoleon dynamite'
params[:qt] = 'complicated'
end
end
connection.should have_last_search_with(:q => 'napoleon dynamite')
connection.should have_last_search_with(:qt => 'complicated')
end

it "should be able to extend parameters" do
session.search Post do
keywords 'keyword search'
adjust_solr_params do |params|
params[:q] += ' AND something_s:more'
end
end
connection.should have_last_search_with(:q => 'keyword search AND something_s:more')
end

end

0 comments on commit 8d15e31

Please sign in to comment.