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

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

Open
yueyuzhao opened this issue May 13, 2024 · 6 comments
Open

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

yueyuzhao opened this issue May 13, 2024 · 6 comments

Comments

@yueyuzhao
Copy link

yueyuzhao commented May 13, 2024

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
Copy link
Author

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
Copy link
Member

Could you share an exact code sample to reproduce this?

@yueyuzhao
Copy link
Author

@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
Copy link
Member

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
Copy link
Member

Hah, I originally documented this 😂 laravel/docs#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

Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants