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

feat(typesense): add whereNotIn filter to typesense engine #878

Merged
merged 2 commits into from
Nov 8, 2024

Conversation

tharropoulos
Copy link
Contributor

Rationale

Currently, the Typesense driver doesn't process whereNotIn constraints in search queries, which limits the ability to exclude specific records from search results.

For example, when searching for products and wanting to exclude specific categories:

Product::search('laptop')->whereNotIn('category', ['refurbished', 'used'])->get();

This enhancement implements the missing whereNotIn functionality, allowing users to properly filter out unwanted results from their Typesense searches, making the search experience more flexible and precise.

Changes

Added Features:

  1. New Methods in src/Engines/TypesenseEngine.php:
    • parseWhereNotInFilter(): Handles the conversion of whereNotIn constraints to
      Typesense filter syntax using !=[...] notation.

Code Changes:

  1. In src/Engines/TypesenseEngine.php:
    • Enhanced filters() method to process whereNotIn constraints alongside
      existing where and whereIn filters
    • Improved filter concatenation logic to handle all filter types using collection
      methods
    • Updated filter string generation to maintain proper && operator placement

Test Updates:

  1. In tests/Unit/TypesenseEngineTest.php:
    • Added new test method test_parse_where_not_in_filter_metheod() to verify
      whereNotIn filter generation
    • Enhanced test_filters_method() to include whereNotIn scenarios
    • Added test cases for both string and numeric value exclusions

Context

This PR addresses issue #876 reported by @alexfierro where whereNotIn constraints were
not being handled by the Typesense driver. The implementation follows the suggested
approach of adapting the existing whereIn logic with the appropriate Typesense
filter syntax for exclusions.

Before:

Product::search('laptop')->whereNotIn('category', ['refurbished']) // ❌ Ignored

After:

// Generates: category:!=[refurbished]
Product::search('laptop')->whereNotIn('category', ['refurbished']) // ✅ Works!

- Add new `parseWhereNotInFilter` method for excluding values
- Update filters method to include whereNotIn conditions
- Add test coverage for whereNotIn filtering functionality
@taylorotwell taylorotwell merged commit b6a98ae into laravel:10.x Nov 8, 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