-
Notifications
You must be signed in to change notification settings - Fork 2
Metadata filter - Adds metadata filtering support across all search methods #4
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
Open
danielebarbaro
wants to merge
9
commits into
ezimuel:main
Choose a base branch
from
danielebarbaro:feat/metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3fdeee5
WIP - Metadata filter
danielebarbaro 3b42a46
WIP - Metadata filter README
danielebarbaro abbdacd
Metadata Filter eval - wip
danielebarbaro b6b87af
Move MetadataFilter to Metadata namespace and add exists/notExists op…
danielebarbaro d268d13
Add SortDirection enum, replace string validation
danielebarbaro b76bfb3
Move overFetchMultiplier to VectorDatabase
danielebarbaro b677789
Extract applyMetadataFilters to eliminate duplication
danielebarbaro 1e141f2
Skip deleted documents in metadataSearch iteration
danielebarbaro d9bf1c1
Update README for namespace, enum, and new operators
danielebarbaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PHPVector\Metadata; | ||
|
|
||
| use InvalidArgumentException; | ||
|
|
||
| final class MetadataFilter | ||
| { | ||
| private const VALID_OPERATORS = ['=', '!=', '<', '<=', '>', '>=', 'in', 'not_in', 'contains', 'exists', 'not_exists']; | ||
|
|
||
| /** | ||
| * @param string $key Metadata field name to filter on. | ||
| * @param mixed $value Value to compare against. | ||
| * @param string $operator Comparison operator. | ||
| */ | ||
| public function __construct( | ||
| public readonly string $key, | ||
| public readonly mixed $value, | ||
| public readonly string $operator = '=', | ||
| ) { | ||
| if (!in_array($operator, self::VALID_OPERATORS, true)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf( | ||
| 'Unknown operator "%s". Valid operators are: %s', | ||
| $operator, | ||
| implode(', ', self::VALID_OPERATORS) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| if (in_array($operator, ['in', 'not_in'], true) && !is_array($value)) { | ||
| throw new InvalidArgumentException( | ||
| sprintf('Operator "%s" requires an array value.', $operator) | ||
| ); | ||
| } | ||
|
|
||
| if ($operator === 'contains' && is_array($value)) { | ||
| throw new InvalidArgumentException( | ||
| 'Operator "contains" requires a single value, not an array.' | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public static function eq(string $key, mixed $value): self | ||
| { | ||
| return new self($key, $value, '='); | ||
| } | ||
|
|
||
| public static function neq(string $key, mixed $value): self | ||
| { | ||
| return new self($key, $value, '!='); | ||
| } | ||
|
|
||
| public static function lt(string $key, mixed $value): self | ||
| { | ||
| return new self($key, $value, '<'); | ||
| } | ||
|
|
||
| public static function lte(string $key, mixed $value): self | ||
| { | ||
| return new self($key, $value, '<='); | ||
| } | ||
|
|
||
| public static function gt(string $key, mixed $value): self | ||
| { | ||
| return new self($key, $value, '>'); | ||
| } | ||
|
|
||
| public static function gte(string $key, mixed $value): self | ||
| { | ||
| return new self($key, $value, '>='); | ||
| } | ||
|
|
||
| public static function in(string $key, array $values): self | ||
| { | ||
| return new self($key, $values, 'in'); | ||
| } | ||
|
|
||
| public static function notIn(string $key, array $values): self | ||
| { | ||
| return new self($key, $values, 'not_in'); | ||
| } | ||
|
|
||
| public static function contains(string $key, mixed $value): self | ||
| { | ||
| return new self($key, $value, 'contains'); | ||
| } | ||
|
|
||
| public static function exists(string $key): self | ||
| { | ||
| return new self($key, true, 'exists'); | ||
| } | ||
|
|
||
| public static function notExists(string $key): self | ||
| { | ||
| return new self($key, true, 'not_exists'); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs claim metadata filtering uses strict type comparison (===), but the implementation only uses strict comparison for '=', '!=', 'in', 'not_in', and 'contains'. The ordering operators (<, <=, >, >=) use PHP's normal comparisons which can coerce types (e.g., numeric strings). Please clarify this section or enforce strict typing for comparison operators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danielebarbaro can you check this? Thanks.