Skip to content

Commit

Permalink
fix: не добавляем в композитный матчинг пустые условия
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryBochkarev committed Dec 19, 2017
1 parent 96b579c commit a105fa7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/sphinx/integration/extensions/thinking_sphinx/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def with_composite_index_conditions

composite_indexes.each do |name, fields|
composite_conditions = fields.keys.each_with_object([]) do |field_name, memo|
memo << "(#{conditions.delete(field_name)})" if conditions.key?(field_name)
condition = conditions.delete(field_name) if conditions.key?(field_name)
memo << "(#{condition})" if condition.present?
end

next if composite_conditions.empty?
Expand All @@ -45,7 +46,7 @@ def with_composite_index_conditions
old_composite_condition = conditions[name]

conditions[name] =
if old_composite_condition
if old_composite_condition.present?
"(#{old_composite_condition}) #{composite_conditions}"
else
composite_conditions
Expand Down
54 changes: 54 additions & 0 deletions spec/sphinx/integration/extensions/thinking_sphinx/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,60 @@
expect(riddle_client).to have_received(:query)
.with("@one_idx one @composite_idx (a_2 | a_1) (b_1 | b_2)", 'composite', any_args)
end

context 'when condition contains empty string' do
it do
ModelWithDisk.search(conditions: {one_idx: 'one', b_idx: ' ', a_idx: "a_2 | a_1"}).to_a

expect(riddle_client).to have_received(:query)
.with("@one_idx one @composite_idx (a_2 | a_1)", 'composite', any_args)
end
end

context 'when condition contains nil' do
it do
ModelWithDisk.search(conditions: {one_idx: 'one', b_idx: nil, a_idx: "a_2 | a_1"}).to_a

expect(riddle_client).to have_received(:query)
.with("@one_idx one @composite_idx (a_2 | a_1)", 'composite', any_args)
end
end

context 'when old composite condition' do
it do
ModelWithDisk.search(conditions: {
one_idx: 'one',
composite_idx: 'z_1 | z_2', b_idx: 'b_1 | b_2', a_idx: "a_2 | a_1"
}).to_a

expect(riddle_client).to have_received(:query)
.with("@one_idx one @composite_idx (z_1 | z_2) (a_2 | a_1) (b_1 | b_2)", 'composite', any_args)
end
end

context 'when old composite condition is a empty string' do
it do
ModelWithDisk.search(conditions: {
one_idx: 'one',
composite_idx: ' ', b_idx: 'b_1 | b_2', a_idx: "a_2 | a_1"
}).to_a

expect(riddle_client).to have_received(:query)
.with("@one_idx one @composite_idx (a_2 | a_1) (b_1 | b_2)", 'composite', any_args)
end
end

context 'when old composite condition is a nil' do
it do
ModelWithDisk.search(conditions: {
one_idx: 'one',
composite_idx: nil, b_idx: 'b_1 | b_2', a_idx: "a_2 | a_1"
}).to_a

expect(riddle_client).to have_received(:query)
.with("@one_idx one @composite_idx (a_2 | a_1) (b_1 | b_2)", 'composite', any_args)
end
end
end
end
end

0 comments on commit a105fa7

Please sign in to comment.