laravel / scout

Laravel Scout provides a driver based solution to searching your Eloquent models.
https://laravel.com/docs/scout
MIT License
1.52k stars 324 forks source link

[Algolia] Invalid syntax for numeric condition:status=open #831

Closed yueyuzhao closed 1 month ago

yueyuzhao commented 2 months ago

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

  1. use algolia engine
  2. create an index
  3. search with whereIn described in https://laravel.com/docs/11.x/scout#where-clauses
yueyuzhao commented 2 months ago

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();
driesvints commented 2 months ago

Could you share an exact code sample to reproduce this?

yueyuzhao commented 2 months ago

@driesvints

  1. Set the engine to algolia
  2. Set User model searchable (or any searchable model)
  3. Put the following code to your routes/console.php
  4. 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))

driesvints commented 2 months ago

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...

driesvints commented 2 months ago

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/

Screenshot 2024-05-17 at 12 12 01

The refactored code to filters should behave the same as in the code example from numericFilters below:

Screenshot 2024-05-17 at 12 18 12
github-actions[bot] commented 2 months ago

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!

Boorinio commented 1 month ago

@driesvints are we going to close this issue now? https://github.com/laravel/scout/pull/837

driesvints commented 1 month ago

Going to close this one now but would like the PR from @Boorinio to be sent to 11.x instead. Thanks!

Boorinio commented 1 month ago

Made a pr for 11.x https://github.com/laravel/scout/pull/839