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

fix(typesense): properly format boolean filters in Typesense #874

Merged
merged 4 commits into from
Oct 29, 2024

Conversation

tharropoulos
Copy link
Contributor

Rationale

This change addresses an issue where boolean filters were not properly handled in the TypesenseEngine for Laravel Scout. When users attempted to search with a boolean filter (e.g., Model::search($query)->where('myFilter', true)->get()), the engine was incorrectly transforming boolean values to integers before passing them to the Typesense SDK. This resulted in a Typesense\Exceptions\RequestMalformed exception with the message "Value of filter field myFilter must be true or false."

By implementing this fix, we ensure that boolean filters are correctly formatted and passed to Typesense, allowing users to perform searches with boolean conditions as expected.

Changes

Code Changes:

  1. In src/Engines/TypesenseEngine.php:
    • Modified the filters method to use a new parseFilterValue method when processing wheres and whereIns.
    • Added a new parseFilterValue method to handle various data types, including booleans:
      protected function parseFilterValue(array|string|bool|int|float $value)
      {
          if (is_array($value)) {
              return array_map([$this, 'parseFilterValue'], $value);
          }
      
          if (gettype($value) == 'boolean') {
              return $value ? 'true' : 'false';
          }
      
          return $value;
      }
    • Updated parseWhereInFilter method to use string interpolation for better readability.

Test Updates:

  1. In tests/Unit/TypesenseEngineTest.php:
    • Added new test methods to cover the changes:
      • test_filters_method: Ensures the filters method correctly formats multiple conditions.
      • test_parse_filter_value_method: Verifies the new parseFilterValue method handles various input types correctly.
      • test_parse_where_filter_method: Checks the parseWhereFilter method's output for different inputs.
      • test_parse_where_in_filter_method: Validates the parseWhereInFilter method's formatting.
    • Implemented a helper method invokeMethod to test protected methods.

Context

This change was prompted by #873. @bredmor encountered a RequestMalformed exception when attempting to use boolean filters with Typesense in Laravel Scout. The problem was traced to the TypesenseEngine::filters() method, which was not properly handling boolean values.

This fix ensures that boolean values are correctly formatted as strings ('true' or 'false') before being sent to Typesense, resolving the exception and allowing users to successfully perform searches with boolean filters.

- Add a new function `parseFilterValue` to check for its type and
properly format values to typesense syntax
- Add unit tests for filtering
@tharropoulos tharropoulos marked this pull request as draft October 21, 2024 11:12
@tharropoulos tharropoulos marked this pull request as ready for review October 25, 2024 11:10
@taylorotwell taylorotwell merged commit b8ce0fe into laravel:10.x Oct 29, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants