From: ankane (@ankane) <noreply+ankane@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
Date: Fri, 28 Mar 2025 21:42:15 +0000
Message-ID: <gh-pgvector-pgvector-806-c2762622688@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-806@github.com>
References: <gh-pgvector-pgvector-806@github.com>
Just to follow up, here's how you could add it unofficially to a database:
```sql
CREATE FUNCTION bit_negative_inner_product(bit, bit) RETURNS float8
AS 'BEGIN RETURN -bit_count($1 & $2); END;'
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
CREATE OPERATOR <#> (
LEFTARG = bit, RIGHTARG = bit, PROCEDURE = bit_negative_inner_product,
COMMUTATOR = '<#>'
);
CREATE OPERATOR CLASS bit_ip_ops
FOR TYPE bit USING hnsw AS
OPERATOR 1 <#> (bit, bit) FOR ORDER BY float_ops,
FUNCTION 1 bit_negative_inner_product(bit, bit),
FUNCTION 3 hnsw_bit_support(internal);
```