Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filter_by => belongs_to_id:123 #19

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading