scout
scout copied to clipboard
[Algolia] Invalid syntax for numeric condition:status=open
Scout Version
10.9.0
Scout Driver
Algolia
Laravel Version
11.7
PHP Version
8.2.17
Database Driver & Version
No response
SDK Version
No response
Meilisearch CLI Version
No response
Description
Invalid syntax for numeric condition:status=open {"exception":"[object] (Algolia\AlgoliaSearch\Exceptions\BadRequestException(code: 400): Invalid syntax for numeric condition:status=open at /xxxx/vendor/algolia/algoliasearch-client-php/src/RetryStrategy/ApiWrapper.php:221)
Array
(
[body] => Array
(
[numericFilters] => Array
(
[0] => Array
(
[0] => status=open
[1] => status=paid
)
)
[hitsPerPage] => 15
[page] => 0
[query] =>
)
[headers] => Array
(
[X-Algolia-Application-Id] => xxxxxx
[X-Algolia-API-Key] => xxxxxx
[User-Agent] => Algolia for PHP (3.4.1); PHP (8.2.17); Guzzle (7); Laravel Scout (10.9.0)
[Content-Type] => application/json
)
[method] => POST
[query] => Array
(
)
[retryNumber] => 1
[host] => https://xxxxx-dsn.algolia.net/1/indexes/dev_order_index/query
)
The input filters are placed in numericFilters
, but those are not numeric values, and should be place in filters
as status:open OR status:paid
Steps To Reproduce
- use algolia engine
- create an index
- search with
whereIn
described in https://laravel.com/docs/11.x/scout#where-clauses
For those who want to search with filters, you can set filters by options
as:
$orders = Order::search('Star Trek')->options([
'filters' => 'status:open OR status:paid',
])->get();
Could you share an exact code sample to reproduce this?
@driesvints
- Set the engine to algolia
- Set
User
model searchable (or any searchable model) - Put the following code to your
routes/console.php
- Run
php artisan scout-where-in
// ......
use App\Models\User;
// ......
Artisan::command('scout-where-in', function () {
User::search('hi')
->whereIn('status', ['open', 'paid'])
->get();
});
And you will get
Algolia\AlgoliaSearch\Exceptions\BadRequestException
Invalid syntax for numeric condition:status=open
at vendor/algolia/algoliasearch-client-php/src/RetryStrategy/ApiWrapper.php:221
217▕
218▕ if (404 == $statusCode) {
219▕ throw new NotFoundException($responseArray['message'], $statusCode);
220▕ } elseif ($statusCode >= 400) {
➜ 221▕ throw new BadRequestException($responseArray['message'], $statusCode);
222▕ } elseif (2 != (int) ($statusCode / 100)) {
223▕ throw new AlgoliaException($statusCode.': '.$body, $statusCode);
224▕ }
225▕
+7 vendor frames
8 routes/console.php:9
Laravel\Scout\Builder::get()
+13 vendor frames
22 artisan:13
Illuminate\Foundation\Application::handleCommand(Object(Symfony\Component\Console\Input\ArgvInput))
I'm so confused. This indeed can't ever work how it's implemented right now but has been around for like 8 years already. I can't believe we never caught this before...
Hah, I originally documented this 😂 https://github.com/laravel/docs/pull/7550
I guess I misunderstood how this worked in Algolia. However, we can't change the docs since meilisearch and other engines now work this way. I feel like we should update the AlgoliaEngine to use filters
instead of numericFilters
. The Algolia docs also say this should be used preferable: https://www.algolia.com/doc/api-reference/api-parameters/numericFilters/
The refactored code to filters
should behave the same as in the code example from numericFilters
below:
Thank you for reporting this issue!
As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.
If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.
Thank you!
@driesvints are we going to close this issue now? https://github.com/laravel/scout/pull/837
Going to close this one now but would like the PR from @Boorinio to be sent to 11.x instead. Thanks!
Made a pr for 11.x https://github.com/laravel/scout/pull/839