Skip to content

Commit

Permalink
Merge pull request #338 from philomena-dev/dr-sort
Browse files Browse the repository at this point in the history
Order detected duplicates based on L2 distance from query point
  • Loading branch information
liamwhite authored Jul 22, 2024
2 parents 08d74b9 + f6c511c commit d1f4eb9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/philomena/duplicate_reports.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ defmodule Philomena.DuplicateReports do
The DuplicateReports context.
"""

import Philomena.DuplicateReports.Power
import Ecto.Query, warn: false

alias Ecto.Multi
alias Philomena.Repo

Expand Down Expand Up @@ -46,6 +48,14 @@ defmodule Philomena.DuplicateReports do
where:
i.image_aspect_ratio >= ^(aspect_ratio - aspect_dist) and
i.image_aspect_ratio <= ^(aspect_ratio + aspect_dist),
order_by: [
asc:
power(it.nw - ^intensities.nw, 2) +
power(it.ne - ^intensities.ne, 2) +
power(it.sw - ^intensities.sw, 2) +
power(it.se - ^intensities.se, 2) +
power(i.image_aspect_ratio - ^aspect_ratio, 2)
],
limit: ^limit
end

Expand Down
9 changes: 9 additions & 0 deletions lib/philomena/duplicate_reports/power.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Philomena.DuplicateReports.Power do
@moduledoc false

defmacro power(left, right) do
quote do
fragment("power(?, ?)", unquote(left), unquote(right))
end
end
end

0 comments on commit d1f4eb9

Please sign in to comment.