Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-159: Fix DisMax sorting (contributed by Coda Hale)

Erik Hatcherc3d36ff1794f
diff --git a/client/ruby/solrb/lib/solr/request/dismax.rb b/client/ruby/solrb/lib/solr/request/dismax.rb
index 8a9f1450a47..8b642887eee 100644
--- a/client/ruby/solrb/lib/solr/request/dismax.rb
+++ b/client/ruby/solrb/lib/solr/request/dismax.rb
@@ -1,3 +1,15 @@
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
class Solr::Request::Dismax < Solr::Request::Standard
VALID_PARAMS.replace(VALID_PARAMS + [:tie_breaker, :query_fields, :minimum_match, :phrase_fields, :phrase_slop,
@@ -5,6 +17,8 @@ class Solr::Request::Dismax < Solr::Request::Standard
def initialize(params)
super(params)
+ @sort_values = @params[:sort]
+ @params.delete(:sort)
@query_type = "dismax"
end
@@ -17,6 +31,12 @@ class Solr::Request::Dismax < Solr::Request::Standard
hash[:ps] = @params[:phrase_slop]
hash[:bq] = @params[:boost_query]
hash[:bf] = @params[:boost_functions]
+ # FIXME: 2007-02-13 <[email protected]> -- This code is duplicated in
+ # Solr::Request::Standard. It should be refactored into a single location.
+ hash[:sort] = @sort_values.collect do |sort|
+ key = sort.keys[0]
+ "#{key.to_s} #{sort[key] == :descending ? 'desc' : 'asc'}"
+ end.join(',') if @sort_values
return hash
end
diff --git a/client/ruby/solrb/test/functional/server_test.rb b/client/ruby/solrb/test/functional/server_test.rb
index aef64e9374d..e0743a11bad 100644
--- a/client/ruby/solrb/test/functional/server_test.rb
+++ b/client/ruby/solrb/test/functional/server_test.rb
@@ -77,6 +77,21 @@ class ServerTest < Test::Unit::TestCase
response = @connection.query('Åäöêâîôû Öëäïöü')
assert_equal 0, response.total_hits
end
+
+ def test_sorting
+ @connection.add(:id => 1, :text => 'aaa woot')
+ @connection.add(:id => 2, :text => 'bbb woot')
+ @connection.add(:id => 3, :text => 'ccc woot')
+ @connection.commit
+
+ results = @connection.query('woot', :sort => [:id => :descending], :rows => 2)
+ assert_equal([3, 2], results.hits.map { |h| h['id'].to_i })
+
+ results = @connection.search('woot', :sort => [:id => :descending], :rows => 2)
+ assert_equal([3, 2], results.hits.map { |h| h['id'].to_i })
+
+ @connection.delete_by_query("id:1 OR id:2 OR id:3")
+ end
def test_bad_connection
conn = Solr::Connection.new 'http://127.0.0.1:9999/invalid'
diff --git a/client/ruby/solrb/test/unit/dismax_request_test.rb b/client/ruby/solrb/test/unit/dismax_request_test.rb
index 98cd1cbf66b..7141334720d 100644
--- a/client/ruby/solrb/test/unit/dismax_request_test.rb
+++ b/client/ruby/solrb/test/unit/dismax_request_test.rb
@@ -16,10 +16,11 @@ require 'solr'
class DismaxRequestTest < Test::Unit::TestCase
def test_basic_query
- request = Solr::Request::Dismax.new(:query => 'query', :phrase_slop => '1000')
+ request = Solr::Request::Dismax.new(:query => 'query', :phrase_slop => '1000', :sort => [{:deedle => :descending}])
assert_match(/q=query/, request.to_s)
assert_match(/qt=dismax/, request.to_s)
assert_match(/ps=1000/, request.to_s)
+ assert_match(/sort=deedle%20desc/, request.to_s)
end
end
\ No newline at end of file