feat: add ltree filtering operators to v2 vector store#289
Open
airbag_deer (airbagdeer) wants to merge 4 commits into
Open
feat: add ltree filtering operators to v2 vector store#289airbag_deer (airbagdeer) wants to merge 4 commits into
airbag_deer (airbagdeer) wants to merge 4 commits into
Conversation
Add 5 PostgreSQL ltree-specific filter operators to the metadata filtering system: \$ancestor (@>), \$descendant (<@), \$lquery (~), \$lquery_any (?), and \$ltxtquery (@). Supports both dedicated ltree columns and JSON-stored metadata fields via automatic ::ltree cast. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace :param::type casts with CAST(:param AS type) to prevent psycopg3's parameter parser from misinterpreting the :: PostgreSQL cast operator immediately following a named parameter placeholder. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The inline comments incorrectly referenced `value::type` cast syntax. The actual SQL uses `CAST(value AS type)` to avoid a psycopg3 bug where `::` immediately after a named placeholder breaks parameter substitution. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Author
|
Please review my pull request |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds 5 PostgreSQL `ltree` operator filters to the v2 vector store metadata filtering system.
Summary
Extends the MongoDB-style filter system with operators that map directly to PostgreSQL's built-in `ltree` extension, enabling hierarchical path queries over tree-structured label data such as category taxonomies, org charts, and file paths.
Motivation
In many retrieval system, a typical tree document system is implemented for order of files.
The existing filter system has no way to query `ltree`-typed columns or hierarchical path data stored in the JSON metadata column. Applications that classify documents with paths like `Top.Science.Astronomy` need ancestor/descendant traversal and pattern-matching that the `ltree` extension provides natively — none of which is expressible with `$eq`, `$like`, or the other existing operators.
Changes
New Operators
Usage Examples
```python
$ancestor — find categories that are on the path UP TO "Top.Science.Astronomy"
("Top" and "Top.Science" are ancestors of "Top.Science.Astronomy")
results = await vs.asimilarity_search(
"query", filter={"category": {"$ancestor": "Top.Science.Astronomy"}}
)
$descendant — find all documents under "Top.Science" (inclusive)
results = await vs.asimilarity_search(
"query", filter={"category": {"$descendant": "Top.Science"}}
)
$lquery — match direct children of Top only
results = await vs.asimilarity_search(
"query", filter={"category": {"$lquery": "Top.*{1}"}}
)
$lquery_any — match either of two exact paths
results = await vs.asimilarity_search(
"query", filter={"category": {"$lquery_any": ["Top.Science", "Top.Technology"]}}
)
$ltxtquery — match any path containing the label word "Science"
results = await vs.asimilarity_search(
"query", filter={"category": {"$ltxtquery": "Science"}}
)
```
Implementation Details
Test Coverage
Breaking Changes
None — fully backward compatible.
Checklist