Skip to content

Commit

Permalink
Handle empty whereIn clause (#729)
Browse files Browse the repository at this point in the history
* handle empty whereIn in Algolia engine

* use IN operator in Meilisearch engine
  • Loading branch information
jshah4517 authored Apr 14, 2023
1 parent fbd0bfc commit 3ca130d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ protected function filters(Builder $builder)
})->values();

return $wheres->merge(collect($builder->whereIns)->map(function ($values, $key) {
if (empty($values)) {
return '0=1';
}

return collect($values)->map(function ($value) use ($key) {
return $key.'='.$value;
})->all();
Expand Down
14 changes: 7 additions & 7 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,20 @@ protected function filters(Builder $builder)
}

return is_numeric($value)
? sprintf('%s=%s', $key, $value)
: sprintf('%s="%s"', $key, $value);
? sprintf('%s=%s', $key, $value)
: sprintf('%s="%s"', $key, $value);
});

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

return filter_var($value, FILTER_VALIDATE_INT) !== false
? sprintf('%s=%s', $key, $value)
: sprintf('%s="%s"', $key, $value);
})->values()->implode(' OR ')));
? sprintf('%s', $value)
: sprintf('"%s"', $value);
})->values()->implode(', ')));
}

return $filters->values()->implode(' AND ');
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ public function test_search_sends_correct_parameters_to_algolia_for_where_in_sea
$engine->search($builder);
}

public function test_search_sends_correct_parameters_to_algolia_for_empty_where_in_search()
{
$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('search')->with('zonda', [
'numericFilters' => ['foo=1', '0=1'],
]);

$engine = new AlgoliaEngine($client);
$builder = new Builder(new SearchableModel, 'zonda');
$builder->where('foo', 1)->whereIn('bar', []);
$engine->search($builder);
}

public function test_map_correctly_maps_results_to_models()
{
$client = m::mock(SearchClient::class);
Expand Down
21 changes: 19 additions & 2 deletions tests/Unit/MeilisearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ public function test_where_in_conditions_are_applied()
$client = m::mock(Client::class);
$client->shouldReceive('index')->once()->andReturn($index = m::mock(Indexes::class));
$index->shouldReceive('rawSearch')->once()->with($builder->query, array_filter([
'filter' => 'foo="bar" AND bar="baz" AND (qux=1 OR qux=2) AND (quux=1 OR quux=2)',
'filter' => 'foo="bar" AND bar="baz" AND qux IN [1, 2] AND quux IN [1, 2]',
'hitsPerPage' => $builder->limit,
]))->andReturn([]);

Expand All @@ -530,7 +530,24 @@ public function test_where_in_conditions_are_applied_without_other_conditions()
$client = m::mock(Client::class);
$client->shouldReceive('index')->once()->andReturn($index = m::mock(Indexes::class));
$index->shouldReceive('rawSearch')->once()->with($builder->query, array_filter([
'filter' => '(qux=1 OR qux=2) AND (quux=1 OR quux=2)',
'filter' => 'qux IN [1, 2] AND quux IN [1, 2]',
'hitsPerPage' => $builder->limit,
]))->andReturn([]);

$engine = new MeilisearchEngine($client);
$engine->search($builder);
}

public function test_empty_where_in_conditions_are_applied_correctly()
{
$builder = new Builder(new SearchableModel(), '');
$builder->where('foo', 'bar');
$builder->where('bar', 'baz');
$builder->whereIn('qux', []);
$client = m::mock(Client::class);
$client->shouldReceive('index')->once()->andReturn($index = m::mock(Indexes::class));
$index->shouldReceive('rawSearch')->once()->with($builder->query, array_filter([
'filter' => 'foo="bar" AND bar="baz" AND qux IN []',
'hitsPerPage' => $builder->limit,
]))->andReturn([]);

Expand Down

0 comments on commit 3ca130d

Please sign in to comment.