Skip to content

[Store][Pg] Add support for custom "where" expression #235

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

Merged
merged 1 commit into from
Aug 1, 2025

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Jul 31, 2025

Q A
Bug fix? no
New feature? yes
Docs? no
Issues
License MIT

I want to be able to filter some record before comparison.
Usage:

$crawlAId = '013e951c-335f-4e2b-9414-9eedbf0f51df';
$crawlBId = '396af6fe-0dfd-47ed-b222-3dbcced3f38e';

$rows = $connection
    ->executeQuery(<<<SQL
        SELECT *
        FROM {$tableName}
        WHERE metadata->>'crawlId' = '{$crawlAId}'
    SQL)
    ->fetchAllAssociative()
;

foreach ($rows as $i => $row) {
    $vector = new Vector(json_decode($row['embedding'], true));
    $documents = $store->query(
        $vector,
        [
            'maxScore' => 0.1,
            'where' => "metadata->>'crawlId' = :crawlId AND id != :currentId",
            'params' => [
                'crawlId' => $crawlBId,
                'currentId' => $row['id'],
            ],
        ],
    );

    if (!$documents) {
        continue;
    }

    $metadata = json_decode($row['metadata'], true, 512, \JSON_THROW_ON_ERROR);
    echo "Current document: {$metadata['url']}\n";
    foreach ($documents as $i => $document) {
        echo "- {$document->metadata['url']} (score: {$document->score})\n";
        if ($i >= 1) {
            break;
        }
    }
    echo "\n";
}

@OskarStark
Copy link
Contributor

We should merge #233 first and rebase this PR then

@OskarStark
Copy link
Contributor

Rebase unlocked :-)

@lyrixx
Copy link
Member Author

lyrixx commented Aug 1, 2025

Here we go! I have rebased, and added (true) support for param bindings

@OskarStark
Copy link
Contributor

Thank you @lyrixx.

@OskarStark OskarStark merged commit 3394f86 into symfony:main Aug 1, 2025
7 checks passed
OskarStark added a commit to OskarStark/ai that referenced this pull request Aug 1, 2025
This adds comprehensive test coverage for the new custom where expression feature merged in PR symfony#235:
- Test custom where expression without maxScore
- Test custom where expression combined with maxScore
- Test custom where expression with custom parameters
@lyrixx lyrixx deleted the PG-where branch August 1, 2025 08:46
chr-hertel added a commit that referenced this pull request Aug 1, 2025
…skarStark)

This PR was merged into the main branch.

Discussion
----------

[Store][Postgres] Add tests for custom where expression

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| Docs?         | no
| Issues        | Refs #235
| License       | MIT

This adds comprehensive test coverage for the new custom where expression feature merged in PR #235:
- Test custom where expression without maxScore
- Test custom where expression combined with maxScore
- Test custom where expression with custom parameters

cc `@lyrixx`

Commits
-------

107ccc4 Add tests for custom where expression in Postgres Store
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