Skip to content

Commit 3ca130d

Browse files
authored
Handle empty whereIn clause (#729)
* handle empty whereIn in Algolia engine * use IN operator in Meilisearch engine
1 parent fbd0bfc commit 3ca130d

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

src/Engines/AlgoliaEngine.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ protected function filters(Builder $builder)
167167
})->values();
168168

169169
return $wheres->merge(collect($builder->whereIns)->map(function ($values, $key) {
170+
if (empty($values)) {
171+
return '0=1';
172+
}
173+
170174
return collect($values)->map(function ($value) use ($key) {
171175
return $key.'='.$value;
172176
})->all();

src/Engines/MeilisearchEngine.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,20 @@ protected function filters(Builder $builder)
173173
}
174174

175175
return is_numeric($value)
176-
? sprintf('%s=%s', $key, $value)
177-
: sprintf('%s="%s"', $key, $value);
176+
? sprintf('%s=%s', $key, $value)
177+
: sprintf('%s="%s"', $key, $value);
178178
});
179179

180180
foreach ($builder->whereIns as $key => $values) {
181-
$filters->push(sprintf('(%s)', collect($values)->map(function ($value) use ($key) {
181+
$filters->push(sprintf('%s IN [%s]', $key, collect($values)->map(function ($value) {
182182
if (is_bool($value)) {
183-
return sprintf('%s=%s', $key, $value ? 'true' : 'false');
183+
return sprintf('%s', $value ? 'true' : 'false');
184184
}
185185

186186
return filter_var($value, FILTER_VALIDATE_INT) !== false
187-
? sprintf('%s=%s', $key, $value)
188-
: sprintf('%s="%s"', $key, $value);
189-
})->values()->implode(' OR ')));
187+
? sprintf('%s', $value)
188+
: sprintf('"%s"', $value);
189+
})->values()->implode(', ')));
190190
}
191191

192192
return $filters->values()->implode(' AND ');

tests/Unit/AlgoliaEngineTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ public function test_search_sends_correct_parameters_to_algolia_for_where_in_sea
136136
$engine->search($builder);
137137
}
138138

139+
public function test_search_sends_correct_parameters_to_algolia_for_empty_where_in_search()
140+
{
141+
$client = m::mock(SearchClient::class);
142+
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
143+
$index->shouldReceive('search')->with('zonda', [
144+
'numericFilters' => ['foo=1', '0=1'],
145+
]);
146+
147+
$engine = new AlgoliaEngine($client);
148+
$builder = new Builder(new SearchableModel, 'zonda');
149+
$builder->where('foo', 1)->whereIn('bar', []);
150+
$engine->search($builder);
151+
}
152+
139153
public function test_map_correctly_maps_results_to_models()
140154
{
141155
$client = m::mock(SearchClient::class);

tests/Unit/MeilisearchEngineTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ public function test_where_in_conditions_are_applied()
514514
$client = m::mock(Client::class);
515515
$client->shouldReceive('index')->once()->andReturn($index = m::mock(Indexes::class));
516516
$index->shouldReceive('rawSearch')->once()->with($builder->query, array_filter([
517-
'filter' => 'foo="bar" AND bar="baz" AND (qux=1 OR qux=2) AND (quux=1 OR quux=2)',
517+
'filter' => 'foo="bar" AND bar="baz" AND qux IN [1, 2] AND quux IN [1, 2]',
518518
'hitsPerPage' => $builder->limit,
519519
]))->andReturn([]);
520520

@@ -530,7 +530,24 @@ public function test_where_in_conditions_are_applied_without_other_conditions()
530530
$client = m::mock(Client::class);
531531
$client->shouldReceive('index')->once()->andReturn($index = m::mock(Indexes::class));
532532
$index->shouldReceive('rawSearch')->once()->with($builder->query, array_filter([
533-
'filter' => '(qux=1 OR qux=2) AND (quux=1 OR quux=2)',
533+
'filter' => 'qux IN [1, 2] AND quux IN [1, 2]',
534+
'hitsPerPage' => $builder->limit,
535+
]))->andReturn([]);
536+
537+
$engine = new MeilisearchEngine($client);
538+
$engine->search($builder);
539+
}
540+
541+
public function test_empty_where_in_conditions_are_applied_correctly()
542+
{
543+
$builder = new Builder(new SearchableModel(), '');
544+
$builder->where('foo', 'bar');
545+
$builder->where('bar', 'baz');
546+
$builder->whereIn('qux', []);
547+
$client = m::mock(Client::class);
548+
$client->shouldReceive('index')->once()->andReturn($index = m::mock(Indexes::class));
549+
$index->shouldReceive('rawSearch')->once()->with($builder->query, array_filter([
550+
'filter' => 'foo="bar" AND bar="baz" AND qux IN []',
534551
'hitsPerPage' => $builder->limit,
535552
]))->andReturn([]);
536553

0 commit comments

Comments
 (0)