Skip to content
This repository has been archived by the owner on Jun 30, 2018. It is now read-only.

Commit

Permalink
Adds has_child support for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
kofno committed Aug 13, 2012
1 parent 5129f8a commit f1f297c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/tire/search/query.rb
Expand Up @@ -75,6 +75,13 @@ def dis_max(options={}, &block)
@value
end

def has_child(options={}, &block)
@has_child = Query.new(&block)
@value[:has_child] = options
@value[:has_child].update({ :query => @has_child.to_hash })
@value
end

def all
@value = { :match_all => {} }
@value
Expand Down
25 changes: 25 additions & 0 deletions test/unit/search_query_test.rb
Expand Up @@ -297,5 +297,30 @@ class QueryTest < Test::Unit::TestCase

end

context "Has child query" do

should "not raise an error when no block is given" do
assert_nothing_raised { Query.new.has_child }
end

should "allow search for documents with children" do
query = Query.new.has_child do
string 'foo'
end

assert_equal( { :has_child => { :query => { :query_string => { :query => 'foo' } } } },
query.to_hash )
end

should "encode options" do
query = Query.new.has_child('_scope' => 'foo', :type => 'bar') do
string 'baz'
end

assert_equal('foo', query.to_hash[:has_child]['_scope'])
assert_equal('bar', query.to_hash[:has_child][:type])
end
end

end
end

0 comments on commit f1f297c

Please sign in to comment.