Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the case when using order_by_function with Solr strdist #572

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion sunspot/lib/sunspot/query/sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,23 @@ class FunctionComp
def initialize(setup,args)
@function=args.shift
@fields = []
@extras = []

case @function.to_s
when "strdist"
# Reference: http://wiki.apache.org/solr/FunctionQuery#strdist
valid_measures = [:jw, :edit, :ngram]
extra = args.pop
if valid_measures.include? extra.to_sym
@extras.push(extra)
else
raise(
UnrecognizedRestrictionError,
"Use only valid measure options with strdist (#{valid_measures.to_s})"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)
end
end

args.each do |argument|
case argument.class.name
when "Array"
Expand All @@ -146,7 +163,9 @@ def initialize(setup,args)
end
end
def to_s
"#{function}(#{fields.map(&:to_s).join(",")})"
mapped_fields = fields
mapped_fields = mapped_fields + @extras if not @extras.empty?
"#{function}(#{mapped_fields.map(&:to_s).join(",")})"
end
end
end
Expand Down
18 changes: 16 additions & 2 deletions sunspot/spec/integration/scoped_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,8 @@ def self.test_field_type(name, attribute, field, *values)
describe 'ordering by function' do
before :all do
Sunspot.remove_all
@p1 = Post.new(:blog_id => 1, :category_ids => [3])
@p2 = Post.new(:blog_id => 2, :category_ids => [1])
@p1 = Post.new(:title => 'Post Test', :blog_id => 1, :category_ids => [3])
@p2 = Post.new(:title => 'Test', :blog_id => 2, :category_ids => [1])
Sunspot.index([@p1,@p2])
Sunspot.commit
end
Expand All @@ -486,5 +486,19 @@ def self.test_field_type(name, attribute, field, *values)
search = Sunspot.search(Post) {order_by_function :product, :blog_id, -2, :desc}
search.results.first.should == @p1
end
context 'when ordering by strdist' do
# Reference: http://wiki.apache.org/solr/FunctionQuery#strdist
it 'should only accept valid values as measures' do
[:jw, :edit, :ngram].each do |measure|
search = Sunspot.search(Post) {order_by_function :strdist, :title, "'Test'", measure, :desc}
search.results.first.should == @p2
end
end
it 'should not accept invalid values as measures' do
lambda do
Sunspot.search(Post) {order_by_function :strdist, :title, "'Test'", :wrong_measure, :desc}
end.should raise_error(Sunspot::UnrecognizedRestrictionError)
end
end
end
end