Skip to content

Commit

Permalink
[karmi#289] Improvements to the "term" query type
Browse files Browse the repository at this point in the history
This is a combination of 3 commits:

* little improvement of term api
* fixing term tests after api changing
* unit tests for term query with an option hash
  • Loading branch information
Diego Plentz authored and karmi committed Mar 26, 2012
1 parent c1e10d6 commit ec4e3ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/tire/search/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ def initialize(&block)
block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
end

def term(field, value)
@value = { :term => { field => value } }
def term(field, value, options={})
query = { field => { :term => value } }
query[field].update(options)
@value = { :term => query }
end

def fuzzy(field, value, options={})
query = { field => { :term => value } }
Expand Down
14 changes: 9 additions & 5 deletions test/unit/search_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ class QueryTest < Test::Unit::TestCase

should "return itself as a Hash" do
assert_respond_to Query.new, :to_hash
assert_equal( { :term => { :foo => 'bar' } }, Query.new.term(:foo, 'bar').to_hash )
assert_equal( { :term => { :foo => { :term => 'bar' } } }, Query.new.term(:foo, 'bar').to_hash )
end

should "allow a block to be given" do
assert_equal( { :term => { :foo => 'bar' } }.to_json, Query.new do
assert_equal( { :term => { :foo => { :term => 'bar' } } }.to_json, Query.new do
term(:foo, 'bar')
end.to_json)
end

should "allow search for single term" do
assert_equal( { :term => { :foo => 'bar' } }, Query.new.term(:foo, 'bar') )
assert_equal( { :term => { :foo => { :term => 'bar' } } }, Query.new.term(:foo, 'bar') )
end

should "allow search for single term passing an options hash" do
assert_equal( { :term => { :foo => { :term => 'bar', :boost => 2.0 } } }, Query.new.term(:foo, 'bar', :boost => 2.0) )
end

should "allow search for multiple terms" do
Expand Down Expand Up @@ -185,7 +189,7 @@ class QueryTest < Test::Unit::TestCase
end

query[:filtered].tap do |f|
assert_equal( { :term => { :foo => 'bar' } }, f[:query].to_hash )
assert_equal( { :term => { :foo => { :term => 'bar' } } }, f[:query].to_hash )
assert_equal( { :tags => ['ruby'] }, f[:filter][:and].first[:terms] )
end
end
Expand Down Expand Up @@ -214,7 +218,7 @@ class QueryTest < Test::Unit::TestCase
end

query[:filtered].tap do |f|
assert_equal( { :term => { :foo => 'bar' } }, f[:query].to_hash )
assert_equal( { :term => { :foo => { :term => 'bar' } } }, f[:query].to_hash )
assert_equal( { :tags => ['ruby'] }, f[:filter][:and].first[:terms] )
end
end
Expand Down

0 comments on commit ec4e3ef

Please sign in to comment.