Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Removed numeric filters #839

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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