Skip to content

Commit

Permalink
[11.x] Removed numeric filters (#839)
Browse files Browse the repository at this point in the history
* Removed numericFilters from AlgoliaEngine.

* Added upgrade entry for algolia filters.

* Update UPGRADE.md

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
Boorinio and taylorotwell committed Jun 20, 2024
1 parent 7f8ae9f commit 86afd4f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Upgrade Guide

## Upgrading To 11.0 From 10.x

### Algolia Engine `filters`

In previous Scout releases, the Algolia engine utilized `numericFilters` to power `where` conditions. However, `numericFilters` does not support simple string matching. In Scout 11.x, `filters` is now used instead of `numericFilters`.

## Upgrading To 10.0 From 9.x

### Minimum Versions
Expand Down
16 changes: 8 additions & 8 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function delete($models)
public function search(Builder $builder)
{
return $this->performSearch($builder, array_filter([
'numericFilters' => $this->filters($builder),
'filters' => $this->filters($builder),
'hitsPerPage' => $builder->limit,
]));
}
Expand All @@ -121,7 +121,7 @@ public function search(Builder $builder)
public function paginate(Builder $builder, $perPage, $page)
{
return $this->performSearch($builder, [
'numericFilters' => $this->filters($builder),
'filters' => $this->filters($builder),
'hitsPerPage' => $perPage,
'page' => $page - 1,
]);
Expand Down Expand Up @@ -163,18 +163,18 @@ protected function performSearch(Builder $builder, array $options = [])
protected function filters(Builder $builder)
{
$wheres = collect($builder->wheres)->map(function ($value, $key) {
return $key.'='.$value;
return $key.":'{$value}'";
})->values();

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

return collect($values)->map(function ($value) use ($key) {
return $key.'='.$value;
})->all();
})->values())->values()->all();
return '('.collect($values)->map(function ($value) use ($key) {
return $key.":'{$value}'";
})->implode(' OR ').')';
})->values()->implode(' AND '))->values()->filter()->implode(' AND ');
}

/**
Expand Down
9 changes: 5 additions & 4 deletions tests/Unit/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function test_search_sends_correct_parameters_to_algolia()
$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('search')->with('zonda', [
'numericFilters' => ['foo=1'],
'filters' => "foo:'1'",
]);

$engine = new AlgoliaEngine($client);
Expand All @@ -127,12 +127,13 @@ public function test_search_sends_correct_parameters_to_algolia_for_where_in_sea
$client = m::mock(SearchClient::class);
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock(stdClass::class));
$index->shouldReceive('search')->with('zonda', [
'numericFilters' => ['foo=1', ['bar=1', 'bar=2']],
'filters' => "foo:'1' AND (bar:'1' OR bar:'2') AND (qux:'2' OR qux:'3')",
]);

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

Expand All @@ -141,7 +142,7 @@ public function test_search_sends_correct_parameters_to_algolia_for_empty_where_
$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'],
'filters' => "foo:'1' AND 0:1",
]);

$engine = new AlgoliaEngine($client);
Expand Down

0 comments on commit 86afd4f

Please sign in to comment.