Postgres: index on cosine similarity of float arrays for one-to-many search
问题 Cosine similarity between two equally-sized vectors (of reals) is defined as the dot product divided by the product of the norms. To represent vectors, I have a large table of float arrays, e.g. CREATE TABLE foo(vec float[])' . Given a certain float array, I need to quickly (with an index, not a seqscan) find the closest arrays in that table by cosine similarity, e.g. SELECT * FROM foo ORDER BY cos_sim(vec, ARRAY[1.0, 4.5, 2.2]) DESC LIMIT 10; But what do I use? pg_trgm 's cosine similarity