Skip to content

Commit

Permalink
fix: filter_by => belongs_to_id:123 (#19)
Browse files Browse the repository at this point in the history
* fix: filter_by => belongs_to_id:123

* style: 💄 clean code
  • Loading branch information
ThaddeusJiang authored Oct 22, 2024
1 parent 6d8354c commit b3ac13d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/save_it/bot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ defmodule SaveIt.Bot do
end

def handle({:command, :search, %{chat: chat, photo: nil, text: q}}, _context) do
photos = TypesensePhoto.search_photos!(q: q)
photos = TypesensePhoto.search_photos!(q: q, belongs_to_id: chat.id)

answer_photos(chat.id, photos)
end
Expand Down Expand Up @@ -266,7 +266,8 @@ defmodule SaveIt.Bot do
if typesense_photo != nil do
TypesensePhoto.search_photos!(
typesense_photo["id"],
distance_threshold: distance_threshold
distance_threshold: distance_threshold,
belongs_to_id: chat_id
)
end
end
Expand Down
11 changes: 9 additions & 2 deletions lib/save_it/typesense_photo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule SaveIt.TypesensePhoto do
Typesense.get_document("photos", photo_id)
end

def search_photos!(q: q) do
def search_photos!(q: q, belongs_to_id: belongs_to_id) do
req_body = %{
"searches" => [
%{
Expand All @@ -34,6 +34,7 @@ defmodule SaveIt.TypesensePhoto do
"collection" => "photos",
"prefix" => false,
"vector_query" => "image_embedding:([], k: 5, distance_threshold: 0.75)",
"filter_by" => "belongs_to_id:#{belongs_to_id}",
"exclude_fields" => "image_embedding"
}
]
Expand All @@ -46,12 +47,17 @@ defmodule SaveIt.TypesensePhoto do
end

def search_photos!(id, opts \\ []) do
belongs_to_id = Keyword.get(opts, :belongs_to_id)
distance_threshold = Keyword.get(opts, :distance_threshold, 0.4)

search_similar_photos!(id, distance_threshold: distance_threshold)
search_similar_photos!(id,
distance_threshold: distance_threshold,
belongs_to_id: belongs_to_id
)
end

def search_similar_photos!(photo_id, opts \\ []) when is_binary(photo_id) do
belongs_to_id = Keyword.get(opts, :belongs_to_id)
distance_threshold = Keyword.get(opts, :distance_threshold, 0.4)

req_body = %{
Expand All @@ -61,6 +67,7 @@ defmodule SaveIt.TypesensePhoto do
"q" => "*",
"vector_query" =>
"image_embedding:([], id:#{photo_id}, distance_threshold: #{distance_threshold}, k: 4)",
"filter_by" => "belongs_to_id:#{belongs_to_id}",
"exclude_fields" => "image_embedding"
}
]
Expand Down

0 comments on commit b3ac13d

Please sign in to comment.