Skip to content

Commit

Permalink
Amended integration tests for the "custom_score" queries [karmi#133] [k…
Browse files Browse the repository at this point in the history
…armi#139]

Also updated docs in examples/tire-dsl.rb.
  • Loading branch information
karmi committed Oct 30, 2011
1 parent 2eee39d commit a2c7406
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/tire-dsl.rb
Expand Up @@ -477,6 +477,7 @@ def self.search
# * [term](http://elasticsearch.org/guide/reference/query-dsl/term-query.html)
# * [terms](http://elasticsearch.org/guide/reference/query-dsl/terms-query.html)
# * [bool](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)
# * [custom_score](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)
# * [all](http://www.elasticsearch.org/guide/reference/query-dsl/match-all-query.html)
# * [ids](http://www.elasticsearch.org/guide/reference/query-dsl/ids-query.html)

Expand Down
47 changes: 45 additions & 2 deletions test/integration/custom_score_queries_test.rb
Expand Up @@ -7,18 +7,61 @@ class CustomScoreQueriesIntegrationTest < Test::Unit::TestCase

context "Custom score queries" do

should "allow custom score queries" do
should "allow to define custom score queries (base score on field value)" do
s = Tire.search('articles-test') do
query do
custom_score :script => "1 / doc['words'].value" do
# Give longer documents higher score
#
custom_score :script => "1.0 / doc['words'].value" do
string "title:T*"
end
end
end

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

assert s.results[0]._score > 0
assert s.results[1]._score > 0
assert s.results[0]._score > s.results[1]._score
end

should "allow to manipulate the default score (boost recent)" do
s = Tire.search('articles-test') do
query do
# Boost recent documents
#
custom_score :script => "_score + ( doc['published_on'].date.getMillis() / time() )" do
string 'title:F*'
end
end
end

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

assert s.results[0]._score > 1
assert s.results[1]._score > 1
end

should "allow to define arbitrary custom scoring" do
s = Tire.search('articles-test') do
query do
# Replace documents score with the count of characters in their title
#
custom_score :script => "doc['title'].value.length()" do
string "title:T*"
end
end
end

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

assert_equal 5.0, s.results[0]._score
assert_equal 3.0, s.results[1]._score
end

end

end
Expand Down
4 changes: 2 additions & 2 deletions test/unit/search_query_test.rb
Expand Up @@ -59,11 +59,11 @@ class QueryTest < Test::Unit::TestCase
end

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

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

should "search for all documents" do
Expand Down

0 comments on commit a2c7406

Please sign in to comment.