diff --git a/UPGRADE.md b/UPGRADE.md index 90eae78d..147fecb5 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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 diff --git a/src/Engines/AlgoliaEngine.php b/src/Engines/AlgoliaEngine.php index cec72d48..b38220df 100644 --- a/src/Engines/AlgoliaEngine.php +++ b/src/Engines/AlgoliaEngine.php @@ -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, ])); } @@ -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, ]); @@ -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 '); } /** diff --git a/tests/Unit/AlgoliaEngineTest.php b/tests/Unit/AlgoliaEngineTest.php index 98c991b1..a1eac86f 100644 --- a/tests/Unit/AlgoliaEngineTest.php +++ b/tests/Unit/AlgoliaEngineTest.php @@ -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); @@ -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); } @@ -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);