Skip to content

Conversation

@artemkdr
Copy link
Contributor

@artemkdr artemkdr commented Sep 3, 2025

Add support for advanced metadata filters in PGVector similarity search

This PR enhances the PGVector vector store integration by adding support for advanced metadata filtering capabilities during similarity search operations.

Changes

  • Added support for filtering on metadata fields with various operators (lt, lte, gt, gte, neq)

Benefits

  • Users can now perform filtering based on metadata criteria in the same way as for Python Langchain framework

Testing

  • Added comprehensive tests for various metadata filter scenarios

This enhancement makes the PGVector integration more powerful and flexible for applications that need to combine semantic similarity with metadata-based filtering.

@changeset-bot
Copy link

changeset-bot bot commented Sep 3, 2025

🦋 Changeset detected

Latest commit: 0882997

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@langchain/community Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Sep 3, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
langchainjs-docs Ready Ready Preview Sep 9, 2025 5:29pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchainjs-api-refs Ignored Ignored Sep 9, 2025 5:29pm

Copy link
Member

@hntrl hntrl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! couple of Q's:

  • Could we add to the pgvector docs for these filter types?
  • Could we rework the Metadata type on line 11 to understand these new filter types?

@artemkdr
Copy link
Contributor Author

artemkdr commented Sep 5, 2025

Thank you @hntrl for the review.
I will add the new filters to the docs and I will check for the Metadata update.

- Introduced a new `MetadataFilter` type to define the structure of filter objects.
- Implemented a `buildFilterClauses` method to dynamically construct SQL WHERE clauses based on the provided filters.
- Updated PGVectorStore documentation

Fix:
- deleteByFilter method didn't apply complex filters at all, now it uses 'buildFilterClauses' to build the proper query string;

New tests:
- Expanded integration tests to cover all new filtering functionalities, ensuring correct behavior for each operator.
- Refactored existing tests to utilize the new filtering structure, improving test clarity and coverage.
@artemkdr
Copy link
Contributor Author

artemkdr commented Sep 6, 2025

@hntrl

  • the pg vector store docs were updated for the new filters
  • I renamed Metadata to MetadataFilter with a strict type and documentation
  • Integration tests were enhanced and adapted for better coverage of different cases
  • I fixed a bug with deleteByFilter (it didn't apply complex filters)

@artemkdr artemkdr changed the title Add support for advanced metadata filters in similarity search feat(langchain-community/pgvector): add support for advanced metadata filters in similarity search Sep 6, 2025
* {
* category: "test", // Simple equality
* score: { gte: 80 }, // Operator filter
* tags: { in: ["test1", "test2"] } // Another operator filter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@hntrl hntrl self-assigned this Sep 9, 2025
@hntrl hntrl merged commit 8ac8edd into langchain-ai:main Sep 9, 2025
33 checks passed
hntrl added a commit that referenced this pull request Sep 9, 2025
hntrl added a commit that referenced this pull request Oct 14, 2025
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.

3 participants