scout icon indicating copy to clipboard operation
scout copied to clipboard

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

Open yueyuzhao opened this issue 9 months ago • 6 comments

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 avatar May 13 '24 13:05 yueyuzhao

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();

yueyuzhao avatar May 13 '24 13:05 yueyuzhao

Could you share an exact code sample to reproduce this?

driesvints avatar May 13 '24 13:05 driesvints

@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))

yueyuzhao avatar May 13 '24 22:05 yueyuzhao

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 avatar May 17 '24 10:05 driesvints

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

driesvints avatar May 17 '24 10:05 driesvints

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!

github-actions[bot] avatar May 17 '24 10:05 github-actions[bot]

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

Boorinio avatar Jun 12 '24 13:06 Boorinio

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

driesvints avatar Jun 18 '24 16:06 driesvints

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

Boorinio avatar Jun 18 '24 19:06 Boorinio