Skip to content

Commit

Permalink
Added test cases for parameter passing to "custom_score" queries [kar…
Browse files Browse the repository at this point in the history
  • Loading branch information
karmi committed Oct 30, 2011
1 parent a2c7406 commit 1b9b157
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
19 changes: 19 additions & 0 deletions test/integration/custom_score_queries_test.rb
Expand Up @@ -62,6 +62,25 @@ class CustomScoreQueriesIntegrationTest < Test::Unit::TestCase
assert_equal 3.0, s.results[1]._score
end

should "allow to pass parameters to the script" do
s = Tire.search('articles-test') do
query do
# Replace documents score with parameterized computation
#
custom_score :script => "doc['words'].doubleValue / max(a, b)",
:params => { :a => 1, :b => 2 } do
string "title:T*"
end
end
end

assert_equal 2, s.results.size
assert_equal ['Three', 'Two'], s.results.map(&:title)

assert_equal 187.5, s.results[0]._score
assert_equal 125.0, s.results[1]._score
end

end

end
Expand Down
12 changes: 11 additions & 1 deletion test/unit/search_query_test.rb
Expand Up @@ -58,14 +58,24 @@ class QueryTest < Test::Unit::TestCase
Query.new.string('foo', :fields => ['title.*'], :use_dis_max => true) )
end

should "allow set options when searching with custom score" do
should "allow to set script for custom score queries" do
query = Query.new.custom_score(:script => "_score * doc['price'].value") do
string 'foo'
end

assert_equal "_score * doc['price'].value", query[:custom_score][:script]
end

should "allow to pass parameters for custom score queries" do
query = Query.new.custom_score(:script => "_score * doc['price'].value / max(a, b)",
:params => { :a => 1, :b => 2 }) do
string 'foo'
end

assert_equal 1, query[:custom_score][:params][:a]
assert_equal 2, query[:custom_score][:params][:b]
end

should "search for all documents" do
assert_equal( { :match_all => { } }, Query.new.all )
end
Expand Down

0 comments on commit 1b9b157

Please sign in to comment.