From: ankane (@ankane) <noreply+ankane@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #961: Feature Request: Jaccard Distance for Integer Arrays
Date: Wed, 04 Mar 2026 07:21:38 +0000
Message-ID: <gh-pgvector-pgvector-961-c3995765319@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-961@github.com>
References: <gh-pgvector-pgvector-961@github.com>
Hi @gerrylwk, I looked into MinHash a while back for #806, but don't think it's common enough to add.
For exact search, you can create a SQL function for it.
```sql
CREATE FUNCTION approximate_jaccard_distance(a bigint[], b bigint[]) RETURNS double precision AS $$
SELECT AVG(CASE WHEN a[i] = b[i] THEN 0 ELSE 1 END) FROM generate_series(1, array_length(a, 1)) i
$$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
```
For approximate search, you can convert the hash to binary and create a custom operator class, but it's not officially supported, so it could break at any time.
```sql
CREATE FUNCTION minhash_distance(a bit, b bit) RETURNS double precision AS $$
SELECT AVG(CASE WHEN substring(a FROM i FOR 64) = substring(b FROM i FOR 64) THEN 0 ELSE 1 END) FROM generate_series(1, bit_length(a), 64) i
$$ LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE;
CREATE OPERATOR <!> (
LEFTARG = bit, RIGHTARG = bit, PROCEDURE = minhash_distance,
COMMUTATOR = '<!>'
);
CREATE OPERATOR CLASS bit_minhash_ops
FOR TYPE bit USING hnsw AS
OPERATOR 1 <!> (bit, bit) FOR ORDER BY float_ops,
FUNCTION 1 minhash_distance(bit, bit),
FUNCTION 3 hnsw_bit_support(internal);
```