Skip to content

Commit

Permalink
feat(typesense): add whereNotIn filter to typesense engine
Browse files Browse the repository at this point in the history
- Add new `parseWhereNotInFilter` method for excluding values
- Update filters method to include whereNotIn conditions
- Add test coverage for whereNotIn filtering functionality
  • Loading branch information
tharropoulos committed Nov 7, 2024
1 parent 641c45c commit 9254fa4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,16 @@ protected function filters(Builder $builder): string
->values()
->implode(' && ');

return $whereFilter.(
($whereFilter !== '' && $whereInFilter !== '') ? ' && ' : ''
).$whereInFilter;
$whereNotInFilter = collect($builder->whereNotIns)
->map(fn ($value, $key) => $this->parseWhereNotInFilter($this->parseFilterValue($value), $key))
->values()
->implode(' && ');

$filters = collect([$whereFilter, $whereInFilter, $whereNotInFilter])
->filter()
->implode(' && ');

return $filters;
}

/**
Expand Down Expand Up @@ -414,6 +421,18 @@ protected function parseWhereInFilter(array $value, string $key): string
return sprintf('%s:=[%s]', $key, implode(', ', $value));
}

/**
* Create a "where not in" filter string.
*
* @param array|string $value
* @param string $key
* @return string
*/
protected function parseWhereNotInFilter(array $value, string $key): string
{
return sprintf('%s:!=[%s]', $key, implode(', ', $value));
}

/**
* Parse the order by fields for the query.
*
Expand Down
10 changes: 9 additions & 1 deletion tests/Unit/TypesenseEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ public function test_filters_method()
$builder->whereIns = [
'category' => ['electronics', 'books'],
];
$builder->whereNotIns = [
'category' => ['furniture', 'phones'],
];

$result = $this->invokeMethod($this->engine, 'filters', [$builder]);

$expected = 'status:=active && age:=25 && category:=[electronics, books]';
$expected = 'status:=active && age:=25 && category:=[electronics, books] && category:!=[furniture, phones]';
$this->assertEquals($expected, $result);
}

Expand Down Expand Up @@ -100,6 +103,11 @@ public function test_parse_where_in_filter_method()
$this->assertEquals('id:=[1, 2, 3]', $this->invokeMethod($this->engine, 'parseWhereInFilter', [[1, 2, 3], 'id']));
}

public function test_parse_where_not_in_filter_metheod()
{
$this->assertEquals('category:!=[electronics, books]', $this->invokeMethod($this->engine, 'parseWhereNotInFilter', [['electronics', 'books'], 'category']));
$this->assertEquals('id:!=[1, 2, 3]', $this->invokeMethod($this->engine, 'parseWhereNotInFilter', [[1, 2, 3], 'id']));
}
public function test_update_method(): void
{
// Mock models and their methods
Expand Down

0 comments on commit 9254fa4

Please sign in to comment.