Adds has_child support for queries #431

Closed
wants to merge 1 commit into
from
View
@@ -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
@@ -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