Skip to content

Commit

Permalink
Filter nil components out of connectives
Browse files Browse the repository at this point in the history
[sunspot#113 state:resolved]
  • Loading branch information
Mat Brown committed Jul 15, 2010
1 parent e53b4bf commit 710cbfd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
10 changes: 7 additions & 3 deletions sunspot/lib/sunspot/query/connective.rb
Expand Up @@ -80,10 +80,14 @@ def to_boolean_phrase #:nodoc:
if @components.length == 1
@components.first.to_boolean_phrase
else
component_phrases = @components.map do |component|
component.to_boolean_phrase
component_phrases = []
@components.each do |component|
boolean_phrase = component.to_boolean_phrase
component_phrases << boolean_phrase if boolean_phrase
end
unless component_phrases.empty?
"(#{component_phrases.join(" #{connector} ")})"
end
"(#{component_phrases.join(" #{connector} ")})"
end
if negated?
"-#{phrase}"
Expand Down
9 changes: 8 additions & 1 deletion sunspot/lib/sunspot/query/scope.rb
Expand Up @@ -2,7 +2,14 @@ module Sunspot
module Query
class Scope < Connective::Conjunction
def to_params
{ :fq => @components.map { |component| component.to_filter_query }}
filters = []
@components.each do |component|
filter = component.to_filter_query
filters << filter unless filter.nil?
end
if filters.empty? then {}
else { :fq => filters}
end
end
end
end
Expand Down
12 changes: 11 additions & 1 deletion sunspot/spec/api/query/connectives_examples.rb
Expand Up @@ -167,10 +167,20 @@
)
end

it 'should ignore empty connectives' do
it 'ignores empty connectives' do
search do
any_of {}
end
connection.should_not have_last_search_including(:fq, '')
end

it 'ignores multiple empty nested connectives' do # regression test
search do
any_of do
all_of { any_of {}}
all_of { any_of {}}
end
end
connection.searches.last[:fq].should have(1).item
end
end

0 comments on commit 710cbfd

Please sign in to comment.