Skip to content

Commit b9e2637

Browse files
committed
Improved hamming_distance and jaccard_distance
1 parent a717213 commit b9e2637

File tree

3 files changed

+4
-8
lines changed

3 files changed

+4
-8
lines changed

lib/pgvector/ecto/query.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ if Code.ensure_loaded?(Ecto) do
4545
"""
4646
defmacro hamming_distance(column, value) do
4747
quote do
48-
fragment("(? <~> ?)", unquote(column), ^Pgvector.Ecto.Utils.to_bit_sql(unquote(value)))
48+
fragment("(? <~> ?)", unquote(column), unquote(value))
4949
end
5050
end
5151

@@ -54,7 +54,7 @@ if Code.ensure_loaded?(Ecto) do
5454
"""
5555
defmacro jaccard_distance(column, value) do
5656
quote do
57-
fragment("(? <%> ?)", unquote(column), ^Pgvector.Ecto.Utils.to_bit_sql(unquote(value)))
57+
fragment("(? <%> ?)", unquote(column), unquote(value))
5858
end
5959
end
6060
end

lib/pgvector/ecto/utils.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,4 @@ defmodule Pgvector.Ecto.Utils do
1313
def to_sql(vector) do
1414
vector |> Pgvector.new()
1515
end
16-
17-
def to_bit_sql(vector) when is_bitstring(vector) do
18-
vector
19-
end
2016
end

test/ecto_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ defmodule EctoTest do
8282
end
8383

8484
test "bit hamming distance" do
85-
items = Repo.all(from i in Item, order_by: hamming_distance(i.binary_embedding, <<1::1, 0::1, 1::1>>), limit: 5)
85+
items = Repo.all(from i in Item, order_by: hamming_distance(i.binary_embedding, ^<<1::1, 0::1, 1::1>>), limit: 5)
8686
assert Enum.map(items, fn v -> v.id end) == [2, 3, 1]
8787
end
8888

8989
test "bit jaccard distance" do
90-
items = Repo.all(from i in Item, order_by: jaccard_distance(i.binary_embedding, <<1::1, 0::1, 1::1>>), limit: 5)
90+
items = Repo.all(from i in Item, order_by: jaccard_distance(i.binary_embedding, ^<<1::1, 0::1, 1::1>>), limit: 5)
9191
assert Enum.map(items, fn v -> v.id end) == [2, 3, 1]
9292
end
9393

0 commit comments

Comments
 (0)