From f6c511ce486a815bc192207e4c3901a5ba7ca60d Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 21 Jul 2024 19:12:41 -0400 Subject: [PATCH] Order detected duplicates based on L2 distance from query point --- lib/philomena/duplicate_reports.ex | 10 ++++++++++ lib/philomena/duplicate_reports/power.ex | 9 +++++++++ 2 files changed, 19 insertions(+) create mode 100644 lib/philomena/duplicate_reports/power.ex diff --git a/lib/philomena/duplicate_reports.ex b/lib/philomena/duplicate_reports.ex index 5f84c9da..c6cb2c55 100644 --- a/lib/philomena/duplicate_reports.ex +++ b/lib/philomena/duplicate_reports.ex @@ -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 @@ -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 diff --git a/lib/philomena/duplicate_reports/power.ex b/lib/philomena/duplicate_reports/power.ex new file mode 100644 index 00000000..32f1bc1c --- /dev/null +++ b/lib/philomena/duplicate_reports/power.ex @@ -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