Merge pull request #332 from mdashlw/reverse-limit

feat: support limit param in reverse search
This commit is contained in:
liamwhite 2024-07-19 08:27:29 -04:00 committed by GitHub
commit 11b414bf7b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 3 deletions

View file

@ -25,7 +25,7 @@ defmodule Philomena.DuplicateReports do
end)
end
def duplicates_of(intensities, aspect_ratio, dist \\ 0.25, aspect_dist \\ 0.05) do
def duplicates_of(intensities, aspect_ratio, dist \\ 0.25, aspect_dist \\ 0.05, limit \\ 10) do
# for each color channel
dist = dist * 3
@ -39,7 +39,7 @@ defmodule Philomena.DuplicateReports do
where:
i.image_aspect_ratio >= ^(aspect_ratio - aspect_dist) and
i.image_aspect_ratio <= ^(aspect_ratio + aspect_dist),
limit: 10
limit: ^limit
end
@doc """

View file

@ -13,6 +13,7 @@ defmodule PhilomenaWeb.Api.Json.Search.ReverseController do
images =
image_params
|> Map.put("distance", conn.params["distance"])
|> Map.put("limit", conn.params["limit"])
|> ImageReverse.images()
interactions = Interactions.user_interactions(images, user)

View file

@ -18,8 +18,9 @@ defmodule PhilomenaWeb.ImageReverse do
{width, height} = analysis.dimensions
aspect = width / height
dist = normalize_dist(image_params)
limit = parse_limit(image_params)
DuplicateReports.duplicates_of(intensities, aspect, dist, dist)
DuplicateReports.duplicates_of(intensities, aspect, dist, dist, limit)
|> preload([:user, :intensity, [:sources, tags: :aliases]])
|> Repo.all()
end
@ -60,4 +61,17 @@ defmodule PhilomenaWeb.ImageReverse do
0.0
end
end
defp parse_limit(%{"limit" => limit}) do
limit
|> Integer.parse()
|> case do
{limit, _rest} -> limit
_ -> 10
end
|> max(1)
|> min(50)
end
defp parse_limit(_), do: 10
end