Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for "custom_score" queries [#133]

  • Loading branch information...
commit 2eee39dd18713b6934dbbfe2fa1b4d338ae6ec78 1 parent 98d7b01
@pacoguzman pacoguzman authored karmi committed
View
7 lib/tire/search/query.rb
@@ -28,6 +28,13 @@ def string(value, options={})
@value
end
+ def custom_score(options={}, &block)
+ @custom_score ||= Query.new(&block)
+ @value[:custom_score] = options
+ @value[:custom_score].update({:query => @custom_score.to_hash})
+ @value
+ end
+
def boolean(options={}, &block)
@boolean ||= BooleanQuery.new(options)
block.arity < 1 ? @boolean.instance_eval(&block) : block.call(@boolean) if block_given?
View
26 test/integration/custom_score_queries_test.rb
@@ -0,0 +1,26 @@
+require 'test_helper'
+
+module Tire
+
+ class CustomScoreQueriesIntegrationTest < Test::Unit::TestCase
+ include Test::Integration
+
+ context "Custom score queries" do
+
+ should "allow custom score queries" do
+ s = Tire.search('articles-test') do
+ query do
+ custom_score :script => "1 / doc['words'].value" do
+ string "title:T*"
+ end
+ end
+ end
+
+ assert_equal 2, s.results.size
+ assert_equal ['Two', 'Three'], s.results.map(&:title)
+ end
+ end
+
+ end
+
+end
View
9 test/unit/search_query_test.rb
@@ -58,6 +58,14 @@ 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
+ query = Query.new.custom_score(:script => "1 / _score") do
+ string 'foo'
+ end
+
+ assert_equal "1 / _score", query[:custom_score][:script]
+ end
+
should "search for all documents" do
assert_equal( { :match_all => { } }, Query.new.all )
end
@@ -175,7 +183,6 @@ class QueryTest < Test::Unit::TestCase
end
end
-
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.