fix(typesense): properly format boolean filters in Typesense #874
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 aTypesense\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:
src/Engines/TypesenseEngine.php
:filters
method to use a newparseFilterValue
method when processingwheres
andwhereIns
.parseFilterValue
method to handle various data types, including booleans:parseWhereInFilter
method to use string interpolation for better readability.Test Updates:
tests/Unit/TypesenseEngineTest.php
:test_filters_method
: Ensures thefilters
method correctly formats multiple conditions.test_parse_filter_value_method
: Verifies the newparseFilterValue
method handles various input types correctly.test_parse_where_filter_method
: Checks theparseWhereFilter
method's output for different inputs.test_parse_where_in_filter_method
: Validates theparseWhereInFilter
method's formatting.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 theTypesenseEngine::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.