pgvector / pgvector-php

pgvector support for PHP
MIT License
121 stars 7 forks source link

Add method to get operator for distance #12

Closed jtomek closed 4 months ago

ankane commented 4 months ago

Hi @jtomek, thanks for the PR, but this doesn't seem like a common need.

jtomek commented 4 months ago

But why not centralize the logic? Each Vector Operator has an name, operator string, description, compatibility (e.g. Hamming and Jaccard work with binary vectors), etc.

To future-proof the package and make it flexible, it would be nice to centralize this logic. Moreover, in my particular case, I find myself needing to generate othe pgvector queries with dynamic operators and use the following:

    /**
     * @see https://github.com/pgvector/pgvector-php/pull/12
     */
    protected function tempGetDistanceOperator(): string
    {
        return match ($this->distance) {
            Distance::L2 => '<->',
            Distance::InnerProduct => '<#>',
            Distance::Cosine => '<=>',
            Distance::L1 => '<+>',
            Distance::Hamming => '<~>',
            Distance::Jaccard => '<%>',
            default => throw new \InvalidArgumentException('Invalid distance')
        };
    }

which fits, in my view, into the enum.

Thank you for considering my comment.

P.S. This is my first ever pull request. :)